$title$
+$description$
+diff --git a/pleroma-comments.css b/pleroma-comments.css new file mode 100644 index 0000000..f1624de --- /dev/null +++ b/pleroma-comments.css @@ -0,0 +1,107 @@ + +/* colors */ + +/*.pleroma-comment blockquote { + border-left: 3px solid red; +}*/ + +/*.pleroma-comment .bar { + background-color: green; +}*/ + +/*.pleroma-comment .card-link { + background-color: blue; + color: white; +}*/ + +/*.pleroma-comment .card-link:hover { + background-color: orange; +}*/ + +.pleroma-comment blockquote { + padding-left:1%; +} + +.pleroma-comment h3 > a { + text-decoration:none; +} + +.pleroma-comment > figure { + display:flex; + align-items:center; +} +.pleroma-comment > figure img { + height:5em; + margin-right: 1em; +} + +/* meta information about post */ +.pleroma-comment p, .pleroma-comment .chart { + margin-left: 5%; +} + +/* usernames can either be figcaption or the first paragraph in the element */ + +.pleroma-comment + p, .pleroma-comment figcaption { + font-weight:bold; +} +.pleroma-comment p > a, .pleroma-comment figcaption > a { + font-weight:normal; + text-decoration:none; +} + +.pleroma-comment .card { + border: 1px solid #ccc; + border-radius: 8px; + overflow: hidden; + margin: 1%; + margin-left:10%; +/* adjust this */ + width: 300px; +} +.pleroma-comment .card-title { + font-size: 1.5em; + margin: 3%; +} +.pleroma-comment .card-description { + font-size: 1em; + margin:5%; +} +.pleroma-comment .card-image { + width: 100%; + height: auto; +} +.pleroma-comment .card-link { + display: block; + text-align: center; + padding: 10px; + text-decoration: none; + border-radius: 5px; + margin: 10px; +} + +.pleroma-comment .chart { +/* border:1px solid red; */ + display: flex; + flex-direction: column; +} + +@media only screen and (min-width:768px){ + .pleroma-comment .chart { + width: 45%; + } +} + +.pleroma-comment .bar-container { + align-items: center; + margin: 1% 0; +} + +.pleroma-comment .bar { + padding-top: 0.5em; + padding-bottom:0.5em; +} + +.pleroma-comment .bar-text { + ; +} diff --git a/pleroma-comments.lua b/pleroma-comments.lua index 2a4aba6..c71bddc 100644 --- a/pleroma-comments.lua +++ b/pleroma-comments.lua @@ -3,6 +3,7 @@ -- local https = "wget" local json = require("cjson") +-- for testing function printTable(t) for key, value in pairs(t) do print(key, value) @@ -48,7 +49,127 @@ function get_short_date(timestamp) ) end -function write_comments(pleroma_posts, instance) +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 = "" + local result = "" + local vars = { + alias = acct_data["display_name"], + uid = acct_data["id"], + handle = acct_data["acct"], + host=instance + } + if include_img then + user_info = [[ + + ]] + vars.avatar = acct_data["avatar_static"] + result = user_info:gsub("%$(%w+)%$", vars) + else + user_info = "
$alias$ @$handle$
" + result = user_info:gsub("%$(%w+)%$", vars) + end + return result + end + + function get_card(card, instance) + if card == nil or type(card) ~= "table" then + return "" + end + if card["provider_url"] == instance then + -- skip rendering a card + return "" + end +-- print(type(card)) + local card_template = [[ +$description$
+media attached:
$x$ people have cast $y$ votes
" + local foo = summary:gsub( + "%$(%w+)%$", + {x=total_voters, y=total_votes} + ) + table.insert(bar_chart, foo) + return table.concat(bar_chart,"\n") + end + if #pleroma_posts == 0 then return "" end @@ -57,10 +178,12 @@ function write_comments(pleroma_posts, instance)$alias$ @$handle$
+ $user$$text$+ $card$ + $attributes$ ]] local comments = {} @@ -68,19 +191,23 @@ function write_comments(pleroma_posts, instance) for i, 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"]["display_name"] - local handle = post["account"]["acct"] - local text = post["pleroma"]["content"]["text/plain"] + local text = post["content"] + + local attrs = {} + table.insert( + attrs, get_media(post["media_attachments"]) + ) + table.insert(attrs, get_poll(post["poll"])) + local interpolated = template:gsub("%$(%w+)%$", { i= #replies - i + 1, host=instance, pid=pid, datetime=datetime, - uid = uid, - handle = handle, - alias=alias, - text = text + user=get_user(post["account"], instance, true), + text = text, + card = get_card(post["card"], instance), + attributes = table.concat(attrs) }) -- print(interpolated) table.insert( @@ -99,33 +226,53 @@ function combine_tables(a,b) return a 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_status(host, post_id) + local url = "https://" .. host .. "/api/v1/statuses/" .. post_id + print(url) + return get(url) +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 + function Meta(meta) - local elem = meta["pleroma-urls"] + local pleroma_urls = meta["pleroma-urls"] -- print(elem) - if elem == nil then + if pleroma_urls == nil then return -- abort end local all_replies = {} - local newelem = {} + local hrefs = {} 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, '/') - local id = tokens[#tokens] - table.insert(newelem, - {link = str, id = id} + -- for each listed url in "pleroma-urls" + for _, v in pairs(pleroma_urls) do + local link, domain, id = get_url_from_pandoc_str(v) + host = domain + table.insert(hrefs, + {link = link, id = id} ) - 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 + local op = get_status(host, id) + table.insert(all_replies, op) + local replies = get_replies(host, id) + combine_tables(all_replies, replies) end table.sort(all_replies, function(a, b) @@ -138,7 +285,7 @@ function Meta(meta) meta["pleroma-comments"] = c meta["pleroma-comments-count"] = #c meta["pleroma-has-comments"] = (#c > 0) - meta["pleroma"] = newelem + meta["pleroma"] = hrefs return meta end