瀏覽代碼

Delete guild portals too in delete-all-portals

Tulir Asokan 2 年之前
父節點
當前提交
efd22e33b5
共有 3 個文件被更改,包括 27 次插入19 次删除
  1. 16 10
      commands.go
  2. 11 0
      guildportal.go
  3. 0 9
      portal.go

+ 16 - 10
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.")
 

+ 11 - 0
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()
+
+}

+ 0 - 9
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()