pleroma-comments/pleroma-comments.lua

146 lines
3.7 KiB
Lua
Raw Normal View History

2024-07-07 12:21:28 +05:30
-- can't resolve seggfault on luasec, luasocket'
-- local https = "wget"
local json = require("cjson")
function printTable(t)
for key, value in pairs(t) do
print(key, value)
end
end
2024-07-07 12:21:28 +05:30
function tokenizeString(inputString, delimiter)
local tokens = {}
for token in inputString:gmatch("[^" .. delimiter .. "]+") do
table.insert(tokens, token)
end
return tokens
end
function get(link)
local args = {
"-qO-",
link
}
local data = pandoc.pipe("wget", args, "")
local parsed = json.decode(data)
-- print(link)
return parsed
end
2024-07-07 14:01:03 +05:30
function get_epoch_time(timestamp)
2024-07-07 12:21:28 +05:30
local pattern = "(%d+)-(%d+)-(%d+)T(%d+):(%d+):(%d+).000Z"
local year, month, day, hour, min, sec = timestamp:match(pattern)
local epoch = os.time({
year = year,
month = month,
day = day,
hour = hour,
min = min,
sec = sec
})
2024-07-07 14:01:03 +05:30
return epoch
end
function get_short_date(timestamp)
return os.date(
"%a, %B %d, %Y", get_epoch_time(timestamp)
)
2024-07-07 12:21:28 +05:30
end
2024-07-07 13:52:00 +05:30
function write_comments(pleroma_posts, instance)
if #pleroma_posts == 0 then
return ""
end
2024-07-07 12:21:28 +05:30
local template = [[
<article class="pleroma-comment" id="pleroma-comment$i$">
<h3>
#$i$ <a href="$host$/notice/$pid$">$datetime$</a>
</h3>
<p>$alias$ <a href="$host$/users/$uid$">@$handle$</a></p>
<blockquote>
$text$
</blockquote>
</article>
2024-07-07 12:21:28 +05:30
]]
local comments = {}
2024-07-07 14:37:13 +05:30
local replies = pleroma_posts-- ["descendants"]
for i, post in ipairs(replies) do
2024-07-07 13:52:00 +05:30
local pid = post["id"]
2024-07-07 14:01:03 +05:30
local datetime = get_short_date(post["created_at"])
2024-07-07 13:52:00 +05:30
local uid = post["account"]["id"]
local alias = post["account"]["display_name"]
local handle = post["account"]["acct"]
2024-07-07 12:21:28 +05:30
local text = post["pleroma"]["content"]["text/plain"]
local interpolated = template:gsub("%$(%w+)%$", {
i= #replies - i + 1,
2024-07-07 13:52:00 +05:30
host=instance,
pid=pid,
2024-07-07 12:21:28 +05:30
datetime=datetime,
2024-07-07 13:52:00 +05:30
uid = uid,
handle = handle,
alias=alias,
2024-07-07 12:21:28 +05:30
text = text
})
2024-07-07 13:52:00 +05:30
-- print(interpolated)
table.insert(
comments, pandoc.RawBlock("html", interpolated)
)
2024-07-07 12:21:28 +05:30
end
2024-07-07 13:52:00 +05:30
-- print(comments)
2024-07-07 12:21:28 +05:30
return comments
end
2024-07-07 14:37:13 +05:30
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
2024-07-07 12:21:28 +05:30
function Meta(meta)
2024-07-07 14:01:03 +05:30
local elem = meta["pleroma-urls"]
2024-07-07 14:37:13 +05:30
-- print(elem)
2024-07-07 12:21:28 +05:30
if elem == nil then
return -- abort
end
2024-07-07 14:37:13 +05:30
local all_replies = {}
local newelem = {}
2024-07-07 14:37:13 +05:30
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, '/')
2024-07-07 14:37:13 +05:30
local id = tokens[#tokens]
table.insert(newelem,
{link = str, id = id}
)
2024-07-07 14:37:13 +05:30
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
2024-07-07 14:37:13 +05:30
end
)
local c = write_comments(all_replies, "https://" .. host)
meta["pleroma-comments"] = c
meta["pleroma-comments-count"] = #c
meta["pleroma-has-comments"] = (#c > 0)
meta["pleroma"] = newelem
2024-07-07 13:52:00 +05:30
return meta
2024-07-07 12:21:28 +05:30
end