From e955d42525ced0147fb05ff20b3099fe84833d32 Mon Sep 17 00:00:00 2001 From: scuti Date: Thu, 23 Jan 2025 18:33:58 -0800 Subject: [PATCH] merged branch 'auth' for instances that require auth to access API --- pleroma-comments.lua | 97 ++++++++++++++++++++++++++++++++------------ 1 file changed, 70 insertions(+), 27 deletions(-) diff --git a/pleroma-comments.lua b/pleroma-comments.lua index 716d7bf..fda4397 100644 --- a/pleroma-comments.lua +++ b/pleroma-comments.lua @@ -1,24 +1,30 @@ local json = require("cjson") -pleroma_avatar_save_path = nil -- location of downloaded images -pleroma_avatar_path = nil -- prepended to image file, written on page --- to configure, put above vars in a file named 'config.lua' +-- GLOBAL VARIABLES +-- & meant to be assigned in 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( function () dofile("config.lua") end ) + --[[ -if these variables are still `nil` after `dofile()` then -the script will look for it in the yaml header. +if `pleroma_avatar_*` are still `nil` after `dofile()` then the script will look for it in the yaml header. -note: lua filters can not access variables defined on the command line, -e.g `pandoc -V foo="bar"` -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. +if they are still nil afterwards, then the script +will hotlink. ]]-- @@ -54,34 +60,50 @@ function tokenizeString(inputString, delimiter) return tokens end -function get(link, filename) +function get(link, filename, auth) print("http/s GET: ".. link) + local filename = filename or nil + local auth = auth or nil + local args = {} - if filename then + if filename then -- when requesting avatars args = { "--timeout=10", "-qO", filename, link } - else + else -- when requesting json args = { "-qO-", "--timeout=10", link } 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, ' ') + print(command) local success, retval = pcall( function () --- print("!: ".. table.concat(args)) - return pandoc.pipe("wget", args, "") + local handle = io.popen(command) + local result = handle:read("*a") + handle:close() + return result end ) if not success then print("warning: error while performing http/s GET") - print(retval) + print("\treturned: " .. tostring(retval)) end return retval end @@ -328,16 +350,27 @@ function get_url_from_pandoc_str(pandoc_str) return link, host, id end -function get_status(host, post_id) +function get_status(host, post_id, auth) local url = "https://" .. host .. "/api/v1/statuses/" .. post_id --- print(url) - return json.decode(get(url)) + local success, retval = pcall( + 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 -function get_replies(host, id) +function get_replies(host, id, auth) local url = "https://" .. host .. "/api/v1/statuses/" .. id .. "/context" -- print(url) - local got = json.decode(get(url)) + local got = json.decode(get(url, nil, auth)) return got["descendants"] end @@ -382,7 +415,6 @@ function Meta(meta) -- if pleroma_avatar_save_path and pleroma_avatar_path then -- is_hotlink = false -- end - local all_replies = {} local hrefs = {} local host = "" @@ -390,12 +422,25 @@ function Meta(meta) for _, v in pairs(pleroma_urls) do local link, domain, id = get_url_from_pandoc_str(v) 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, - {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) - local replies = get_replies(host, id) + local replies = get_replies(host, id, auth_key) combine_tables(all_replies, replies) end table.sort(all_replies, @@ -418,5 +463,3 @@ function Meta(meta) meta["pleroma"] = hrefs return meta end - -