Annotate

mod_csi_muc_priorities/mod_csi_muc_priorities.lua @ 3536:eed657091329

mod_csi_muc_priorities: Allow specifying which MUC JIDs are less important
author Kim Alvefur <zash@zash.se>
date Mon, 01 Apr 2019 08:23:56 +0200
parent 3535:bcb0eb9121a9
child 3537:7d6fb9570395
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3536
eed657091329 mod_csi_muc_priorities: Allow specifying which MUC JIDs are less important
Kim Alvefur <zash@zash.se>
parents: 3535
diff changeset
1 local jid_bare, jid_split = import("util.jid", "bare", "split");
eed657091329 mod_csi_muc_priorities: Allow specifying which MUC JIDs are less important
Kim Alvefur <zash@zash.se>
parents: 3535
diff changeset
2
eed657091329 mod_csi_muc_priorities: Allow specifying which MUC JIDs are less important
Kim Alvefur <zash@zash.se>
parents: 3535
diff changeset
3 -- luacheck: ignore 122
eed657091329 mod_csi_muc_priorities: Allow specifying which MUC JIDs are less important
Kim Alvefur <zash@zash.se>
parents: 3535
diff changeset
4 local user_sessions = prosody.hosts[module.host].sessions;
3532
85c357b69eec mod_csi_muc_priorities: Reduce importance of group chat messages
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5
85c357b69eec mod_csi_muc_priorities: Reduce importance of group chat messages
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6 module:hook("csi-is-stanza-important", function (event)
85c357b69eec mod_csi_muc_priorities: Reduce importance of group chat messages
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7 local stanza, session = event.stanza, event.session;
85c357b69eec mod_csi_muc_priorities: Reduce importance of group chat messages
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8 if stanza.name == "message" then
85c357b69eec mod_csi_muc_priorities: Reduce importance of group chat messages
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9 if stanza.attr.type == "groupchat" then
85c357b69eec mod_csi_muc_priorities: Reduce importance of group chat messages
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10 local body = stanza:get_child_text("body");
85c357b69eec mod_csi_muc_priorities: Reduce importance of group chat messages
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 if not body then return end
85c357b69eec mod_csi_muc_priorities: Reduce importance of group chat messages
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12
3534
700340b57851 mod_csi_muc_priorities: Break out room jid into a variable
Kim Alvefur <zash@zash.se>
parents: 3533
diff changeset
13 local room_jid = jid_bare(stanza.attr.from);
700340b57851 mod_csi_muc_priorities: Break out room jid into a variable
Kim Alvefur <zash@zash.se>
parents: 3533
diff changeset
14
3536
eed657091329 mod_csi_muc_priorities: Allow specifying which MUC JIDs are less important
Kim Alvefur <zash@zash.se>
parents: 3535
diff changeset
15 local username = session.username;
eed657091329 mod_csi_muc_priorities: Allow specifying which MUC JIDs are less important
Kim Alvefur <zash@zash.se>
parents: 3535
diff changeset
16 local priorities = user_sessions[username].csi_muc_priorities;
eed657091329 mod_csi_muc_priorities: Allow specifying which MUC JIDs are less important
Kim Alvefur <zash@zash.se>
parents: 3535
diff changeset
17
eed657091329 mod_csi_muc_priorities: Allow specifying which MUC JIDs are less important
Kim Alvefur <zash@zash.se>
parents: 3535
diff changeset
18 if not priorities or priorities[room_jid] ~= false then
eed657091329 mod_csi_muc_priorities: Allow specifying which MUC JIDs are less important
Kim Alvefur <zash@zash.se>
parents: 3535
diff changeset
19 return nil;
eed657091329 mod_csi_muc_priorities: Allow specifying which MUC JIDs are less important
Kim Alvefur <zash@zash.se>
parents: 3535
diff changeset
20 end
eed657091329 mod_csi_muc_priorities: Allow specifying which MUC JIDs are less important
Kim Alvefur <zash@zash.se>
parents: 3535
diff changeset
21
3535
bcb0eb9121a9 mod_csi_muc_priorities: Add a comment
Kim Alvefur <zash@zash.se>
parents: 3534
diff changeset
22 -- Look for mention
3532
85c357b69eec mod_csi_muc_priorities: Reduce importance of group chat messages
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23 local rooms = session.rooms_joined;
3533
d8c4543f1b19 mod_csi_muc_priorities: Remove dependency on mod_track_muc_joins
Kim Alvefur <zash@zash.se>
parents: 3532
diff changeset
24 if not rooms then return; end
3532
85c357b69eec mod_csi_muc_priorities: Reduce importance of group chat messages
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25
3534
700340b57851 mod_csi_muc_priorities: Break out room jid into a variable
Kim Alvefur <zash@zash.se>
parents: 3533
diff changeset
26 local room_nick = rooms[room_jid];
3532
85c357b69eec mod_csi_muc_priorities: Reduce importance of group chat messages
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 if room_nick and body:find(room_nick, 1, true) then return true; end
85c357b69eec mod_csi_muc_priorities: Reduce importance of group chat messages
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 end
85c357b69eec mod_csi_muc_priorities: Reduce importance of group chat messages
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29 end
85c357b69eec mod_csi_muc_priorities: Reduce importance of group chat messages
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30 end);
85c357b69eec mod_csi_muc_priorities: Reduce importance of group chat messages
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31
3536
eed657091329 mod_csi_muc_priorities: Allow specifying which MUC JIDs are less important
Kim Alvefur <zash@zash.se>
parents: 3535
diff changeset
32 module:depends("adhoc");
eed657091329 mod_csi_muc_priorities: Allow specifying which MUC JIDs are less important
Kim Alvefur <zash@zash.se>
parents: 3535
diff changeset
33
eed657091329 mod_csi_muc_priorities: Allow specifying which MUC JIDs are less important
Kim Alvefur <zash@zash.se>
parents: 3535
diff changeset
34 local dataform = require"util.dataforms";
eed657091329 mod_csi_muc_priorities: Allow specifying which MUC JIDs are less important
Kim Alvefur <zash@zash.se>
parents: 3535
diff changeset
35 local adhoc_inital_data = require "util.adhoc".new_initial_data_form;
eed657091329 mod_csi_muc_priorities: Allow specifying which MUC JIDs are less important
Kim Alvefur <zash@zash.se>
parents: 3535
diff changeset
36 local instructions = [[
eed657091329 mod_csi_muc_priorities: Allow specifying which MUC JIDs are less important
Kim Alvefur <zash@zash.se>
parents: 3535
diff changeset
37 These settings affect battery optimizations performed by the server
eed657091329 mod_csi_muc_priorities: Allow specifying which MUC JIDs are less important
Kim Alvefur <zash@zash.se>
parents: 3535
diff changeset
38 while your client has indicated that it is inactive.
eed657091329 mod_csi_muc_priorities: Allow specifying which MUC JIDs are less important
Kim Alvefur <zash@zash.se>
parents: 3535
diff changeset
39 ]]
eed657091329 mod_csi_muc_priorities: Allow specifying which MUC JIDs are less important
Kim Alvefur <zash@zash.se>
parents: 3535
diff changeset
40
eed657091329 mod_csi_muc_priorities: Allow specifying which MUC JIDs are less important
Kim Alvefur <zash@zash.se>
parents: 3535
diff changeset
41 local priority_settings_form = dataform.new {
eed657091329 mod_csi_muc_priorities: Allow specifying which MUC JIDs are less important
Kim Alvefur <zash@zash.se>
parents: 3535
diff changeset
42 title = "Prioritize addresses of group chats";
eed657091329 mod_csi_muc_priorities: Allow specifying which MUC JIDs are less important
Kim Alvefur <zash@zash.se>
parents: 3535
diff changeset
43 instructions = instructions;
eed657091329 mod_csi_muc_priorities: Allow specifying which MUC JIDs are less important
Kim Alvefur <zash@zash.se>
parents: 3535
diff changeset
44 {
eed657091329 mod_csi_muc_priorities: Allow specifying which MUC JIDs are less important
Kim Alvefur <zash@zash.se>
parents: 3535
diff changeset
45 type = "hidden";
eed657091329 mod_csi_muc_priorities: Allow specifying which MUC JIDs are less important
Kim Alvefur <zash@zash.se>
parents: 3535
diff changeset
46 name = "FORM_TYPE";
eed657091329 mod_csi_muc_priorities: Allow specifying which MUC JIDs are less important
Kim Alvefur <zash@zash.se>
parents: 3535
diff changeset
47 value = "xmpp:modules.prosody.im/mod_"..module.name;
eed657091329 mod_csi_muc_priorities: Allow specifying which MUC JIDs are less important
Kim Alvefur <zash@zash.se>
parents: 3535
diff changeset
48 };
eed657091329 mod_csi_muc_priorities: Allow specifying which MUC JIDs are less important
Kim Alvefur <zash@zash.se>
parents: 3535
diff changeset
49 {
eed657091329 mod_csi_muc_priorities: Allow specifying which MUC JIDs are less important
Kim Alvefur <zash@zash.se>
parents: 3535
diff changeset
50 type = "jid-multi";
eed657091329 mod_csi_muc_priorities: Allow specifying which MUC JIDs are less important
Kim Alvefur <zash@zash.se>
parents: 3535
diff changeset
51 name = "unimportant";
eed657091329 mod_csi_muc_priorities: Allow specifying which MUC JIDs are less important
Kim Alvefur <zash@zash.se>
parents: 3535
diff changeset
52 label = "Lower priority";
eed657091329 mod_csi_muc_priorities: Allow specifying which MUC JIDs are less important
Kim Alvefur <zash@zash.se>
parents: 3535
diff changeset
53 desc = "E.g. large noisy public channels";
eed657091329 mod_csi_muc_priorities: Allow specifying which MUC JIDs are less important
Kim Alvefur <zash@zash.se>
parents: 3535
diff changeset
54 };
eed657091329 mod_csi_muc_priorities: Allow specifying which MUC JIDs are less important
Kim Alvefur <zash@zash.se>
parents: 3535
diff changeset
55 }
eed657091329 mod_csi_muc_priorities: Allow specifying which MUC JIDs are less important
Kim Alvefur <zash@zash.se>
parents: 3535
diff changeset
56
eed657091329 mod_csi_muc_priorities: Allow specifying which MUC JIDs are less important
Kim Alvefur <zash@zash.se>
parents: 3535
diff changeset
57 local store = module:open_store();
eed657091329 mod_csi_muc_priorities: Allow specifying which MUC JIDs are less important
Kim Alvefur <zash@zash.se>
parents: 3535
diff changeset
58 module:hook("resource-bind", function (event)
eed657091329 mod_csi_muc_priorities: Allow specifying which MUC JIDs are less important
Kim Alvefur <zash@zash.se>
parents: 3535
diff changeset
59 local username = event.session.username;
eed657091329 mod_csi_muc_priorities: Allow specifying which MUC JIDs are less important
Kim Alvefur <zash@zash.se>
parents: 3535
diff changeset
60 user_sessions[username].csi_muc_priorities = store:get(username);
eed657091329 mod_csi_muc_priorities: Allow specifying which MUC JIDs are less important
Kim Alvefur <zash@zash.se>
parents: 3535
diff changeset
61 end);
eed657091329 mod_csi_muc_priorities: Allow specifying which MUC JIDs are less important
Kim Alvefur <zash@zash.se>
parents: 3535
diff changeset
62
eed657091329 mod_csi_muc_priorities: Allow specifying which MUC JIDs are less important
Kim Alvefur <zash@zash.se>
parents: 3535
diff changeset
63 local adhoc_command_handler = adhoc_inital_data(priority_settings_form, function (data)
eed657091329 mod_csi_muc_priorities: Allow specifying which MUC JIDs are less important
Kim Alvefur <zash@zash.se>
parents: 3535
diff changeset
64 local username = jid_split(data.from);
eed657091329 mod_csi_muc_priorities: Allow specifying which MUC JIDs are less important
Kim Alvefur <zash@zash.se>
parents: 3535
diff changeset
65 local prioritized_jids = user_sessions[username].csi_muc_priorities or store:get(username);
eed657091329 mod_csi_muc_priorities: Allow specifying which MUC JIDs are less important
Kim Alvefur <zash@zash.se>
parents: 3535
diff changeset
66 local unimportant = {};
eed657091329 mod_csi_muc_priorities: Allow specifying which MUC JIDs are less important
Kim Alvefur <zash@zash.se>
parents: 3535
diff changeset
67 if prioritized_jids then
eed657091329 mod_csi_muc_priorities: Allow specifying which MUC JIDs are less important
Kim Alvefur <zash@zash.se>
parents: 3535
diff changeset
68 for jid in pairs(prioritized_jids) do
eed657091329 mod_csi_muc_priorities: Allow specifying which MUC JIDs are less important
Kim Alvefur <zash@zash.se>
parents: 3535
diff changeset
69 table.insert(unimportant, jid);
eed657091329 mod_csi_muc_priorities: Allow specifying which MUC JIDs are less important
Kim Alvefur <zash@zash.se>
parents: 3535
diff changeset
70 end
eed657091329 mod_csi_muc_priorities: Allow specifying which MUC JIDs are less important
Kim Alvefur <zash@zash.se>
parents: 3535
diff changeset
71 end
eed657091329 mod_csi_muc_priorities: Allow specifying which MUC JIDs are less important
Kim Alvefur <zash@zash.se>
parents: 3535
diff changeset
72 return { unimportant = unimportant };
eed657091329 mod_csi_muc_priorities: Allow specifying which MUC JIDs are less important
Kim Alvefur <zash@zash.se>
parents: 3535
diff changeset
73 end, function(fields, form_err, data)
eed657091329 mod_csi_muc_priorities: Allow specifying which MUC JIDs are less important
Kim Alvefur <zash@zash.se>
parents: 3535
diff changeset
74 if form_err then
eed657091329 mod_csi_muc_priorities: Allow specifying which MUC JIDs are less important
Kim Alvefur <zash@zash.se>
parents: 3535
diff changeset
75 return { status = "completed", error = { message = "Problem in submitted form" } };
eed657091329 mod_csi_muc_priorities: Allow specifying which MUC JIDs are less important
Kim Alvefur <zash@zash.se>
parents: 3535
diff changeset
76 end
eed657091329 mod_csi_muc_priorities: Allow specifying which MUC JIDs are less important
Kim Alvefur <zash@zash.se>
parents: 3535
diff changeset
77 local prioritized_jids = {};
eed657091329 mod_csi_muc_priorities: Allow specifying which MUC JIDs are less important
Kim Alvefur <zash@zash.se>
parents: 3535
diff changeset
78 for _, jid in ipairs(fields.unimportant) do
eed657091329 mod_csi_muc_priorities: Allow specifying which MUC JIDs are less important
Kim Alvefur <zash@zash.se>
parents: 3535
diff changeset
79 prioritized_jids[jid] = false;
eed657091329 mod_csi_muc_priorities: Allow specifying which MUC JIDs are less important
Kim Alvefur <zash@zash.se>
parents: 3535
diff changeset
80 end
eed657091329 mod_csi_muc_priorities: Allow specifying which MUC JIDs are less important
Kim Alvefur <zash@zash.se>
parents: 3535
diff changeset
81
eed657091329 mod_csi_muc_priorities: Allow specifying which MUC JIDs are less important
Kim Alvefur <zash@zash.se>
parents: 3535
diff changeset
82 local username = jid_split(data.from);
eed657091329 mod_csi_muc_priorities: Allow specifying which MUC JIDs are less important
Kim Alvefur <zash@zash.se>
parents: 3535
diff changeset
83 local ok, err = store:set(username, prioritized_jids);
eed657091329 mod_csi_muc_priorities: Allow specifying which MUC JIDs are less important
Kim Alvefur <zash@zash.se>
parents: 3535
diff changeset
84 if ok then
eed657091329 mod_csi_muc_priorities: Allow specifying which MUC JIDs are less important
Kim Alvefur <zash@zash.se>
parents: 3535
diff changeset
85 user_sessions[username].csi_muc_priorities = prioritized_jids;
eed657091329 mod_csi_muc_priorities: Allow specifying which MUC JIDs are less important
Kim Alvefur <zash@zash.se>
parents: 3535
diff changeset
86 return { status = "completed", info = "Priorities updated" };
eed657091329 mod_csi_muc_priorities: Allow specifying which MUC JIDs are less important
Kim Alvefur <zash@zash.se>
parents: 3535
diff changeset
87 else
eed657091329 mod_csi_muc_priorities: Allow specifying which MUC JIDs are less important
Kim Alvefur <zash@zash.se>
parents: 3535
diff changeset
88 return { status = "completed", error = { message = "Error saving priorities: "..err } };
eed657091329 mod_csi_muc_priorities: Allow specifying which MUC JIDs are less important
Kim Alvefur <zash@zash.se>
parents: 3535
diff changeset
89 end
eed657091329 mod_csi_muc_priorities: Allow specifying which MUC JIDs are less important
Kim Alvefur <zash@zash.se>
parents: 3535
diff changeset
90 end);
eed657091329 mod_csi_muc_priorities: Allow specifying which MUC JIDs are less important
Kim Alvefur <zash@zash.se>
parents: 3535
diff changeset
91
eed657091329 mod_csi_muc_priorities: Allow specifying which MUC JIDs are less important
Kim Alvefur <zash@zash.se>
parents: 3535
diff changeset
92 module:add_item("adhoc", module:require "adhoc".new("Configure group chat priorities",
eed657091329 mod_csi_muc_priorities: Allow specifying which MUC JIDs are less important
Kim Alvefur <zash@zash.se>
parents: 3535
diff changeset
93 "xmpp:modules.prosody.im/mod_"..module.name, adhoc_command_handler, "local_user"));