mirror of
https://notabug.org/scuti/pleroma-comments
synced 2025-01-24 03:51:43 +05:30
working - retrieves avatars - needs folder paths
This commit is contained in:
parent
c4fb5e1b7b
commit
3efcb65455
@ -5,11 +5,28 @@ local json = require("cjson")
|
||||
|
||||
-- for testing
|
||||
function printTable(t)
|
||||
for key, value in pairs(t) do
|
||||
print(key, value)
|
||||
if t then
|
||||
for key, value in pairs(t) do
|
||||
print(key, value)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function add_unique(list, item)
|
||||
if type(list) ~= "table" or item == nil then
|
||||
-- print("item "..item)
|
||||
return false
|
||||
end
|
||||
for _, value in ipairs(list) do
|
||||
if value == item then
|
||||
return false
|
||||
end
|
||||
end
|
||||
table.insert(list, item)
|
||||
-- print("added "..item)
|
||||
return true
|
||||
end
|
||||
|
||||
function tokenizeString(inputString, delimiter)
|
||||
local tokens = {}
|
||||
for token in inputString:gmatch("[^" .. delimiter .. "]+") do
|
||||
@ -18,15 +35,35 @@ function tokenizeString(inputString, delimiter)
|
||||
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
|
||||
function get(link, filename)
|
||||
print(link)
|
||||
local filename = filename or nil
|
||||
local args = {}
|
||||
if filename then
|
||||
args = {
|
||||
"-qO",
|
||||
filename,
|
||||
link
|
||||
}
|
||||
else
|
||||
args = {
|
||||
"-qO-",
|
||||
link
|
||||
}
|
||||
end
|
||||
|
||||
local command = "wget " .. table.concat(args, ' ')
|
||||
local success, retval = pcall(
|
||||
function ()
|
||||
-- print("!: ".. table.concat(args))
|
||||
return pandoc.pipe("wget", args, "")
|
||||
end
|
||||
)
|
||||
if not success then
|
||||
print("warning: error while performing http/s GET")
|
||||
print(retval)
|
||||
end
|
||||
return retval
|
||||
end
|
||||
|
||||
function get_epoch_time(timestamp)
|
||||
@ -52,30 +89,48 @@ end
|
||||
function write_comments(pleroma_posts, instance, show_avatars)
|
||||
show_avatars = show_avatars or false
|
||||
|
||||
function get_user(acct_data, instance, include_img)
|
||||
-- user data
|
||||
local user_info = ""
|
||||
-- img mode : 0 = omit; 1 = display (hotlink); 2 = download
|
||||
function get_user(acct_data, instance, img_mode, folder)
|
||||
-- specify path to store avatars
|
||||
-- should be empty string if img_mode != 2
|
||||
local folder = folderh or ""
|
||||
|
||||
-- related to output
|
||||
local user_info = "" -- template
|
||||
local result = ""
|
||||
|
||||
local vars = {
|
||||
alias = acct_data["display_name"],
|
||||
uid = acct_data["id"],
|
||||
handle = acct_data["acct"],
|
||||
host=instance
|
||||
}
|
||||
if include_img then
|
||||
local avatar_url = acct_data["avatar_static"]
|
||||
if img_mode then
|
||||
user_info = [[
|
||||
<figure>
|
||||
<img src="$avatar$" loading="lazy" alt="avatar"/>
|
||||
<figcaption>$alias$ <a href="$host$/users/$uid$">@$handle$</a> </figcaption>
|
||||
</figure>
|
||||
]]
|
||||
vars.avatar = acct_data["avatar_static"]
|
||||
if img_mode == 1 then
|
||||
vars.avatar = acct_data["avatar_static"]
|
||||
else
|
||||
-- save to file such as user_at_instance_tld.png
|
||||
-- omit query e.g ?name="foo" - get the extension only
|
||||
local extension = (avatar_url:match("([^?]+)")):match("^.+(%..+)$")
|
||||
-- replace '@'s and '.'
|
||||
local name = (acct_data["acct"]:gsub("@", "_at_")):gsub("%.", "_")
|
||||
local filename = folder .. name .. extension
|
||||
vars.avatar = filename
|
||||
end
|
||||
result = user_info:gsub("%$(%w+)%$", vars)
|
||||
else
|
||||
user_info = "<p>$alias$ <a href=\"$host$/users/$uid$\">@$handle$</a></p>"
|
||||
user_info = "<p>$a`lias$ <a href=\"$host$/users/$uid$\">@$handle$</a></p>"
|
||||
result = user_info:gsub("%$(%w+)%$", vars)
|
||||
end
|
||||
return result
|
||||
-- print("vars: " .. vars.avatar)
|
||||
return result, vars.avatar, avatar_url
|
||||
end
|
||||
|
||||
function get_card(card, instance)
|
||||
@ -124,7 +179,7 @@ function write_comments(pleroma_posts, instance, show_avatars)
|
||||
mime = v["pleroma"]["mime_type"]
|
||||
}
|
||||
local foo = item:gsub("%$(%w+)%$", vars)
|
||||
print(foo)
|
||||
-- print(foo)
|
||||
table.insert(media_list, foo)
|
||||
end
|
||||
table.insert(media_list, "</ol>")
|
||||
@ -188,6 +243,10 @@ function write_comments(pleroma_posts, instance, show_avatars)
|
||||
]]
|
||||
local comments = {}
|
||||
local replies = pleroma_posts-- ["descendants"]
|
||||
|
||||
local links = {}
|
||||
local images = {}
|
||||
|
||||
for i, post in ipairs(replies) do
|
||||
local pid = post["id"]
|
||||
local datetime = get_short_date(post["created_at"])
|
||||
@ -199,12 +258,17 @@ function write_comments(pleroma_posts, instance, show_avatars)
|
||||
)
|
||||
table.insert(attrs, get_poll(post["poll"]))
|
||||
|
||||
local user, img_file, img_url = get_user(
|
||||
post["account"], instance, 2, "images/avatars/")
|
||||
add_unique(images, img_file)
|
||||
add_unique(links, img_url)
|
||||
|
||||
local interpolated = template:gsub("%$(%w+)%$", {
|
||||
i= #replies - i + 1,
|
||||
host=instance,
|
||||
pid=pid,
|
||||
datetime=datetime,
|
||||
user=get_user(post["account"], instance, true),
|
||||
user=user,
|
||||
text = text,
|
||||
card = get_card(post["card"], instance),
|
||||
attributes = table.concat(attrs)
|
||||
@ -214,8 +278,8 @@ function write_comments(pleroma_posts, instance, show_avatars)
|
||||
comments, pandoc.RawBlock("html", interpolated)
|
||||
)
|
||||
end
|
||||
-- print(comments)
|
||||
return comments
|
||||
-- printTable(dl_list)
|
||||
return comments, images, links
|
||||
end
|
||||
|
||||
function combine_tables(a,b)
|
||||
@ -242,17 +306,29 @@ end
|
||||
|
||||
function get_status(host, post_id)
|
||||
local url = "https://" .. host .. "/api/v1/statuses/" .. post_id
|
||||
print(url)
|
||||
return get(url)
|
||||
-- print(url)
|
||||
return json.decode(get(url))
|
||||
end
|
||||
|
||||
function get_replies(host, id)
|
||||
local url = "https://" .. host .. "/api/v1/statuses/" .. id .. "/context"
|
||||
print(url)
|
||||
local got = get(url)
|
||||
-- print(url)
|
||||
local got = json.decode(get(url))
|
||||
return got["descendants"]
|
||||
end
|
||||
|
||||
function get_images(filenames, urls)
|
||||
if not filenames or not urls then
|
||||
return
|
||||
end
|
||||
if #filenames ~= #urls then
|
||||
return
|
||||
end
|
||||
for i = 1, #urls, 1 do
|
||||
get(urls[i], filenames[i])
|
||||
end
|
||||
end
|
||||
|
||||
function Meta(meta)
|
||||
local pleroma_urls = meta["pleroma-urls"]
|
||||
-- print(elem)
|
||||
@ -281,7 +357,10 @@ function Meta(meta)
|
||||
return ta > tb
|
||||
end
|
||||
)
|
||||
local c = write_comments(all_replies, "https://" .. host)
|
||||
local c, i, l = write_comments(all_replies, "https://" .. host)
|
||||
printTable(i)
|
||||
printTable(l)
|
||||
get_images(i, l)
|
||||
meta["pleroma-comments"] = c
|
||||
meta["pleroma-comments-count"] = #c
|
||||
meta["pleroma-has-comments"] = (#c > 0)
|
||||
|
Loading…
x
Reference in New Issue
Block a user