Changeset

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
parents 4481:e453eaf1589e
children 4483:c4f11a4b5ac7
files mod_ogp/mod_ogp.lua
diffstat 1 files changed, 15 insertions(+), 8 deletions(-) [+]
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)