pleroma-comments.lua 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. local json = require("cjson")
  2. -- GLOBAL VARIABLES
  3. -- & meant to be assigned in config.lua
  4. --------------------
  5. -- location of downloaded images
  6. pleroma_avatar_save_path = nil
  7. -- prepended to image file, written on page
  8. pleroma_avatar_path = nil
  9. -- instances may require auth to access api
  10. pleroma_auth = {}
  11. -- pleroma_auth["instance.tld"] = "your_auth_key"
  12. pcall(
  13. function ()
  14. dofile("config.lua")
  15. end
  16. )
  17. --[[
  18. if `pleroma_avatar_*` are still `nil` after `dofile()` then the script will look for it in the yaml header.
  19. if they are still nil afterwards, then the script
  20. will hotlink.
  21. ]]--
  22. -- for testing
  23. function printTable(t)
  24. if t then
  25. for key, value in pairs(t) do
  26. print(key, value)
  27. end
  28. end
  29. end
  30. function add_unique(list, item)
  31. if type(list) ~= "table" or item == nil then
  32. -- print("item "..item)
  33. return false
  34. end
  35. for _, value in ipairs(list) do
  36. if value == item then
  37. return false
  38. end
  39. end
  40. table.insert(list, item)
  41. -- print("added "..item)
  42. return true
  43. end
  44. function tokenizeString(inputString, delimiter)
  45. local tokens = {}
  46. for token in inputString:gmatch("[^" .. delimiter .. "]+") do
  47. table.insert(tokens, token)
  48. end
  49. return tokens
  50. end
  51. function get(link, filename, auth)
  52. print("http/s GET: ".. link)
  53. local filename = filename or nil
  54. local auth = auth or nil
  55. local args = {}
  56. if filename then -- when requesting avatars
  57. args = {
  58. "--timeout=10",
  59. "-qO",
  60. filename,
  61. link
  62. }
  63. else -- when requesting json
  64. args = {
  65. "-qO-",
  66. "--timeout=10",
  67. link
  68. }
  69. end
  70. -- don't use auth bearer for downloading images'
  71. -- its either not needed OR
  72. -- there should be an extra check on the host
  73. if auth and not filename then
  74. local h = "--header=\"Authorization: Bearer %s\" "
  75. table.insert(
  76. args, 1, string.format(h, auth)
  77. )
  78. end
  79. local command = "wget " .. table.concat(args, ' ')
  80. print(command)
  81. local success, retval = pcall(
  82. function ()
  83. local handle = io.popen(command)
  84. local result = handle:read("*a")
  85. handle:close()
  86. return result
  87. end
  88. )
  89. if not success then
  90. print("warning: error while performing http/s GET")
  91. print("\treturned: " .. tostring(retval))
  92. end
  93. return retval
  94. end
  95. function get_epoch_time(timestamp)
  96. local pattern = "(%d+)-(%d+)-(%d+)T(%d+):(%d+):(%d+).000Z"
  97. local year, month, day, hour, min, sec = timestamp:match(pattern)
  98. local epoch = os.time({
  99. year = year,
  100. month = month,
  101. day = day,
  102. hour = hour,
  103. min = min,
  104. sec = sec
  105. })
  106. return epoch
  107. end
  108. function get_short_date(timestamp)
  109. return os.date(
  110. "%a, %B %d, %Y", get_epoch_time(timestamp)
  111. )
  112. end
  113. function write_comments(pleroma_posts, instance, avatar_path)
  114. local avatar_mode = 1
  115. if avatar_path then
  116. avatar_mode = 2
  117. end
  118. -- img mode : 0 = omit; 1 = display (hotlink); 2 = download
  119. function get_user(acct_data, instance, img_mode, folder)
  120. -- specify path to store avatars
  121. -- should be empty string if img_mode != 2
  122. local folder = folder or ""
  123. -- related to output
  124. local user_info = "" -- template
  125. local result = ""
  126. local vars = {
  127. alias = acct_data["display_name"],
  128. uid = acct_data["id"],
  129. handle = acct_data["acct"],
  130. host=instance
  131. }
  132. local filename = nil
  133. local avatar_url = acct_data["avatar_static"]
  134. if img_mode then
  135. user_info = [[
  136. <figure>
  137. <img src="$avatar$" loading="lazy" alt="avatar"/>
  138. <figcaption>$alias$ <a href="$host$/users/$uid$">@$handle$</a> </figcaption>
  139. </figure>
  140. ]]
  141. if img_mode == 1 then
  142. vars.avatar = acct_data["avatar_static"]
  143. else
  144. -- save to file such as user_at_instance_tld.png
  145. -- omit query e.g ?name="foo" - get the extension only
  146. local extension = (avatar_url:match("([^?]+)")):match("^.+(%..+)$")
  147. -- replace '@'s and '.'
  148. local name = (acct_data["acct"]:gsub("@", "_at_")):gsub("%.", "_")
  149. filename = name .. extension
  150. vars.avatar = folder .. filename
  151. end
  152. result = user_info:gsub("%$(%w+)%$", vars)
  153. else
  154. user_info = "<p>$a`lias$ <a href=\"$host$/users/$uid$\">@$handle$</a></p>"
  155. result = user_info:gsub("%$(%w+)%$", vars)
  156. end
  157. -- print("vars: " .. vars.avatar)
  158. return result, filename, avatar_url
  159. end
  160. function get_card(card, instance)
  161. if card == nil or type(card) ~= "table" then
  162. return ""
  163. end
  164. if card["provider_url"] == instance then
  165. -- skip rendering a card
  166. return ""
  167. end
  168. -- print(type(card))
  169. local card_template = [[
  170. <article class="card">
  171. <header>
  172. <h1 class="card-title">$title$</h1>
  173. <p class="card-description">$description$</p>
  174. </header>
  175. <!-- <img src="$image$" alt="$image_description$" class="card-image" loading="lazy"/> -->
  176. <footer>
  177. <a href="$link$" class="card-link">Read More</a>
  178. </footer>
  179. </article>
  180. ]]
  181. local vars = {
  182. title = card["title"],
  183. description = card["description"],
  184. image = card["image"],
  185. image_description=card["image_description"],
  186. link = card["url"]
  187. }
  188. return card_template:gsub("%$(%w+)%$", vars)
  189. end
  190. function get_media(attachments)
  191. if type(attachments) ~= "table" then
  192. return ""
  193. end
  194. if #attachments < 1 then
  195. return ""
  196. end
  197. local media_list = {"<p>media attached: </p><ol>"}
  198. local item = "<li><a href=\"$link$\">$mime$</a></li>"
  199. for _, v in pairs(attachments) do
  200. local vars = {
  201. link = v["preview_url"],
  202. mime = v["pleroma"]["mime_type"]
  203. }
  204. local foo = item:gsub("%$(%w+)%$", vars)
  205. -- print(foo)
  206. table.insert(media_list, foo)
  207. end
  208. table.insert(media_list, "</ol>")
  209. return table.concat(media_list, "\n")
  210. end
  211. function get_poll(poll)
  212. if type(poll) ~= "table" then
  213. return ""
  214. end
  215. local bar_chart = {"<div class=\"chart\">"}
  216. local bar_template = [[
  217. <div class="bar-container">
  218. <div class="bar" style="width: $pct$%;">
  219. <span>$pct$%</span>
  220. </div>
  221. <div class="bar-text">
  222. $label$
  223. </div>
  224. </div>
  225. ]]
  226. local total_votes = math.floor(poll["votes_count"])
  227. local total_voters = math.floor(poll["voters_count"])
  228. for _, v in pairs(poll["options"]) do
  229. local percentage = (v["votes_count"]/total_votes) * 100
  230. local rounded = math.floor(0.5 + percentage)
  231. local vars = {
  232. label = v["title"],
  233. pct = rounded
  234. }
  235. local bar = bar_template:gsub("%$(%w+)%$", vars)
  236. table.insert(bar_chart, bar)
  237. end
  238. -- close chart div
  239. table.insert(bar_chart, "</div>")
  240. local summary = "<p>$x$ people have cast $y$ votes</p>"
  241. local foo = summary:gsub(
  242. "%$(%w+)%$",
  243. {x=total_voters, y=total_votes}
  244. )
  245. table.insert(bar_chart, foo)
  246. return table.concat(bar_chart,"\n")
  247. end
  248. if #pleroma_posts == 0 then
  249. return ""
  250. end
  251. local template = [[
  252. <article class="pleroma-comment" id="pleroma-comment$i$">
  253. <h3>
  254. #$i$ <a href="$host$/notice/$pid$">$datetime$</a>
  255. </h3>
  256. $user$
  257. <blockquote>
  258. $text$
  259. </blockquote>
  260. $card$
  261. $attributes$
  262. </article>
  263. ]]
  264. local comments = {}
  265. local replies = pleroma_posts-- ["descendants"]
  266. local links = {}
  267. local images = {}
  268. for i, post in ipairs(replies) do
  269. local pid = post["id"]
  270. local datetime = get_short_date(post["created_at"])
  271. local text = post["content"]
  272. local attrs = {}
  273. table.insert(
  274. attrs, get_media(post["media_attachments"])
  275. )
  276. table.insert(attrs, get_poll(post["poll"]))
  277. local user, img_file, img_url = get_user(
  278. post["account"], instance, avatar_mode, avatar_path)
  279. add_unique(images, img_file)
  280. add_unique(links, img_url)
  281. local interpolated = template:gsub("%$(%w+)%$", {
  282. i= #replies - i + 1,
  283. host=instance,
  284. pid=pid,
  285. datetime=datetime,
  286. user=user,
  287. text = text,
  288. card = get_card(post["card"], instance),
  289. attributes = table.concat(attrs)
  290. })
  291. -- print(interpolated)
  292. table.insert(
  293. comments, pandoc.RawBlock("html", interpolated)
  294. )
  295. end
  296. -- printTable(dl_list)
  297. return comments, images, links
  298. end
  299. function combine_tables(a,b)
  300. -- iterate through b, add to a
  301. for i=1,#b do
  302. table.insert(a, b[i])
  303. end
  304. return a
  305. end
  306. function get_url_from_pandoc_str(pandoc_str)
  307. local str = pandoc.utils.stringify(pandoc_str)
  308. -- 1 = protocol, 2 = host ...
  309. -- https://host.tld/notice/12345
  310. local tokens = tokenizeString(str, '/')
  311. local id = tokens[#tokens]
  312. local host = tokens[2]
  313. local id = tokens[#tokens]
  314. local link = str
  315. return link, host, id
  316. end
  317. function get_status(host, post_id, auth)
  318. local url = "https://" .. host .. "/api/v1/statuses/" .. post_id
  319. local success, retval = pcall(
  320. function ()
  321. local got = get(url, nil, auth)
  322. -- print(got)
  323. return json.decode(got)
  324. end
  325. )
  326. -- if an error occurred in retrieving a status
  327. -- it will cause errors in write_comments
  328. --consider skipping over statuses later
  329. assert(success)
  330. assert(not retval["error"])
  331. return retval
  332. end
  333. function get_replies(host, id, auth)
  334. local url = "https://" .. host .. "/api/v1/statuses/" .. id .. "/context"
  335. -- print(url)
  336. local got = json.decode(get(url, nil, auth))
  337. return got["descendants"]
  338. end
  339. function get_images(filenames, urls, folder)
  340. if not folder then
  341. folder = ""
  342. end
  343. if not filenames or not urls then
  344. return
  345. end
  346. if #filenames ~= #urls then
  347. return
  348. end
  349. for i = 1, #urls, 1 do
  350. -- still possible to have a ilst of nil (file names)
  351. if not filenames[i] then
  352. break
  353. end
  354. get(urls[i], folder .. filenames[i])
  355. end
  356. end
  357. function Meta(meta)
  358. local pleroma_urls = meta["pleroma-urls"]
  359. if pleroma_urls == nil then
  360. return -- abort
  361. end
  362. -- if both are defined, then do not hotlink avatars
  363. if not pleroma_avatar_save_path then
  364. pleroma_avatar_save_path = meta["pleroma-avatar-save-path"]
  365. end
  366. if not pleroma_avatar_path then
  367. pleroma_avatar_path = meta["pleroma-avatar-path"]
  368. end
  369. -- most servers appear to serve hotilnked avatars however
  370. -- images will be missing in case of downtime or shutdown
  371. -- OR a user has changed their avatar and the old avatar gets deleted
  372. -- var currently unused
  373. -- local is_hotlink = true
  374. -- if pleroma_avatar_save_path and pleroma_avatar_path then
  375. -- is_hotlink = false
  376. -- end
  377. local all_replies = {}
  378. local hrefs = {}
  379. local host = ""
  380. -- for each listed url in "pleroma-urls"
  381. for _, v in pairs(pleroma_urls) do
  382. local link, domain, id = get_url_from_pandoc_str(v)
  383. host = domain
  384. -- list of links people can reply using
  385. local reply_href = link
  386. if type(pleroma_reply_href) == "string" then
  387. local temp = "https://%s%s%s"
  388. reply_href = string.format(
  389. temp, host, pleroma_reply_href, id)
  390. end
  391. table.insert(hrefs,
  392. {link = reply_href, id = id}
  393. )
  394. local auth_key = nil
  395. if pleroma_auth[host] then
  396. auth_key = pleroma_auth[host]
  397. end
  398. local op = get_status(host, id, auth_key)
  399. table.insert(all_replies, op)
  400. local replies = get_replies(host, id, auth_key)
  401. combine_tables(all_replies, replies)
  402. end
  403. table.sort(all_replies,
  404. function(a, b)
  405. local ta = get_epoch_time(a["created_at"])
  406. local tb = get_epoch_time(b["created_at"])
  407. return ta > tb
  408. end
  409. )
  410. -- returns comments, images, links (img urls)
  411. local c, i, l = write_comments(
  412. all_replies,
  413. "https://" .. host,
  414. pleroma_avatar_path
  415. )
  416. get_images(i, l, pleroma_avatar_save_path)
  417. meta["pleroma-comments"] = c
  418. meta["pleroma-comments-count"] = #c
  419. meta["pleroma-has-comments"] = (#c > 0)
  420. meta["pleroma"] = hrefs
  421. return meta
  422. end