Mercurial > prosody-modules
annotate mod_muc_rai/mod_muc_rai.lua @ 4513:ade2064160e3
mod_pubsub_summary: Fix to not strip inline links
Links were turned into `label <url>` and then a later pass removed the
`<url>` part leaving only the label. This should avoid that. Escaping is
hard.
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Wed, 17 Mar 2021 15:52:12 +0100 |
parents | 6690586826e8 |
children | 9377b5593cc7 |
rev | line source |
---|---|
3974
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
1 local cache = require "util.cache"; |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
2 local jid = require "util.jid"; |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
3 local st = require "util.stanza"; |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
4 |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
5 local max_subscribers = module:get_option_number("muc_rai_max_subscribers", 1024); |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
6 |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
7 local muc_affiliation_store = module:open_store("config", "map"); |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
8 local muc_archive = module:open_store("muc_log", "archive"); |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
9 |
4297
4a5c4a352b78
mod_muc_rai: Use xmlns from XEP-0437
JC Brand <jc@opkode.com>
parents:
4281
diff
changeset
|
10 local xmlns_rai = "urn:xmpp:rai:0"; |
3974
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
12 local muc_markers = module:depends("muc_markers"); |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 -- subscriber_jid -> { [room_jid] = interested } |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 local subscribed_users = cache.new(max_subscribers, false); |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 -- room_jid -> { [user_jid] = interested } |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 local interested_users = {}; |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 -- room_jid -> last_id |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 local room_activity_cache = cache.new(1024); |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 -- Send a single notification for a room, updating data structures as needed |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 local function send_single_notification(user_jid, room_jid) |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 local notification = st.message({ to = user_jid, from = module.host }) |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
24 :tag("rai", { xmlns = xmlns_rai }) |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
25 :text_tag("activity", room_jid) |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 :up(); |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 local interested_room_users = interested_users[room_jid]; |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 if interested_room_users then |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 interested_room_users[user_jid] = nil; |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 end |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 local interested_rooms = subscribed_users:get(user_jid); |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
32 if interested_rooms then |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 interested_rooms[room_jid] = nil; |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 end |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
35 module:log("debug", "Sending notification from %s to %s", room_jid, user_jid); |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
36 return module:send(notification); |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
37 end |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
38 |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
39 local function subscribe_room(user_jid, room_jid) |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
40 local interested_rooms = subscribed_users:get(user_jid); |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
41 if not interested_rooms then |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
42 return nil, "not-subscribed"; |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
43 end |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
44 module:log("debug", "Subscribed %s to %s", user_jid, room_jid); |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
45 interested_rooms[room_jid] = true; |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
47 local interested_room_users = interested_users[room_jid]; |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
48 if not interested_room_users then |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 interested_room_users = {}; |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 interested_users[room_jid] = interested_room_users; |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
51 end |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 interested_room_users[user_jid] = true; |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 return true; |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
54 end |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
55 |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
56 local function unsubscribe_room(user_jid, room_jid) |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
57 local interested_rooms = subscribed_users:get(user_jid); |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
58 if not interested_rooms then |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
59 return nil, "not-subscribed"; |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
60 end |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
61 interested_rooms[room_jid] = nil; |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
62 |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
63 local interested_room_users = interested_users[room_jid]; |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
64 if not interested_room_users then |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
65 return true; |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
66 end |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
67 interested_room_users[user_jid] = nil; |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
68 return true; |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
69 end |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
70 |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
71 local function notify_interested_users(room_jid) |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
72 module:log("warn", "NOTIFYING FOR %s", room_jid) |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
73 local interested_room_users = interested_users[room_jid]; |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
74 if not interested_room_users then |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
75 module:log("debug", "Nobody interested in %s", room_jid); |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
76 return; |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
77 end |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
78 for user_jid in pairs(interested_room_users) do |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
79 send_single_notification(user_jid, room_jid); |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
80 end |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
81 return true; |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
82 end |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
83 |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
84 local function unsubscribe_user_from_all_rooms(user_jid) |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
85 local interested_rooms = subscribed_users:get(user_jid); |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
86 if not interested_rooms then |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
87 return nil, "not-subscribed"; |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
88 end |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
89 for room_jid in pairs(interested_rooms) do |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
90 unsubscribe_room(user_jid, room_jid); |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
91 end |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
92 return true; |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
93 end |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
94 |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
95 local function get_last_room_message_id(room_jid) |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
96 local last_room_message_id = room_activity_cache:get(room_jid); |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
97 if last_room_message_id then |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
98 return last_room_message_id; |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
99 end |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
100 |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
101 -- Load all the data! |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
102 local query = { |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
103 limit = 1; |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
104 reverse = true; |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
105 with = "message<groupchat"; |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
106 } |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
107 local data, err = muc_archive:find(jid.node(room_jid), query); |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
108 |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
109 if not data then |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
110 module:log("error", "Could not fetch history: %s", err); |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
111 return nil; |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
112 end |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
113 |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
114 local id = data(); |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
115 room_activity_cache:set(room_jid, id); |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
116 return id; |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
117 end |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
118 |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
119 local function update_room_activity(room_jid, last_id) |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
120 room_activity_cache:set(room_jid, last_id); |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
121 end |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
122 |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
123 local function get_last_user_read_id(user_jid, room_jid) |
4057
2ede3450abca
mod_muc_rai: Fix to use bare JID where a bare JID is needed
Matthew Wild <mwild1@gmail.com>
parents:
3998
diff
changeset
|
124 return muc_markers.get_user_read_marker(jid.bare(user_jid), room_jid); |
3974
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
125 end |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
126 |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
127 local function has_new_activity(room_jid, user_jid) |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
128 local last_room_message_id = get_last_room_message_id(room_jid); |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
129 local last_user_read_id = get_last_user_read_id(user_jid, room_jid); |
4060
b44620cacb11
mod_muc_rai: Add debug logging
Matthew Wild <mwild1@gmail.com>
parents:
4059
diff
changeset
|
130 module:log("debug", "Checking activity in <%s> (%s) for <%s> (%s): %s", |
b44620cacb11
mod_muc_rai: Add debug logging
Matthew Wild <mwild1@gmail.com>
parents:
4059
diff
changeset
|
131 room_jid, last_room_message_id, |
b44620cacb11
mod_muc_rai: Add debug logging
Matthew Wild <mwild1@gmail.com>
parents:
4059
diff
changeset
|
132 user_jid, last_user_read_id, |
b44620cacb11
mod_muc_rai: Add debug logging
Matthew Wild <mwild1@gmail.com>
parents:
4059
diff
changeset
|
133 tostring(last_room_message_id ~= last_user_read_id) |
b44620cacb11
mod_muc_rai: Add debug logging
Matthew Wild <mwild1@gmail.com>
parents:
4059
diff
changeset
|
134 ); |
3974
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
135 return last_room_message_id ~= last_user_read_id; |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
136 end |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
137 |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
138 -- Returns a set of rooms that a user is interested in |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
139 local function get_interested_rooms(user_jid) |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
140 -- Use affiliation as an indication of interest, return |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
141 -- all rooms a user is affiliated |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
142 return muc_affiliation_store:get_all(jid.bare(user_jid)); |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
143 end |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
144 |
3998
67047ed63b15
mod_muc_rai: Ignore attempts to re-subscribe from the same full JID
Matthew Wild <mwild1@gmail.com>
parents:
3997
diff
changeset
|
145 local function is_subscribed(user_jid) |
67047ed63b15
mod_muc_rai: Ignore attempts to re-subscribe from the same full JID
Matthew Wild <mwild1@gmail.com>
parents:
3997
diff
changeset
|
146 return not not subscribed_users:get(user_jid); |
67047ed63b15
mod_muc_rai: Ignore attempts to re-subscribe from the same full JID
Matthew Wild <mwild1@gmail.com>
parents:
3997
diff
changeset
|
147 end |
67047ed63b15
mod_muc_rai: Ignore attempts to re-subscribe from the same full JID
Matthew Wild <mwild1@gmail.com>
parents:
3997
diff
changeset
|
148 |
3974
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
149 -- Subscribes to all rooms that the user has an interest in |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
150 -- Returns a set of room JIDs that have already had activity (thus no subscription) |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
151 local function subscribe_all_rooms(user_jid) |
3998
67047ed63b15
mod_muc_rai: Ignore attempts to re-subscribe from the same full JID
Matthew Wild <mwild1@gmail.com>
parents:
3997
diff
changeset
|
152 if is_subscribed(user_jid) then |
67047ed63b15
mod_muc_rai: Ignore attempts to re-subscribe from the same full JID
Matthew Wild <mwild1@gmail.com>
parents:
3997
diff
changeset
|
153 return nil; |
67047ed63b15
mod_muc_rai: Ignore attempts to re-subscribe from the same full JID
Matthew Wild <mwild1@gmail.com>
parents:
3997
diff
changeset
|
154 end |
67047ed63b15
mod_muc_rai: Ignore attempts to re-subscribe from the same full JID
Matthew Wild <mwild1@gmail.com>
parents:
3997
diff
changeset
|
155 |
3974
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
156 -- Send activity notifications for all relevant rooms |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
157 local interested_rooms, err = get_interested_rooms(user_jid); |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
158 |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
159 if not interested_rooms then |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
160 if err then |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
161 return nil, "internal-server-error"; |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
162 end |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
163 interested_rooms = {}; |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
164 end |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
165 |
4058
cf9a1c7d558d
mod_muc_rai: Don't store/modify existing table to track rooms
Matthew Wild <mwild1@gmail.com>
parents:
4057
diff
changeset
|
166 if not subscribed_users:set(user_jid, {}) then |
3974
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
167 module:log("warn", "Subscriber limit (%d) reached, rejecting subscription from %s", max_subscribers, user_jid); |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
168 return nil, "resource-constraint"; |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
169 end |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
170 |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
171 local rooms_with_activity; |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
172 for room_name in pairs(interested_rooms) do |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
173 local room_jid = room_name.."@"..module.host; |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
174 if has_new_activity(room_jid, user_jid) then |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
175 -- There has already been activity, include this room |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
176 -- in the response |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
177 if not rooms_with_activity then |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
178 rooms_with_activity = {}; |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
179 end |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
180 rooms_with_activity[room_jid] = true; |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
181 else |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
182 -- Subscribe to any future activity |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
183 subscribe_room(user_jid, room_jid); |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
184 end |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
185 end |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
186 return rooms_with_activity; |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
187 end |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
188 |
4510
6690586826e8
mod_muc_rai: Switch to low-priority pre-* events, which should suffice for per-session tracking
Matthew Wild <mwild1@gmail.com>
parents:
4335
diff
changeset
|
189 module:hook("muc-occupant-pre-join", function(event) |
4277
10dc4527574f
mod_muc_rai: Subscribe/unsubscribe users when leaving/joining
Seve Ferrer <seve@delape.net>
parents:
4074
diff
changeset
|
190 local room_jid, user_jid = event.room.jid, event.stanza.attr.from; |
4297
4a5c4a352b78
mod_muc_rai: Use xmlns from XEP-0437
JC Brand <jc@opkode.com>
parents:
4281
diff
changeset
|
191 local ok, _ = unsubscribe_room(user_jid, room_jid); |
4277
10dc4527574f
mod_muc_rai: Subscribe/unsubscribe users when leaving/joining
Seve Ferrer <seve@delape.net>
parents:
4074
diff
changeset
|
192 if ok then |
4281
3c80e46e26f2
mod_muc_rai: Use log systems string formatting facilities
Kim Alvefur <zash@zash.se>
parents:
4277
diff
changeset
|
193 module:log("debug", "Unsubscribed %s to %s Reason: muc-occupant-joined", user_jid, room_jid) |
4277
10dc4527574f
mod_muc_rai: Subscribe/unsubscribe users when leaving/joining
Seve Ferrer <seve@delape.net>
parents:
4074
diff
changeset
|
194 end |
4510
6690586826e8
mod_muc_rai: Switch to low-priority pre-* events, which should suffice for per-session tracking
Matthew Wild <mwild1@gmail.com>
parents:
4335
diff
changeset
|
195 end, -500); |
4277
10dc4527574f
mod_muc_rai: Subscribe/unsubscribe users when leaving/joining
Seve Ferrer <seve@delape.net>
parents:
4074
diff
changeset
|
196 |
4510
6690586826e8
mod_muc_rai: Switch to low-priority pre-* events, which should suffice for per-session tracking
Matthew Wild <mwild1@gmail.com>
parents:
4335
diff
changeset
|
197 module:hook("muc-occupant-pre-leave", function(event) |
6690586826e8
mod_muc_rai: Switch to low-priority pre-* events, which should suffice for per-session tracking
Matthew Wild <mwild1@gmail.com>
parents:
4335
diff
changeset
|
198 local room_jid, user_jid = event.room.jid, event.stanza.attr.from; |
4297
4a5c4a352b78
mod_muc_rai: Use xmlns from XEP-0437
JC Brand <jc@opkode.com>
parents:
4281
diff
changeset
|
199 local ok, _ = subscribe_room(user_jid, room_jid); |
4277
10dc4527574f
mod_muc_rai: Subscribe/unsubscribe users when leaving/joining
Seve Ferrer <seve@delape.net>
parents:
4074
diff
changeset
|
200 if ok then |
4281
3c80e46e26f2
mod_muc_rai: Use log systems string formatting facilities
Kim Alvefur <zash@zash.se>
parents:
4277
diff
changeset
|
201 module:log("debug", "Subscribed %s to %s Reason: muc-occupant-left", user_jid, room_jid) |
4277
10dc4527574f
mod_muc_rai: Subscribe/unsubscribe users when leaving/joining
Seve Ferrer <seve@delape.net>
parents:
4074
diff
changeset
|
202 end |
4510
6690586826e8
mod_muc_rai: Switch to low-priority pre-* events, which should suffice for per-session tracking
Matthew Wild <mwild1@gmail.com>
parents:
4335
diff
changeset
|
203 end, -500); |
4277
10dc4527574f
mod_muc_rai: Subscribe/unsubscribe users when leaving/joining
Seve Ferrer <seve@delape.net>
parents:
4074
diff
changeset
|
204 |
3974
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
205 module:hook("presence/host", function (event) |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
206 local origin, stanza = event.origin, event.stanza; |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
207 local user_jid = stanza.attr.from; |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
208 |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
209 if stanza.attr.type == "unavailable" then -- User going offline |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
210 unsubscribe_user_from_all_rooms(user_jid); |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
211 return true; |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
212 end |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
213 |
4074
4b84beb48ba0
mod_muc_rai: Ignore presence to host that doesn't contain the <rai/> element
Matthew Wild <mwild1@gmail.com>
parents:
4060
diff
changeset
|
214 if not stanza:get_child("rai", xmlns_rai) then |
4b84beb48ba0
mod_muc_rai: Ignore presence to host that doesn't contain the <rai/> element
Matthew Wild <mwild1@gmail.com>
parents:
4060
diff
changeset
|
215 return; -- Ignore, no <rai/> tag |
4b84beb48ba0
mod_muc_rai: Ignore presence to host that doesn't contain the <rai/> element
Matthew Wild <mwild1@gmail.com>
parents:
4060
diff
changeset
|
216 end |
4b84beb48ba0
mod_muc_rai: Ignore presence to host that doesn't contain the <rai/> element
Matthew Wild <mwild1@gmail.com>
parents:
4060
diff
changeset
|
217 |
3974
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
218 local rooms_with_activity, err = subscribe_all_rooms(user_jid); |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
219 |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
220 if not rooms_with_activity then |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
221 if not err then |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
222 module:log("debug", "No activity to notify"); |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
223 return true; |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
224 else |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
225 return origin.send(st.error_reply(stanza, "wait", "resource-constraint")); |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
226 end |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
227 end |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
228 |
3997
0e72dd70afff
mod_muc_rai: Use correct stanza kind (message) for initial notification
Matthew Wild <mwild1@gmail.com>
parents:
3974
diff
changeset
|
229 local reply = st.message({ to = stanza.attr.from, from = module.host }) |
3974
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
230 :tag("rai", { xmlns = xmlns_rai }); |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
231 for room_jid in pairs(rooms_with_activity) do |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
232 reply:text_tag("activity", room_jid); |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
233 end |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
234 return origin.send(reply); |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
235 end); |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
236 |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
237 module:hook("muc-broadcast-message", function (event) |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
238 local room, stanza = event.room, event.stanza; |
4059
072366857d79
mod_muc_rai: Fix to correctly extract the archive id
Matthew Wild <mwild1@gmail.com>
parents:
4058
diff
changeset
|
239 local archive_id = stanza:get_child("stanza-id", "urn:xmpp:sid:0"); |
072366857d79
mod_muc_rai: Fix to correctly extract the archive id
Matthew Wild <mwild1@gmail.com>
parents:
4058
diff
changeset
|
240 if archive_id and archive_id.attr.id then |
3974
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
241 -- Remember the id of the last message so we can compare it |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
242 -- to the per-user marker (managed by mod_muc_markers) |
4059
072366857d79
mod_muc_rai: Fix to correctly extract the archive id
Matthew Wild <mwild1@gmail.com>
parents:
4058
diff
changeset
|
243 update_room_activity(room.jid, archive_id.attr.id); |
3974
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
244 -- Notify any users that need to be notified |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
245 notify_interested_users(room.jid); |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
246 end |
f14c862598a9
mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
247 end, -1); |