diff 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
line wrap: on
line diff
--- a/mod_groups_muc_bookmarks/mod_groups_muc_bookmarks.lua	Thu Nov 02 16:59:44 2023 +0000
+++ b/mod_groups_muc_bookmarks/mod_groups_muc_bookmarks.lua	Thu Nov 02 17:00:14 2023 +0000
@@ -41,6 +41,7 @@
 end
 
 local function inject_bookmark(jid, room, autojoin, name)
+	module:log("debug", "Injecting bookmark for %s into %s", room, jid);
 	local pep_service = mod_pep.get_pep_service(jid_split(jid))
 
 	local current, err = get_current_bookmarks(jid, pep_service);
@@ -97,3 +98,26 @@
 
 module:hook("group-user-added", handle_user_added)
 module:hook("group-user-removed", handle_user_removed)
+
+
+local function handle_muc_added(event)
+	-- Add MUC to all members' bookmarks
+	module:log("info", "Adding new group chat to all member bookmarks...");
+	local muc_jid, muc_name = event.muc.jid, event.muc.name;
+	for member_username in pairs(mod_groups.get_members(event.group_id)) do
+		local member_jid = member_username .. "@" .. module.host;
+		inject_bookmark(member_jid, muc_jid, true, muc_name);
+	end
+end
+
+local function handle_muc_removed(event)
+	-- Remove MUC from all members' bookmarks
+	local muc_jid = event.muc.jid;
+	for member_username in ipairs(mod_groups.get_members(event.group_id)) do
+		local member_jid = member_username .. "@" .. module.host;
+		remove_bookmark(member_jid, muc_jid);
+	end
+end
+
+module:hook("group-chat-added", handle_muc_added)
+module:hook("group-chat-removed", handle_muc_removed)