Changeset

5709: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 5708:9edc698848e9
children 5710:4c84cfb586c1
files mod_groups_muc_bookmarks/mod_groups_muc_bookmarks.lua
diffstat 1 files changed, 24 insertions(+), 0 deletions(-) [+]
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)