pleroma-comments.lua 12 KB

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