Continue expanding addon system

This commit is contained in:
Robin Malley 2023-02-20 05:06:36 +00:00
parent 66b48f06a4
commit 443a6331e6
4 changed files with 76 additions and 6 deletions

View File

@ -156,6 +156,7 @@ domain * {
validate qs:get story v_storyid
validate qs:get pwd v_hex_128
validate qs:get fmt v_any
}
route /_preview {

View File

@ -5,6 +5,7 @@ local db = require("db")
local queries = require("queries")
local util = require("util")
local pages = require("pages")
local api = require("hooks")
local stmnt_download
local oldconfigure = configure
@ -13,6 +14,24 @@ function configure(...)
return oldconfigure(...)
end
local oldget = api.get.page_reader
api.get.page_reader = function(env)
local ret = oldget(env)
local button = {
endpoint = string.format("https://%s/_download",env.domain),
method = "GET",
fields = {
story = env.short,
},
text = "Download TXT"
}
if env.unlisted then
button.fields.pwd = env.hashstr
end
table.insert(ret,button)
return ret
end
local function download_get(req)
local host = http_request_get_host(req)
local path = http_request_get_path(req)

45
src/lua/hooks.lua Normal file
View File

@ -0,0 +1,45 @@
--[[
Global functions that smr exposes that can be overriden by addons
]]
local api = {}
-- Called before any request processing. Returning true "traps" the request, and
-- does not continue calling smr logic, or logic of any other addons.
api.pre_request = function(req) end
-- Called after smr request processing. Returning true "traps" the request, and
-- does not continue calling any other addon logic. This will not stop smr from
-- responding to the request, since by this time http_request_response() has
-- already been called.
api.post_request = function(req) end
-- Called during startup of the worker process
-- Failures in this function will prevent kore from starting.
-- Return value is ignored.
api.worker_init = function() end
-- Called during shutdown of the worker process
-- Failures in this function cause other addon hooks to this function to be skipped.
-- Return value is ignored.
api.worker_shutdown = function() end
-- The following are tables and their options:
-- "buttonspec" - specifies a button to display on the front end, has the fields:
-- .endpoint - the url to go to when the button is pressed
-- .method - the HTTP method to use to call the endpoint
-- .fields - key/value pairs to send as arguments when calling the endpoint
-- These are usually "hidden" fields for a form.
-- .text - The text that displays on the button
-- Called to display configuration as html.
api.get = {
-- returns an array of buttonspec, displayed at the top of a story,
-- only for the logged in owner of a story.
page_owner = function(env) return {} end,
-- returns an array of buttonspec, displayed at the bottom of a story
page_reader = function(env) return {} end,
}
return api

View File

@ -37,13 +37,18 @@
<p><%= views %> Hits</p>
<form action="https://<%= domain %>/_download" method="get">
<input type="hidden" name="story" value="<%= short %>"/>
<% if unlisted then %>
<input type="hidden" name="pwd" value="<%= hashstr %>"/>
<div class="row">
<% for _, spec in ipairs(api.get.page_reader(getfenv(1))) do %>
<form action="<%= spec.endpoint %>" method="<%= spec.method %>"><fieldset>
<% for key, value in pairs(spec.fields) do %>
<input type="hidden" name="<%= key %>" value="<%= value %>"/>
<% end %>
<input type="submit" value="Download TXT" class="button"/>
</form>
<input type="submit" value="<%= spec.text %>" class="button">
</fieldset></form>
<% end %>
</div>
<% if not show_comments then -%>
<form action="https://<%= domain %>/<%= short %>"><fieldset>
<input type="hidden" name="comments" value="1">