mod_csi_muc_priorities/mod_csi_muc_priorities.lua
changeset 3540 eed657091329
parent 3539 bcb0eb9121a9
child 3541 7d6fb9570395
equal deleted inserted replaced
3539:bcb0eb9121a9 3540:eed657091329
     1 local jid_bare = require "util.jid".bare;
     1 local jid_bare, jid_split = import("util.jid", "bare", "split");
       
     2 
       
     3 -- luacheck: ignore 122
       
     4 local user_sessions = prosody.hosts[module.host].sessions;
     2 
     5 
     3 module:hook("csi-is-stanza-important", function (event)
     6 module:hook("csi-is-stanza-important", function (event)
     4 	local stanza, session = event.stanza, event.session;
     7 	local stanza, session = event.stanza, event.session;
     5 	if stanza.name == "message" then
     8 	if stanza.name == "message" then
     6 		if stanza.attr.type == "groupchat" then
     9 		if stanza.attr.type == "groupchat" then
     7 			local body = stanza:get_child_text("body");
    10 			local body = stanza:get_child_text("body");
     8 			if not body then return end
    11 			if not body then return end
     9 
    12 
    10 			local room_jid = jid_bare(stanza.attr.from);
    13 			local room_jid = jid_bare(stanza.attr.from);
    11 
    14 
       
    15 			local username = session.username;
       
    16 			local priorities = user_sessions[username].csi_muc_priorities;
       
    17 
       
    18 			if not priorities or priorities[room_jid] ~= false then
       
    19 				return nil;
       
    20 			end
       
    21 
    12 			-- Look for mention
    22 			-- Look for mention
    13 			local rooms = session.rooms_joined;
    23 			local rooms = session.rooms_joined;
    14 			if not rooms then return; end
    24 			if not rooms then return; end
    15 
    25 
    16 			local room_nick = rooms[room_jid];
    26 			local room_nick = rooms[room_jid];
    17 			if room_nick and body:find(room_nick, 1, true) then return true; end
    27 			if room_nick and body:find(room_nick, 1, true) then return true; end
    18 
       
    19 			return false;
       
    20 		end
    28 		end
    21 	end
    29 	end
    22 end);
    30 end);
    23 
    31 
       
    32 module:depends("adhoc");
       
    33 
       
    34 local dataform = require"util.dataforms";
       
    35 local adhoc_inital_data = require "util.adhoc".new_initial_data_form;
       
    36 local instructions = [[
       
    37 These settings affect battery optimizations performed by the server
       
    38 while your client has indicated that it is inactive.
       
    39 ]]
       
    40 
       
    41 local priority_settings_form = dataform.new {
       
    42 	title = "Prioritize addresses of group chats";
       
    43 	instructions = instructions;
       
    44 	{
       
    45 		type = "hidden";
       
    46 		name = "FORM_TYPE";
       
    47 		value = "xmpp:modules.prosody.im/mod_"..module.name;
       
    48 	};
       
    49 	{
       
    50 		type = "jid-multi";
       
    51 		name = "unimportant";
       
    52 		label = "Lower priority";
       
    53 		desc = "E.g. large noisy public channels";
       
    54 	};
       
    55 }
       
    56 
       
    57 local store = module:open_store();
       
    58 module:hook("resource-bind", function (event)
       
    59 	local username = event.session.username;
       
    60 	user_sessions[username].csi_muc_priorities = store:get(username);
       
    61 end);
       
    62 
       
    63 local adhoc_command_handler = adhoc_inital_data(priority_settings_form, function (data)
       
    64 	local username = jid_split(data.from);
       
    65 	local prioritized_jids = user_sessions[username].csi_muc_priorities or store:get(username);
       
    66 	local unimportant = {};
       
    67 	if prioritized_jids then
       
    68 		for jid in pairs(prioritized_jids) do
       
    69 			table.insert(unimportant, jid);
       
    70 		end
       
    71 	end
       
    72 	return { unimportant = unimportant };
       
    73 end, function(fields, form_err, data)
       
    74 	if form_err then
       
    75 		return { status = "completed", error = { message = "Problem in submitted form" } };
       
    76 	end
       
    77 	local prioritized_jids = {};
       
    78 	for _, jid in ipairs(fields.unimportant) do
       
    79 		prioritized_jids[jid] = false;
       
    80 	end
       
    81 
       
    82 	local username = jid_split(data.from);
       
    83 	local ok, err = store:set(username, prioritized_jids);
       
    84 	if ok then
       
    85 		user_sessions[username].csi_muc_priorities = prioritized_jids;
       
    86 		return { status = "completed", info = "Priorities updated" };
       
    87 	else
       
    88 		return { status = "completed", error = { message = "Error saving priorities: "..err } };
       
    89 	end
       
    90 end);
       
    91 
       
    92 module:add_item("adhoc", module:require "adhoc".new("Configure group chat priorities",
       
    93 	"xmpp:modules.prosody.im/mod_"..module.name, adhoc_command_handler, "local_user"));