comparison mod_groups_muc_bookmarks/mod_groups_muc_bookmarks.lua @ 5686:d5ff386dc97f

mod_groups_muc_bookmarks: Update bookmarks when a group MUC is added/removed
author Matthew Wild <mwild1@gmail.com>
date Thu, 02 Nov 2023 17:00:14 +0000
parents fdf50c4d23a3
children 220502eb05aa
comparison
equal deleted inserted replaced
5685:9edc698848e9 5686:d5ff386dc97f
39 local function find_matching_bookmark(storage, room) 39 local function find_matching_bookmark(storage, room)
40 return storage[room]; 40 return storage[room];
41 end 41 end
42 42
43 local function inject_bookmark(jid, room, autojoin, name) 43 local function inject_bookmark(jid, room, autojoin, name)
44 module:log("debug", "Injecting bookmark for %s into %s", room, jid);
44 local pep_service = mod_pep.get_pep_service(jid_split(jid)) 45 local pep_service = mod_pep.get_pep_service(jid_split(jid))
45 46
46 local current, err = get_current_bookmarks(jid, pep_service); 47 local current, err = get_current_bookmarks(jid, pep_service);
47 if err then 48 if err then
48 module:log("error", "Could not retrieve existing bookmarks for %s: %s", jid, err); 49 module:log("error", "Could not retrieve existing bookmarks for %s: %s", jid, err);
95 remove_bookmark(jid, event.group_info.muc_jid, true, event.group_info.name) 96 remove_bookmark(jid, event.group_info.muc_jid, true, event.group_info.name)
96 end 97 end
97 98
98 module:hook("group-user-added", handle_user_added) 99 module:hook("group-user-added", handle_user_added)
99 module:hook("group-user-removed", handle_user_removed) 100 module:hook("group-user-removed", handle_user_removed)
101
102
103 local function handle_muc_added(event)
104 -- Add MUC to all members' bookmarks
105 module:log("info", "Adding new group chat to all member bookmarks...");
106 local muc_jid, muc_name = event.muc.jid, event.muc.name;
107 for member_username in pairs(mod_groups.get_members(event.group_id)) do
108 local member_jid = member_username .. "@" .. module.host;
109 inject_bookmark(member_jid, muc_jid, true, muc_name);
110 end
111 end
112
113 local function handle_muc_removed(event)
114 -- Remove MUC from all members' bookmarks
115 local muc_jid = event.muc.jid;
116 for member_username in ipairs(mod_groups.get_members(event.group_id)) do
117 local member_jid = member_username .. "@" .. module.host;
118 remove_bookmark(member_jid, muc_jid);
119 end
120 end
121
122 module:hook("group-chat-added", handle_muc_added)
123 module:hook("group-chat-removed", handle_muc_removed)