浏览代码

Delete portal if user deletes chat on WhatsApp (#531)

vurpo 2 年之前
父节点
当前提交
1600559de7
共有 4 个文件被更改,包括 25 次插入1 次删除
  1. 1 1
      go.mod
  2. 2 0
      go.sum
  3. 17 0
      portal.go
  4. 5 0
      user.go

+ 1 - 1
go.mod

@@ -10,7 +10,7 @@ require (
 	github.com/prometheus/client_golang v1.12.2-0.20220613221938-ebd77f036066
 	github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e
 	github.com/tidwall/gjson v1.14.1
-	go.mau.fi/whatsmeow v0.0.0-20220711111122-6340068d67de
+	go.mau.fi/whatsmeow v0.0.0-20220715134127-5e6b9804193a
 	golang.org/x/image v0.0.0-20220617043117-41969df76e82
 	golang.org/x/net v0.0.0-20220624214902-1bab6f366d9e
 	google.golang.org/protobuf v1.28.0

+ 2 - 0
go.sum

@@ -66,6 +66,8 @@ go.mau.fi/libsignal v0.0.0-20220628090436-4d18b66b087e h1:ByHDg+D+dMIGuBA2n+1xOU
 go.mau.fi/libsignal v0.0.0-20220628090436-4d18b66b087e/go.mod h1:RCdzkTWSJv0AKGqurzPXJsEGIVMuQps3E/h7CMUPous=
 go.mau.fi/whatsmeow v0.0.0-20220711111122-6340068d67de h1:ZrxHSdpUGODtCtq/0A6CXisEgWtcNwK6BGG4f+WVTlM=
 go.mau.fi/whatsmeow v0.0.0-20220711111122-6340068d67de/go.mod h1:hsjqq2xLuoFew8vbsDCJcGf5EbXCRcR/yoQ+87w6m3k=
+go.mau.fi/whatsmeow v0.0.0-20220715134127-5e6b9804193a h1:Ki4Y9d+qW/ED+7gsQ26AlQxUUyZOQ9bQEiVyjTWRmCQ=
+go.mau.fi/whatsmeow v0.0.0-20220715134127-5e6b9804193a/go.mod h1:hsjqq2xLuoFew8vbsDCJcGf5EbXCRcR/yoQ+87w6m3k=
 golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d h1:sK3txAijHtOK88l68nt020reeT1ZdKLIYetKl95FzVY=
 golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
 golang.org/x/image v0.0.0-20220617043117-41969df76e82 h1:KpZB5pUSBvrHltNEdK/tw0xlPeD13M6M6aGP32gKqiw=

+ 17 - 0
portal.go

@@ -2149,6 +2149,7 @@ func (portal *Portal) removeUser(isSameUser bool, kicker *appservice.IntentAPI,
 			_, _ = portal.MainIntent().KickUser(portal.MXID, &mautrix.ReqKickUser{UserID: target})
 		}
 	}
+	portal.CleanupIfEmpty()
 }
 
 func (portal *Portal) HandleWhatsAppKick(source *User, senderJID types.JID, jids []types.JID) {
@@ -2203,6 +2204,22 @@ func (portal *Portal) HandleWhatsAppInvite(source *User, senderJID *types.JID, j
 	return
 }
 
+func (portal *Portal) HandleWhatsAppDeleteChat(user *User) {
+	matrixUsers, err := portal.GetMatrixUsers()
+	if err != nil {
+		portal.log.Errorln("Failed to get Matrix users to see if DeleteChat should be handled:", err)
+		return
+	}
+	if len(matrixUsers) > 1 {
+		portal.log.Infoln("Portal contains more than one Matrix user, so deleteChat will not be handled.")
+		return
+	} else if (len(matrixUsers) == 1 && matrixUsers[0] == user.MXID) || len(matrixUsers) < 1 {
+		portal.log.Debugln("User deleted chat and there are no other Matrix users using it, deleting portal...")
+		portal.Delete()
+		portal.Cleanup(false)
+	}
+}
+
 const failedMediaField = "fi.mau.whatsapp.failed_media"
 
 type FailedMediaKeys struct {

+ 5 - 0
user.go

@@ -926,6 +926,11 @@ func (user *User) HandleEvent(event interface{}) {
 		if portal != nil {
 			portal.deleteForMe(user, v)
 		}
+	case *events.DeleteChat:
+		portal := user.GetPortalByJID(v.JID)
+		if portal != nil {
+			portal.HandleWhatsAppDeleteChat(user)
+		}
 	default:
 		user.log.Debugfln("Unknown type of event in HandleEvent: %T", v)
 	}