mirror of
https://notabug.org/scuti/pleroma-comments
synced 2024-12-22 19:09:45 +05:30
124 lines
3.1 KiB
Lua
124 lines
3.1 KiB
Lua
|
|
-- 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 get_epoch_time(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 epoch
|
|
end
|
|
|
|
function get_short_date(timestamp)
|
|
return os.date(
|
|
"%a, %B %d, %Y", get_epoch_time(timestamp)
|
|
)
|
|
end
|
|
|
|
function write_comments(pleroma_posts, instance)
|
|
local template = [[
|
|
<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>
|
|
]]
|
|
local comments = {}
|
|
local replies = pleroma_posts-- ["descendants"]
|
|
for _, post in ipairs(replies) do
|
|
local pid = post["id"]
|
|
local datetime = get_short_date(post["created_at"])
|
|
local uid = post["account"]["id"]
|
|
local alias = post["account"]["acct"]
|
|
local text = post["pleroma"]["content"]["text/plain"]
|
|
local interpolated = template:gsub("%$(%w+)%$", {
|
|
host=instance,
|
|
pid=pid,
|
|
datetime=datetime,
|
|
uid = uid,
|
|
alias = alias,
|
|
text = text
|
|
})
|
|
-- print(interpolated)
|
|
table.insert(
|
|
comments, pandoc.RawBlock("html", interpolated)
|
|
)
|
|
end
|
|
-- print(comments)
|
|
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)
|
|
if elem == nil then
|
|
return -- abort
|
|
end
|
|
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
|
|
|
|
|