annotate mod_muc_rai/mod_muc_rai.lua @ 4547:a9e997d5eb94

mod_rest: Replace 'oob_url' with datamapper-native 'oob' While 'oob_url' is simpler and covers all current use, it's harder to describe the XML mapping.
author Kim Alvefur <zash@zash.se>
date Sun, 25 Apr 2021 16:38:20 +0200
parents 9377b5593cc7
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
4543
9377b5593cc7 mod_statistics_statsd: Remove obsolete module, use the newer built-in statsd provider
Matthew Wild <mwild1@gmail.com>
parents: 4510
diff changeset
98 module:log("debug", "Last room message: %s (cached)", last_room_message_id);
3974
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99 return last_room_message_id;
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 end
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
102 -- Load all the data!
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103 local query = {
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
104 limit = 1;
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
105 reverse = true;
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
106 with = "message<groupchat";
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
107 }
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
108 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
109
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
110 if not data then
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
111 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
112 return nil;
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
113 end
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
115 local id = data();
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116 room_activity_cache:set(room_jid, id);
4543
9377b5593cc7 mod_statistics_statsd: Remove obsolete module, use the newer built-in statsd provider
Matthew Wild <mwild1@gmail.com>
parents: 4510
diff changeset
117 module:log("debug", "Last room message: %s (queried)", id);
3974
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
118 return id;
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119 end
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
120
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121 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
122 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
123 end
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
125 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
126 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
127 end
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
128
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
129 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
130 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
131 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
132 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
133 room_jid, last_room_message_id,
b44620cacb11 mod_muc_rai: Add debug logging
Matthew Wild <mwild1@gmail.com>
parents: 4059
diff changeset
134 user_jid, last_user_read_id,
b44620cacb11 mod_muc_rai: Add debug logging
Matthew Wild <mwild1@gmail.com>
parents: 4059
diff changeset
135 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
136 );
3974
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
137 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
138 end
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
139
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
140 -- 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
141 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
142 -- 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
143 -- 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
144 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
145 end
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
146
3998
67047ed63b15 mod_muc_rai: Ignore attempts to re-subscribe from the same full JID
Matthew Wild <mwild1@gmail.com>
parents: 3997
diff changeset
147 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
148 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
149 end
67047ed63b15 mod_muc_rai: Ignore attempts to re-subscribe from the same full JID
Matthew Wild <mwild1@gmail.com>
parents: 3997
diff changeset
150
3974
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
151 -- 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
152 -- 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
153 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
154 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
155 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
156 end
67047ed63b15 mod_muc_rai: Ignore attempts to re-subscribe from the same full JID
Matthew Wild <mwild1@gmail.com>
parents: 3997
diff changeset
157
3974
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
158 -- 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
159 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
160
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
161 if not interested_rooms then
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
162 if err then
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
163 return nil, "internal-server-error";
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 interested_rooms = {};
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
166 end
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
167
4058
cf9a1c7d558d mod_muc_rai: Don't store/modify existing table to track rooms
Matthew Wild <mwild1@gmail.com>
parents: 4057
diff changeset
168 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
169 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
170 return nil, "resource-constraint";
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
171 end
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
172
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
173 local rooms_with_activity;
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
174 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
175 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
176 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
177 -- 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
178 -- in the response
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
179 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
180 rooms_with_activity = {};
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
181 end
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
182 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
183 else
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
184 -- Subscribe to any future activity
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
185 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
186 end
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 return rooms_with_activity;
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
189 end
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
190
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
191 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
192 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
193 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
194 if ok then
4281
3c80e46e26f2 mod_muc_rai: Use log systems string formatting facilities
Kim Alvefur <zash@zash.se>
parents: 4277
diff changeset
195 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
196 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
197 end, -500);
4277
10dc4527574f mod_muc_rai: Subscribe/unsubscribe users when leaving/joining
Seve Ferrer <seve@delape.net>
parents: 4074
diff changeset
198
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
199 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
200 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
201 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
202 if ok then
4281
3c80e46e26f2 mod_muc_rai: Use log systems string formatting facilities
Kim Alvefur <zash@zash.se>
parents: 4277
diff changeset
203 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
204 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
205 end, -500);
4277
10dc4527574f mod_muc_rai: Subscribe/unsubscribe users when leaving/joining
Seve Ferrer <seve@delape.net>
parents: 4074
diff changeset
206
3974
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
207 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
208 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
209 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
210
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
211 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
212 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
213 return true;
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
214 end
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
215
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
216 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
217 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
218 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
219
3974
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
220 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
221
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
222 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
223 if not err then
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
224 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
225 return true;
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
226 else
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
227 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
228 end
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
229 end
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
230
3997
0e72dd70afff mod_muc_rai: Use correct stanza kind (message) for initial notification
Matthew Wild <mwild1@gmail.com>
parents: 3974
diff changeset
231 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
232 :tag("rai", { xmlns = xmlns_rai });
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
233 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
234 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
235 end
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
236 return origin.send(reply);
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
237 end);
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
238
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
239 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
240 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
241 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
242 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
243 -- 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
244 -- 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
245 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
246 -- 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
247 notify_interested_users(room.jid);
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
248 end
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
249 end, -1);