comparison 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
comparison
equal deleted inserted replaced
4025:57b4cdeba318 4026:e3964f876b5d
5 -- We rewrite the id because XEP-0333 doesn't tell clients explicitly which id to use 5 -- We rewrite the id because XEP-0333 doesn't tell clients explicitly which id to use
6 -- in marker reports. However it implies the 'id' attribute through examples, and this 6 -- in marker reports. However it implies the 'id' attribute through examples, and this
7 -- is what some clients implement. 7 -- is what some clients implement.
8 -- Notably Conversations will ack the origin-id instead. We need to update the XEP to 8 -- Notably Conversations will ack the origin-id instead. We need to update the XEP to
9 -- clarify the correct behaviour. 9 -- clarify the correct behaviour.
10
11 local st = require "util.stanza";
10 12
11 local xmlns_markers = "urn:xmpp:chat-markers:0"; 13 local xmlns_markers = "urn:xmpp:chat-markers:0";
12 14
13 local marker_element_name = module:get_option_string("muc_marker_type", "displayed"); 15 local marker_element_name = module:get_option_string("muc_marker_type", "displayed");
14 16
53 if marker then 55 if marker then
54 return false 56 return false
55 end 57 end
56 end); 58 end);
57 59
60 local function find_nickname(room, user_jid)
61 -- Find their current nickname
62 for nick, occupant in pairs(room._occupants) do
63 if occupant.bare_jid == user_jid then
64 return nick;
65 end
66 end
67 -- Or if they're not here
68 local nickname = room:get_affiliation_data(user_jid, "reserved_nickname");
69 if nickname then return room.jid.."/"..nickname; end
70 end
71
72 -- Synthesize markers
73 if muc_marker_map_store.get_all then
74 module:hook("muc-occupant-session-new", function (event)
75 local room, to = event.room, event.stanza.attr.from;
76 local markers = muc_marker_map_store:get_all(room.jid);
77 if not markers then return end
78 for user_jid, id in pairs(markers) do
79 local room_nick = find_nickname(room, user_jid);
80 if room_nick then
81 local recv_marker = st.message({ type = "groupchat", from = room_nick, to = to })
82 :tag("received", { xmlns = xmlns_markers, id = id });
83 room:route_stanza(recv_marker);
84 end
85 end
86 end);
87 end
88
58 -- Public API 89 -- Public API
59 90
60 function get_user_read_marker(user_jid, room_jid) 91 function get_user_read_marker(user_jid, room_jid)
61 return muc_marker_map_store:get(user_jid, room_jid); 92 return muc_marker_map_store:get(user_jid, room_jid);
62 end 93 end