File

mod_muc_markers/mod_muc_markers.lua @ 4025:57b4cdeba318

mod_muc_markers: Prevent storage instead of broadcast Restores the ability to see how far others have read. Maybe this should be a setting?
author Kim Alvefur <zash@zash.se>
date Tue, 05 May 2020 21:50:35 +0200
parent 4024:95882b487ed2
child 4026:e3964f876b5d
line wrap: on
line source

-- Track messages received by users of the MUC

-- We rewrite the 'id' attribute of outgoing stanzas to match the stanza (archive) id
-- This module is therefore incompatible with the muc#stable_id feature
-- We rewrite the id because XEP-0333 doesn't tell clients explicitly which id to use
-- in marker reports. However it implies the 'id' attribute through examples, and this
-- is what some clients implement.
-- Notably Conversations will ack the origin-id instead. We need to update the XEP to
-- clarify the correct behaviour.

local xmlns_markers = "urn:xmpp:chat-markers:0";

local marker_element_name = module:get_option_string("muc_marker_type", "displayed");

local muc_marker_map_store = module:open_store("muc_markers", "map");

local function get_stanza_id(stanza, by_jid)
	for tag in stanza:childtags("stanza-id", "urn:xmpp:sid:0") do
		if tag.attr.by == by_jid then
			return tag.attr.id;
		end
	end
	return nil;
end

module:hook("muc-broadcast-message", function (event)
	local stanza = event.stanza;

	local archive_id = get_stanza_id(stanza, event.room.jid);
	-- We are not interested in stanzas that didn't get archived
	if not archive_id then return; end

	-- Add stanza id as id attribute
	stanza.attr.id = archive_id;
	-- Add markable element to request markers from clients
	stanza:tag("markable", { xmlns = xmlns_markers }):up();
end, -1);

module:hook("muc-occupant-groupchat", function (event)
	local marker = event.stanza:get_child(marker_element_name, xmlns_markers);
	if not marker then return; end

	-- Store the id that the user has received to
	module:log("warn", "New marker for %s: %s", event.occupant.bare_jid, marker.attr.id);
	muc_marker_map_store:set(event.occupant.bare_jid, event.room.jid, marker.attr.id);

end);

module:hook("muc-message-is-historic", function (event)
	local marker = event.stanza:get_child("received", xmlns_markers);

	-- Prevent stanza from reaching the archive (it's just noise)
	if marker then
		return false
	end
end);

-- Public API

function get_user_read_marker(user_jid, room_jid)
	return muc_marker_map_store:get(user_jid, room_jid);
end