From efd22e33b552b82e2ec8fd770a1bfbac426b0158 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Mon, 27 Feb 2023 00:08:23 +0200 Subject: [PATCH] Delete guild portals too in delete-all-portals --- commands.go | 26 ++++++++++++++++---------- guildportal.go | 11 +++++++++++ portal.go | 9 --------- 3 files changed, 27 insertions(+), 19 deletions(-) diff --git a/commands.go b/commands.go index 5293c3f..4896d4e 100644 --- a/commands.go +++ b/commands.go @@ -30,6 +30,7 @@ import ( "github.com/skip2/go-qrcode" "maunium.net/go/mautrix" + "maunium.net/go/mautrix/appservice" "maunium.net/go/mautrix/bridge/commands" "maunium.net/go/mautrix/event" "maunium.net/go/mautrix/id" @@ -426,14 +427,15 @@ var cmdDeleteAllPortals = &commands.FullHandler{ func fnDeleteAllPortals(ce *WrappedCommandEvent) { portals := ce.Bridge.GetAllPortals() - if len(portals) == 0 { + guilds := ce.Bridge.GetAllGuilds() + if len(portals) == 0 && len(guilds) == 0 { ce.Reply("Didn't find any portals") return } - leave := func(portal *Portal) { - if len(portal.MXID) > 0 { - _, _ = portal.MainIntent().KickUser(portal.MXID, &mautrix.ReqKickUser{ + leave := func(mxid id.RoomID, intent *appservice.IntentAPI) { + if len(mxid) > 0 { + _, _ = intent.KickUser(mxid, &mautrix.ReqKickUser{ Reason: "Deleting portal", UserID: ce.User.MXID, }) @@ -442,17 +444,21 @@ func fnDeleteAllPortals(ce *WrappedCommandEvent) { customPuppet := ce.Bridge.GetPuppetByCustomMXID(ce.User.MXID) if customPuppet != nil && customPuppet.CustomIntent() != nil { intent := customPuppet.CustomIntent() - leave = func(portal *Portal) { - if len(portal.MXID) > 0 { - _, _ = intent.LeaveRoom(portal.MXID) - _, _ = intent.ForgetRoom(portal.MXID) + leave = func(mxid id.RoomID, _ *appservice.IntentAPI) { + if len(mxid) > 0 { + _, _ = intent.LeaveRoom(mxid) + _, _ = intent.ForgetRoom(mxid) } } } - ce.Reply("Found %d portals, deleting...", len(portals)) + ce.Reply("Found %d channel portals and %d guild portals, deleting...", len(portals), len(guilds)) for _, portal := range portals { portal.Delete() - leave(portal) + leave(portal.MXID, portal.MainIntent()) + } + for _, guild := range guilds { + guild.Delete() + leave(guild.MXID, ce.Bot) } ce.Reply("Finished deleting portal info. Now cleaning up rooms in background.") diff --git a/guildportal.go b/guildportal.go index 515e942..222f055 100644 --- a/guildportal.go +++ b/guildportal.go @@ -315,3 +315,14 @@ func (guild *Guild) RemoveMXID() { guild.BridgingMode = database.GuildBridgeNothing guild.Update() } + +func (guild *Guild) Delete() { + guild.Guild.Delete() + guild.bridge.guildsLock.Lock() + delete(guild.bridge.guildsByID, guild.ID) + if guild.MXID != "" { + delete(guild.bridge.guildsByMXID, guild.MXID) + } + guild.bridge.guildsLock.Unlock() + +} diff --git a/portal.go b/portal.go index 8f9ba1c..735cbec 100644 --- a/portal.go +++ b/portal.go @@ -1210,15 +1210,6 @@ func (portal *Portal) HandleMatrixLeave(brSender bridge.User) { func (portal *Portal) HandleMatrixKick(brSender bridge.User, brTarget bridge.Ghost) {} func (portal *Portal) HandleMatrixInvite(brSender bridge.User, brTarget bridge.Ghost) {} -func (portal *Portal) leave(sender *User) { - if portal.MXID == "" { - return - } - - intent := portal.bridge.GetPuppetByID(sender.DiscordID).IntentFor(portal) - intent.LeaveRoom(portal.MXID) -} - func (portal *Portal) Delete() { portal.Portal.Delete() portal.bridge.portalsLock.Lock()