support auth with multiple instances

This commit is contained in:
scuti 2024-12-10 17:50:39 -08:00
parent 17c5474f06
commit 43115f1a42

View File

@ -10,7 +10,8 @@ pleroma_avatar_save_path = nil
pleroma_avatar_path = nil pleroma_avatar_path = nil
-- instances may require auth to access api -- instances may require auth to access api
-- pleroma_auth_token = nil -- pleroma_auth = {}
-- pleroma_auth["instance.tld"] = "your_auth_key"
pcall( pcall(
function () function ()
@ -59,34 +60,12 @@ function tokenizeString(inputString, delimiter)
return tokens return tokens
end end
-- function get(link) function get(link, filename, auth)
-- print("http/s GET: ".. link)
-- local args = {link}
--
-- if pleroma_auth_token then
-- local auth = string.format(
-- "\'Authorization: Bearer %s\' ", pleroma_auth_token)
-- table.insert(args, 1, "-H")
-- table.insert(args, 2, auth)
-- end
--
-- local command = "curl " .. table.concat(args, ' ')
-- print(command)
-- local success, retval = pcall(
-- function ()
-- return pandoc.pipe("curl", args, "")
-- end
-- )
-- if not success then
-- print("warning: error while performing http/s GET")
-- print("\treturned: " .. tostring(retval))
-- end
-- return retval
-- end
function get(link, filename)
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 -- when requesting avatars if filename then -- when requesting avatars
args = { args = {
@ -105,10 +84,10 @@ function get(link, filename)
-- don't use auth bearer for downloading images' -- don't use auth bearer for downloading images'
-- its either not needed OR -- its either not needed OR
-- there should be an extra check on the host -- there should be an extra check on the host
if pleroma_auth_token and not filename then if auth and not filename then
local h = "--header=\"Authorization: Bearer %s\" " local h = "--header=\"Authorization: Bearer %s\" "
table.insert( table.insert(
args, 1, string.format(h, pleroma_auth_token) args, 1, string.format(h, auth)
) )
end end
@ -374,11 +353,11 @@ 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
local success, retval = pcall( local success, retval = pcall(
function () function ()
local got = get(url) local got = get(url, nil, auth)
-- print(got) -- print(got)
return json.decode(got) return json.decode(got)
end end
@ -391,10 +370,10 @@ function get_status(host, post_id)
return retval 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
@ -458,9 +437,13 @@ function Meta(meta)
{link = reply_href, 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,
@ -483,5 +466,3 @@ function Meta(meta)
meta["pleroma"] = hrefs meta["pleroma"] = hrefs
return meta return meta
end end