mirror of
https://notabug.org/scuti/pleroma-comments
synced 2025-03-14 07:44:28 +05:30
clean up main function
This commit is contained in:
parent
63fed806ac
commit
a02033cc83
@ -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
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user