merged branch 'auth' for instances that require auth to access API

This commit is contained in:
scuti 2025-01-23 18:33:58 -08:00
parent c4cd40a0de
commit e955d42525

View File

@ -1,24 +1,30 @@
local json = require("cjson") local json = require("cjson")
pleroma_avatar_save_path = nil -- location of downloaded images -- GLOBAL VARIABLES
pleroma_avatar_path = nil -- prepended to image file, written on page -- & meant to be assigned in config.lua
-- to configure, put above vars in a file named 'config.lua' --------------------
-- location of downloaded images
pleroma_avatar_save_path = nil
-- prepended to image file, written on page
pleroma_avatar_path = nil
-- instances may require auth to access api
pleroma_auth = {}
-- pleroma_auth["instance.tld"] = "your_auth_key"
pcall( pcall(
function () function ()
dofile("config.lua") dofile("config.lua")
end end
) )
--[[ --[[
if these variables are still `nil` after `dofile()` then if `pleroma_avatar_*` are still `nil` after `dofile()` then the script will look for it in the yaml header.
the script will look for it in the yaml header.
note: lua filters can not access variables defined on the command line, if they are still nil afterwards, then the script
e.g `pandoc -V foo="bar"` will hotlink.
and it may not be ideal to define paths in yaml headers,
i.e changing the path for avatars on a website would involve editing each
manuscript.
]]-- ]]--
@ -54,34 +60,50 @@ function tokenizeString(inputString, delimiter)
return tokens return tokens
end end
function get(link, filename) function get(link, filename, auth)
print("http/s GET: ".. link) print("http/s GET: ".. link)
local filename = filename or nil local filename = filename or nil
local auth = auth or nil
local args = {} local args = {}
if filename then if filename then -- when requesting avatars
args = { args = {
"--timeout=10", "--timeout=10",
"-qO", "-qO",
filename, filename,
link link
} }
else else -- when requesting json
args = { args = {
"-qO-", "-qO-",
"--timeout=10", "--timeout=10",
link link
} }
end end
-- don't use auth bearer for downloading images'
-- its either not needed OR
-- there should be an extra check on the host
if auth and not filename then
local h = "--header=\"Authorization: Bearer %s\" "
table.insert(
args, 1, string.format(h, auth)
)
end
local command = "wget " .. table.concat(args, ' ') local command = "wget " .. table.concat(args, ' ')
print(command)
local success, retval = pcall( local success, retval = pcall(
function () function ()
-- print("!: ".. table.concat(args)) local handle = io.popen(command)
return pandoc.pipe("wget", args, "") local result = handle:read("*a")
handle:close()
return result
end end
) )
if not success then if not success then
print("warning: error while performing http/s GET") print("warning: error while performing http/s GET")
print(retval) print("\treturned: " .. tostring(retval))
end end
return retval return retval
end end
@ -328,16 +350,27 @@ function get_url_from_pandoc_str(pandoc_str)
return link, host, id return link, host, id
end end
function get_status(host, post_id) function get_status(host, post_id, auth)
local url = "https://" .. host .. "/api/v1/statuses/" .. post_id local url = "https://" .. host .. "/api/v1/statuses/" .. post_id
-- print(url) local success, retval = pcall(
return json.decode(get(url)) function ()
local got = get(url, nil, auth)
-- print(got)
return json.decode(got)
end
)
-- if an error occurred in retrieving a status
-- it will cause errors in write_comments
--consider skipping over statuses later
assert(success)
assert(not retval["error"])
return retval
end end
function get_replies(host, id) function get_replies(host, id, auth)
local url = "https://" .. host .. "/api/v1/statuses/" .. id .. "/context" local url = "https://" .. host .. "/api/v1/statuses/" .. id .. "/context"
-- print(url) -- print(url)
local got = json.decode(get(url)) local got = json.decode(get(url, nil, auth))
return got["descendants"] return got["descendants"]
end end
@ -382,7 +415,6 @@ function Meta(meta)
-- if pleroma_avatar_save_path and pleroma_avatar_path then -- if pleroma_avatar_save_path and pleroma_avatar_path then
-- is_hotlink = false -- is_hotlink = false
-- end -- end
local all_replies = {} local all_replies = {}
local hrefs = {} local hrefs = {}
local host = "" local host = ""
@ -390,12 +422,25 @@ function Meta(meta)
for _, v in pairs(pleroma_urls) do for _, v in pairs(pleroma_urls) do
local link, domain, id = get_url_from_pandoc_str(v) local link, domain, id = get_url_from_pandoc_str(v)
host = domain host = domain
-- list of links people can reply using
local reply_href = link
if type(pleroma_reply_href) == "string" then
local temp = "https://%s%s%s"
reply_href = string.format(
temp, host, pleroma_reply_href, id)
end
table.insert(hrefs, table.insert(hrefs,
{link = link, id = id} {link = reply_href, id = id}
) )
local op = get_status(host, id)
local auth_key = nil
if pleroma_auth[host] then
auth_key = pleroma_auth[host]
end
local op = get_status(host, id, auth_key)
table.insert(all_replies, op) table.insert(all_replies, op)
local replies = get_replies(host, id) local replies = get_replies(host, id, auth_key)
combine_tables(all_replies, replies) combine_tables(all_replies, replies)
end end
table.sort(all_replies, table.sort(all_replies,
@ -418,5 +463,3 @@ function Meta(meta)
meta["pleroma"] = hrefs meta["pleroma"] = hrefs
return meta return meta
end end