Annotate

mod_muc_rai/mod_muc_rai.lua @ 3974:f14c862598a9

mod_muc_rai: New module to implement Room Activity Indicators
author Matthew Wild <mwild1@gmail.com>
date Wed, 15 Apr 2020 21:19:45 +0100
child 3997:0e72dd70afff
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
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 local xmlns_rai = "xmpp:prosody.im/protocol/rai";
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)
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124 return muc_markers.get_user_read_marker(user_jid, room_jid);
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);
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
130 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
131 end
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
132
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
133 -- 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
134 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
135 -- 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
136 -- 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
137 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
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 -- 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
141 -- 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
142 local function subscribe_all_rooms(user_jid)
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
143 -- 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
144 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
145
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
146 if not interested_rooms then
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
147 if err then
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
148 return nil, "internal-server-error";
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
149 end
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
150 interested_rooms = {};
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
151 end
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
152
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
153 if not subscribed_users:set(user_jid, interested_rooms) then
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
154 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
155 return nil, "resource-constraint";
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
156 end
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
157
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
158 local rooms_with_activity;
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
159 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
160 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
161 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
162 -- 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
163 -- in the response
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
164 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
165 rooms_with_activity = {};
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 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
168 else
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
169 -- Subscribe to any future activity
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
170 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
171 end
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
172 end
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
173 return rooms_with_activity;
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
174 end
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
175
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
176 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
177 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
178 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
179
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
180 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
181 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
182 return true;
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
183 end
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
184
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
185 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
186
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
187 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
188 if not err then
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
189 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
190 return true;
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
191 else
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
192 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
193 end
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
194 end
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
195
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
196 local reply = st.reply(stanza)
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
197 :tag("rai", { xmlns = xmlns_rai });
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
198 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
199 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
200 end
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
201 return origin.send(reply);
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
202 end);
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
203
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
204 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
205 local room, stanza = event.room, event.stanza;
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
206 local archive_id = stanza:get_child_text("stanza-id", "urn:xmpp:sid:0");
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
207 if archive_id then
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
208 -- 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
209 -- to the per-user marker (managed by mod_muc_markers)
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
210 update_room_activity(room.jid, archive_id);
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
211 -- 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
212 notify_interested_users(room.jid);
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
213 end
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
214 end, -1);
f14c862598a9 mod_muc_rai: New module to implement Room Activity Indicators
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
215