From 1600559de7931045b95312b7c1dfa4bb99454d6f Mon Sep 17 00:00:00 2001 From: vurpo Date: Mon, 18 Jul 2022 16:16:17 +0300 Subject: [PATCH] Delete portal if user deletes chat on WhatsApp (#531) --- go.mod | 2 +- go.sum | 2 ++ portal.go | 17 +++++++++++++++++ user.go | 5 +++++ 4 files changed, 25 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index b163a68..397780d 100644 --- a/go.mod +++ b/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 diff --git a/go.sum b/go.sum index e5eb346..a8fa04e 100644 --- a/go.sum +++ b/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= diff --git a/portal.go b/portal.go index 9c48b8c..3dfde55 100644 --- a/portal.go +++ b/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 { diff --git a/user.go b/user.go index ab1d511..05dac07 100644 --- a/user.go +++ b/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) }