comparison mod_muc_room_mention_notifications/mod_muc_room_mention_notifications.lua @ 4280:278f2998ce49

mod_muc_room_mention_notifications: Publish module
author Seve Ferrer <seve@delape.net>
date Fri, 04 Dec 2020 16:58:36 +0100
parents
children 2ca55a4da3ea
comparison
equal deleted inserted replaced
4279:776eacd233b5 4280:278f2998ce49
1 local jid = require "util.jid";
2 local st = require "util.stanza";
3 local datetime = require "util.datetime";
4
5 local notify_unaffiliated_users = module:get_option("muc_rmn_notify_unaffiliated_users", false)
6
7 local muc_affiliation_store = module:open_store("config", "map");
8
9 local rmn_xmlns = "urn:xmpp:rmn:0";
10 local reference_xmlns = "urn:xmpp:reference:0";
11 local forwarded_xmlns = "urn:xmpp:forward:0";
12 local deplay_xmlns = "urn:xmpp:delay";
13
14
15 -- Returns a set of rooms the user is affiliated to
16 local function get_user_rooms(user_bare_jid)
17 return muc_affiliation_store:get_all(user_bare_jid);
18 end
19
20 local function is_eligible(user_bare_jid, room)
21 if notify_unaffiliated_users then return true; end
22
23 local user_rooms, err = get_user_rooms(user_bare_jid);
24 if not user_rooms then
25 if err then
26 return false, err;
27 end
28 return false;
29 end
30
31 local room_node = jid.node(room.jid)
32 if user_rooms[room_node] then
33 return true;
34 end
35
36 return false
37 end
38
39 -- Send a single notification for a room, updating data structures as needed
40 local function send_single_notification(user_bare_jid, room_jid, mention_stanza)
41 local notification = st.message({ to = user_bare_jid, from = module.host })
42 :tag("mentions", { xmlns = rmn_xmlns })
43 :tag("forwarded", {xmlns = forwarded_xmlns})
44 :tag("delay", {xmlns = deplay_xmlns, stamp = datetime.datetime()}):up()
45 :add_child(mention_stanza)
46 :reset();
47 module:log("debug", "Sending mention notification from %s to %s", room_jid, user_bare_jid);
48 return module:send(notification);
49 end
50
51 local function notify_mentioned_users(room, client_mentions, mention_stanza)
52 module:log("debug", "NOTIFYING FOR %s", room.jid)
53 for user_bare_jid in pairs(client_mentions) do
54 if is_eligible(user_bare_jid, room) then
55 send_single_notification(user_bare_jid, room.jid, mention_stanza);
56 end
57 end
58 end
59
60 local function get_mentions(stanza)
61 local has_mentions = false
62 local client_mentions = {}
63
64 for element in stanza:childtags("reference", reference_xmlns) do
65 if element.attr.type == "mention" then
66 local user_bare_jid = element.attr.uri:match("^xmpp:(.+)$");
67 if user_bare_jid then
68 client_mentions[user_bare_jid] = user_bare_jid;
69 has_mentions = true
70 end
71 end
72 end
73
74 return has_mentions, client_mentions
75 end
76
77 module:hook("muc-broadcast-message", function (event)
78 local room, stanza = event.room, event.stanza;
79 local body = stanza:get_child_text("body")
80 if not body or #body < 1 then return; end
81 local correction = stanza:get_child("replace", "urn:xmpp:message-correct:0");
82 if correction then return; end -- Do not notify on message corrections
83
84 local has_mentions, client_mentions = get_mentions(stanza)
85 if not has_mentions then return; end
86
87 -- Notify any users that need to be notified
88 notify_mentioned_users(room, client_mentions, stanza);
89 end, -1);