pleroma-comments/pleroma-comments.lua

93 lines
2.3 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 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
function short_date(timestamp)
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
})
return os.date("%a, %B %d, %Y", epoch)
end
2024-07-07 13:52:00 +05:30
function write_comments(pleroma_posts, instance)
2024-07-07 12:21:28 +05:30
local template = [[
2024-07-07 13:52:00 +05:30
<h4 class="pleroma-comment">
<a href="$host$/notice/$pid$">$datetime$</a>
</h4>
<a href="$host$/users/$uid$" class="pleroma-user">$alias$</a>
<blockquote>
$text$
</blockquote>
2024-07-07 12:21:28 +05:30
]]
local comments = {}
local replies = pleroma_posts["descendants"]
for _, post in ipairs(replies) do
2024-07-07 13:52:00 +05:30
local pid = post["id"]
2024-07-07 12:21:28 +05:30
local datetime = short_date(post["created_at"])
2024-07-07 13:52:00 +05:30
local uid = post["account"]["id"]
2024-07-07 12:21:28 +05:30
local alias = post["account"]["acct"]
local text = post["pleroma"]["content"]["text/plain"]
local interpolated = template:gsub("%$(%w+)%$", {
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,
2024-07-07 12:21:28 +05:30
alias = alias,
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
function Meta(meta)
local elem = meta["pleroma-url"]
-- print(elem)
if elem == nil then
return -- abort
end
2024-07-07 13:52:00 +05:30
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)
return meta
2024-07-07 12:21:28 +05:30
end