pleroma-comments/pleroma-comments.lua

168 lines
4.1 KiB
Lua
Raw Normal View History

2024-07-06 23:51:28 -07:00
-- can't resolve seggfault on luasec, luasocket'
-- local https = "wget"
local json = require("cjson")
2024-11-26 03:06:49 -08:00
-- for testing
function printTable(t)
for key, value in pairs(t) do
print(key, value)
end
end
2024-07-06 23:51:28 -07:00
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 01:31:03 -07:00
function get_epoch_time(timestamp)
2024-07-06 23:51:28 -07:00
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 01:31:03 -07:00
return epoch
end
function get_short_date(timestamp)
return os.date(
"%a, %B %d, %Y", get_epoch_time(timestamp)
)
2024-07-06 23:51:28 -07:00
end
2024-07-07 01:22:00 -07:00
function write_comments(pleroma_posts, instance)
if #pleroma_posts == 0 then
return ""
end
2024-07-06 23:51:28 -07:00
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-06 23:51:28 -07:00
]]
local comments = {}
2024-07-07 02:07:13 -07:00
local replies = pleroma_posts-- ["descendants"]
for i, post in ipairs(replies) do
2024-07-07 01:22:00 -07:00
local pid = post["id"]
2024-07-07 01:31:03 -07:00
local datetime = get_short_date(post["created_at"])
2024-07-07 01:22:00 -07:00
local uid = post["account"]["id"]
local alias = post["account"]["display_name"]
local handle = post["account"]["acct"]
2024-11-26 03:06:49 -08:00
local text = post["content"]
2024-07-06 23:51:28 -07:00
local interpolated = template:gsub("%$(%w+)%$", {
i= #replies - i + 1,
2024-07-07 01:22:00 -07:00
host=instance,
pid=pid,
2024-07-06 23:51:28 -07:00
datetime=datetime,
2024-07-07 01:22:00 -07:00
uid = uid,
handle = handle,
alias=alias,
2024-07-06 23:51:28 -07:00
text = text
})
2024-07-07 01:22:00 -07:00
-- print(interpolated)
table.insert(
comments, pandoc.RawBlock("html", interpolated)
)
2024-07-06 23:51:28 -07:00
end
2024-07-07 01:22:00 -07:00
-- print(comments)
2024-07-06 23:51:28 -07:00
return comments
end
2024-07-07 02:07:13 -07:00
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-06 23:51:28 -07:00
2024-11-26 03:06:49 -08:00
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
2024-07-06 23:51:28 -07:00
function Meta(meta)
2024-11-26 03:06:49 -08:00
local pleroma_urls = meta["pleroma-urls"]
2024-07-07 02:07:13 -07:00
-- print(elem)
2024-11-26 03:06:49 -08:00
if pleroma_urls == nil then
2024-07-06 23:51:28 -07:00
return -- abort
end
2024-07-07 02:07:13 -07:00
local all_replies = {}
2024-11-26 03:06:49 -08:00
local hrefs = {}
2024-07-07 02:07:13 -07:00
local host = ""
2024-11-26 03:06:49 -08:00
-- 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}
)
2024-11-26 03:06:49 -08:00
local replies = get_replies(host, id)
2024-07-07 02:07:13 -07:00
if #all_replies == 0 then
2024-11-26 03:06:49 -08:00
all_replies = replies
2024-07-07 02:07:13 -07:00
else
2024-11-26 03:06:49 -08:00
combine_tables(all_replies, replies)
2024-07-07 02:07:13 -07:00
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 02:07:13 -07:00
end
)
local c = write_comments(all_replies, "https://" .. host)
meta["pleroma-comments"] = c
meta["pleroma-comments-count"] = #c
meta["pleroma-has-comments"] = (#c > 0)
2024-11-26 03:06:49 -08:00
meta["pleroma"] = hrefs
2024-07-07 01:22:00 -07:00
return meta
2024-07-06 23:51:28 -07:00
end