From 50dc454443586e3ba91e95ceb16ff9de8b4c47fd Mon Sep 17 00:00:00 2001 From: antares Date: Sun, 7 Jul 2024 02:07:13 -0700 Subject: [PATCH] works for multiple urls --- pleroma-comments.lua | 45 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 35 insertions(+), 10 deletions(-) diff --git a/pleroma-comments.lua b/pleroma-comments.lua index 7de7b11..6e3806f 100644 --- a/pleroma-comments.lua +++ b/pleroma-comments.lua @@ -53,7 +53,7 @@ function write_comments(pleroma_posts, instance) ]] local comments = {} - local replies = pleroma_posts["descendants"] + local replies = pleroma_posts-- ["descendants"] for _, post in ipairs(replies) do local pid = post["id"] local datetime = get_short_date(post["created_at"]) @@ -77,21 +77,46 @@ function write_comments(pleroma_posts, instance) return comments end +function combine_tables(a,b) + -- iterate through b, add to a + for i=1,#b do + table.insert(a, b[i]) + end + return a +end function Meta(meta) local elem = meta["pleroma-urls"] - print(elem) +-- print(elem) if elem == nil then return -- abort end - local str = pandoc.utils.stringify(elem) - local tokens = tokenizeString(str, '/') - -- 1 = protocol, 2 = host ... - -- https://host.tld/notice/12345 - local id = tokens[#tokens] - local host = tokens[2] - local url = "https://" .. host .. "/api/v1/statuses/" .. id .. "/context" - meta["comments"] = write_comments(get(url), "https://" .. host) + local all_replies = {} + local host = "" + for _, v in pairs(elem) do + local str = pandoc.utils.stringify(v) + print(str) + local tokens = tokenizeString(str, '/') + -- 1 = protocol, 2 = host ... + -- https://host.tld/notice/12345 + local id = tokens[#tokens] + host = tokens[2] + local url = "https://" .. host .. "/api/v1/statuses/" .. id .. "/context" + local got = get(url) + if #all_replies == 0 then + all_replies = got["descendants"] + else + combine_tables(all_replies, got["descendants"]) + end + end + table.sort(all_replies, + function(a, b) + local ta = get_epoch_time(a["created_at"]) + local tb = get_epoch_time(b["created_at"]) + return ta < tb + end + ) + meta["comments"] = write_comments(all_replies, "https://" .. host) return meta end