diff mod_ogp/mod_ogp.lua @ 4459:dbfda7f5522d

mod_ogp: Make sure OGP fasten messages get archived - By adding a type of "groupchat" - By adding a hook for `muc-message-is-historic`
author JC Brand <jc@opkode.com>
date Mon, 22 Feb 2021 17:37:50 +0100
parents 38da10e4b593
children 21698b960bd6
line wrap: on
line diff
--- a/mod_ogp/mod_ogp.lua	Mon Feb 22 16:44:40 2021 +0100
+++ b/mod_ogp/mod_ogp.lua	Mon Feb 22 17:37:50 2021 +0100
@@ -1,10 +1,9 @@
 local mod_muc = module:depends("muc")
 local http = require "net.http"
 local st = require "util.stanza"
-
-local ogp_pattern = [[<meta property=["']?(og:.-)["']? content=%s*["']?(.-)["']?%s-/?>]]
-local ogp_pattern2 = [[<meta content=%s*["']?(.-)["']? property=["']?(og:.-)["']?%s-/?>]]
 local url_pattern = [[https?://%S+]]
+local xmlns_fasten = "urn:xmpp:fasten:0";
+local xmlns_xhtml = "http://www.w3.org/1999/xhtml";
 
 local function ogp_handler(event)
 	local room, stanza = event.room, st.clone(event.stanza)
@@ -27,7 +26,7 @@
 
 			local to = room.jid
 			local from = room and room.jid or module.host
-			local fastening = st.message({to = to, from = from}):tag("apply-to", {xmlns = "urn:xmpp:fasten:0", id = origin_id})
+			local fastening = st.message({to = to, from = from, type = 'groupchat'}):tag("apply-to", {xmlns = xmlns_fasten, id = origin_id})
 			local found_metadata = false
 			local message_body = ""
 
@@ -54,7 +53,7 @@
 					fastening:tag(
 						"meta",
 						{
-							xmlns = "http://www.w3.org/1999/xhtml",
+							xmlns = xmlns_xhtml,
 							property = property,
 							content = content
 						}
@@ -64,7 +63,6 @@
 				end
 			end
 
-
 			if found_metadata then
 				mod_muc.get_room_from_jid(room.jid):broadcast_message(fastening)
 			end
@@ -74,3 +72,11 @@
 end
 
 module:hook("muc-occupant-groupchat", ogp_handler)
+
+
+module:hook("muc-message-is-historic", function (event)
+	local fastening = event.stanza:get_child('apply-to', xmlns_fasten)
+	if fastening and fastening:get_child('meta', xmlns_xhtml) then
+		return true
+	end
+end);