Diff

mod_ogp/mod_ogp.lua @ 4482:21698b960bd6

mod_ogp: Add the ability to detect and process multiple URLs in a body
author JC Brand <jc@opkode.com>
date Tue, 02 Mar 2021 12:04:14 +0100
parent 4459:dbfda7f5522d
child 4483:c4f11a4b5ac7
line wrap: on
line diff
--- a/mod_ogp/mod_ogp.lua	Mon Mar 01 17:33:32 2021 +0100
+++ b/mod_ogp/mod_ogp.lua	Tue Mar 02 12:04:14 2021 +0100
@@ -5,17 +5,10 @@
 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)
-	local body = stanza:get_child_text("body")
-	if not body then return; end
 
-	local url = body:match(url_pattern)
+local function fetch_ogp_data(room, url, origin_id)
 	if not url then return; end
 
-	local origin_id = stanza:find("{urn:xmpp:sid:0}origin-id@id")
-	if not origin_id then return; end
-
 	http.request(
 		url,
 		nil,
@@ -71,6 +64,20 @@
 	)
 end
 
+local function ogp_handler(event)
+	local room, stanza = event.room, st.clone(event.stanza)
+	local body = stanza:get_child_text("body")
+
+	if not body then return; end
+
+	local origin_id = stanza:find("{urn:xmpp:sid:0}origin-id@id")
+	if not origin_id then return; end
+
+	for url in body:gmatch(url_pattern) do
+		fetch_ogp_data(room, url, origin_id);
+	end
+end
+
 module:hook("muc-occupant-groupchat", ogp_handler)