From 87ead90e03266a1b0da83a9ef73e23e218ad14d6 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Wed, 10 Feb 2021 20:27:14 +0200 Subject: [PATCH] Ignore group leaves in message history --- portal.go | 11 ++++++++--- user.go | 13 ++++++++++++- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/portal.go b/portal.go index 4169118..1ca36f6 100644 --- a/portal.go +++ b/portal.go @@ -342,7 +342,7 @@ func (portal *Portal) startHandling(source *User, info whatsapp.MessageInfo) *ap if intent != nil { portal.log.Debugfln("Starting handling of %s (ts: %d)", info.Id, info.Timestamp) } else { - portal.log.Debugfln("Not handling %s: sender is not known") + portal.log.Debugfln("Not handling %s: sender is not known", info.Id) } return intent } @@ -1315,7 +1315,7 @@ func (portal *Portal) HandleStubMessage(source *User, message whatsapp.StubMessa case waProto.WebMessageInfo_GROUP_PARTICIPANT_ADD, waProto.WebMessageInfo_GROUP_PARTICIPANT_INVITE: portal.HandleWhatsAppInvite(senderJID, intent, message.Params) case waProto.WebMessageInfo_GROUP_PARTICIPANT_REMOVE, waProto.WebMessageInfo_GROUP_PARTICIPANT_LEAVE: - portal.HandleWhatsAppKick(senderJID, message.Params) + portal.HandleWhatsAppKick(source, senderJID, message.Params) case waProto.WebMessageInfo_GROUP_PARTICIPANT_PROMOTE: eventID = portal.ChangeAdminStatus(message.Params, true) case waProto.WebMessageInfo_GROUP_PARTICIPANT_DEMOTE: @@ -1485,10 +1485,14 @@ func (portal *Portal) removeUser(isSameUser bool, kicker *appservice.IntentAPI, } } -func (portal *Portal) HandleWhatsAppKick(senderJID string, jids []string) { +func (portal *Portal) HandleWhatsAppKick(source *User, senderJID string, jids []string) { sender := portal.bridge.GetPuppetByJID(senderJID) senderIntent := sender.IntentFor(portal) for _, jid := range jids { + if source != nil && source.JID == jid { + portal.log.Debugln("Ignoring self-kick by", source.MXID) + continue + } puppet := portal.bridge.GetPuppetByJID(jid) portal.removeUser(puppet.JID == sender.JID, senderIntent, puppet.MXID, puppet.DefaultIntent()) @@ -1516,6 +1520,7 @@ func (portal *Portal) HandleWhatsAppInvite(senderJID string, intent *appservice. _, err := intent.InviteUser(portal.MXID, &mautrix.ReqInviteUser{UserID: puppet.MXID}) if err != nil { portal.log.Warnfln("Failed to invite %s as %s: %v", puppet.MXID, intent.UserID, err) + _ = portal.MainIntent().EnsureInvited(portal.MXID, puppet.MXID) } err = puppet.DefaultIntent().EnsureJoined(portal.MXID) if err != nil { diff --git a/user.go b/user.go index 55cdf7e..2bbe64d 100644 --- a/user.go +++ b/user.go @@ -1145,6 +1145,17 @@ func (user *User) HandleChatUpdate(cmd whatsappExt.ChatUpdate) { go portal.UpdateTopic(cmd.Data.AddTopic.Topic, cmd.Data.SenderJID, nil,true) case whatsappExt.ChatActionRemoveTopic: go portal.UpdateTopic("", cmd.Data.SenderJID, nil,true) + case whatsappExt.ChatActionRemove: + // We ignore leaving groups in the message history to avoid accidentally leaving rejoined groups, + // but if we get a real-time command that says we left, it should be safe to bridge it. + if !user.bridge.Config.Bridge.ChatMetaSync { + for _, jid := range cmd.Data.UserChange.JIDs { + if jid == user.JID { + go portal.HandleWhatsAppKick(nil, cmd.Data.SenderJID, cmd.Data.UserChange.JIDs) + break + } + } + } } if !user.bridge.Config.Bridge.ChatMetaSync { @@ -1164,7 +1175,7 @@ func (user *User) HandleChatUpdate(cmd whatsappExt.ChatUpdate) { case whatsappExt.ChatActionRestrict: go portal.RestrictMetadataChanges(cmd.Data.Restrict) case whatsappExt.ChatActionRemove: - go portal.HandleWhatsAppKick(cmd.Data.SenderJID, cmd.Data.UserChange.JIDs) + go portal.HandleWhatsAppKick(nil, cmd.Data.SenderJID, cmd.Data.UserChange.JIDs) case whatsappExt.ChatActionAdd: go portal.HandleWhatsAppInvite(cmd.Data.SenderJID, nil, cmd.Data.UserChange.JIDs) case whatsappExt.ChatActionIntroduce: