diff mod_muc_markers/mod_muc_markers.lua @ 4026:e3964f876b5d

mod_muc_markers: Broadcast current markers on join This lets you see how far others have read upon joining without cluttering the archives with obsolete chat markers.
author Kim Alvefur <zash@zash.se>
date Tue, 05 May 2020 21:52:14 +0200
parents 57b4cdeba318
children 787fc3030087
line wrap: on
line diff
--- a/mod_muc_markers/mod_muc_markers.lua	Tue May 05 21:50:35 2020 +0200
+++ b/mod_muc_markers/mod_muc_markers.lua	Tue May 05 21:52:14 2020 +0200
@@ -8,6 +8,8 @@
 -- Notably Conversations will ack the origin-id instead. We need to update the XEP to
 -- clarify the correct behaviour.
 
+local st = require "util.stanza";
+
 local xmlns_markers = "urn:xmpp:chat-markers:0";
 
 local marker_element_name = module:get_option_string("muc_marker_type", "displayed");
@@ -55,6 +57,35 @@
 	end
 end);
 
+local function find_nickname(room, user_jid)
+	-- Find their current nickname
+	for nick, occupant in pairs(room._occupants) do
+		if occupant.bare_jid == user_jid then
+			return nick;
+		end
+	end
+	-- Or if they're not here
+	local nickname = room:get_affiliation_data(user_jid, "reserved_nickname");
+	if nickname then return room.jid.."/"..nickname; end
+end
+
+-- Synthesize markers
+if muc_marker_map_store.get_all then
+module:hook("muc-occupant-session-new", function (event)
+	local room, to = event.room, event.stanza.attr.from;
+	local markers = muc_marker_map_store:get_all(room.jid);
+	if not markers then return end
+	for user_jid, id in pairs(markers) do
+		local room_nick = find_nickname(room, user_jid);
+		if room_nick then
+			local recv_marker = st.message({ type = "groupchat", from = room_nick, to = to })
+				:tag("received", { xmlns = xmlns_markers, id = id });
+			room:route_stanza(recv_marker);
+		end
+	end
+end);
+end
+
 -- Public API
 
 function get_user_read_marker(user_jid, room_jid)