mod_ogp: Add the ability to detect and process multiple URLs in a body
authorJC Brand <jc@opkode.com>
Tue, 02 Mar 2021 12:04:14 +0100
changeset 4486 21698b960bd6
parent 4485 e453eaf1589e
child 4487 c4f11a4b5ac7
mod_ogp: Add the ability to detect and process multiple URLs in a body
mod_ogp/mod_ogp.lua
--- 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)