From a02033cc83cf622297259224d418fd950576ee61 Mon Sep 17 00:00:00 2001 From: scuti Date: Tue, 26 Nov 2024 03:06:49 -0800 Subject: [PATCH] clean up main function --- pleroma-comments.lua | 60 ++++++++++++++++++++++++++++++-------------- 1 file changed, 41 insertions(+), 19 deletions(-) diff --git a/pleroma-comments.lua b/pleroma-comments.lua index 2a4aba6..3687315 100644 --- a/pleroma-comments.lua +++ b/pleroma-comments.lua @@ -3,6 +3,7 @@ -- local https = "wget" local json = require("cjson") +-- for testing function printTable(t) for key, value in pairs(t) do print(key, value) @@ -71,7 +72,7 @@ function write_comments(pleroma_posts, instance) local uid = post["account"]["id"] local alias = post["account"]["display_name"] local handle = post["account"]["acct"] - local text = post["pleroma"]["content"]["text/plain"] + local text = post["content"] local interpolated = template:gsub("%$(%w+)%$", { i= #replies - i + 1, host=instance, @@ -99,32 +100,53 @@ function combine_tables(a,b) return a end +function get_opening_post(host, post_id) + local url = "https://" .. host .. "/api/v1/statuses" .. post_id + print(url) + local received = get(url) +end + +function get_url_from_pandoc_str(pandoc_str) + local str = pandoc.utils.stringify(pandoc_str) + -- 1 = protocol, 2 = host ... + -- https://host.tld/notice/12345 + local tokens = tokenizeString(str, '/') + local id = tokens[#tokens] + + local host = tokens[2] + local id = tokens[#tokens] + local link = str + + return link, host, id +end + +function get_replies(host, id) + local url = "https://" .. host .. "/api/v1/statuses/" .. id .. "/context" + print(url) + local got = get(url) + return got["descendants"] +end + function Meta(meta) - local elem = meta["pleroma-urls"] + local pleroma_urls = meta["pleroma-urls"] -- print(elem) - if elem == nil then + if pleroma_urls == nil then return -- abort end local all_replies = {} - local newelem = {} + local hrefs = {} local host = "" - for _, v in pairs(elem) do - local str = pandoc.utils.stringify(v) - print(str) - -- 1 = protocol, 2 = host ... - -- https://host.tld/notice/12345 - local tokens = tokenizeString(str, '/') - local id = tokens[#tokens] - table.insert(newelem, - {link = str, id = id} + -- for each listed url in "pleroma-urls" + for _, v in pairs(pleroma_urls) do + local link, host, id = get_url_from_pandoc_str(v) + table.insert(hrefs, + {link = link, id = id} ) - host = tokens[2] - local url = "https://" .. host .. "/api/v1/statuses/" .. id .. "/context" - local got = get(url) + local replies = get_replies(host, id) if #all_replies == 0 then - all_replies = got["descendants"] + all_replies = replies else - combine_tables(all_replies, got["descendants"]) + combine_tables(all_replies, replies) end end table.sort(all_replies, @@ -138,7 +160,7 @@ function Meta(meta) meta["pleroma-comments"] = c meta["pleroma-comments-count"] = #c meta["pleroma-has-comments"] = (#c > 0) - meta["pleroma"] = newelem + meta["pleroma"] = hrefs return meta end