Changeset

3972:45c5603a6c07

mod_muc_markers: New module for server-side receipt tracking in MUCs
author Matthew Wild <mwild1@gmail.com>
date Mon, 13 Apr 2020 15:48:58 +0100
parents 3971:ae5ac41c391d
children 3973:df6227e288e5
files mod_muc_markers/README.markdown mod_muc_markers/mod_muc_markers.lua
diffstat 2 files changed, 94 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mod_muc_markers/README.markdown	Mon Apr 13 15:48:58 2020 +0100
@@ -0,0 +1,41 @@
+# Introduction
+
+This module adds an internal Prosody API to retrieve the last received message by MUC occupants.
+
+## Requirements
+
+The clients must support XEP-0333, and the users to be tracked must be affiliated with the room.
+
+Currently due to lack of clarity about which id to use in acknowledgements in XEP-0333, this module
+rewrites the id attribute of stanzas to match the stanza (archive) id assigned by the MUC server.
+
+Oh yeah, and mod_muc_mam is required (or another module that adds a stanza-id), otherwise this module
+won't do anything.
+
+# Configuring
+
+## Enabling
+
+``` {.lua}
+Component "rooms.example.net" "muc"
+modules_enabled = {
+    "muc_markers";
+    "muc_mam";
+}
+```
+
+## Settings
+
+There are no configuration options for this module.
+
+# Developers
+
+## Example usage
+
+```
+local muc_markers = module:depends("muc_markers");
+
+function something()
+	local last_received_id = muc_markers.get_user_read_marker("user@localhost", "room@conference.localhost");
+end
+```
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mod_muc_markers/mod_muc_markers.lua	Mon Apr 13 15:48:58 2020 +0100
@@ -0,0 +1,53 @@
+-- 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 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("received", 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);
+
+	-- Prevent stanza from reaching the room (it's just noise)
+	return true;
+end);
+
+-- Public API
+
+function get_user_read_marker(user_jid, room_jid)
+	return muc_marker_map_store:get(user_jid, room_jid);
+end