Ignore group leaves in message history

This commit is contained in:
Tulir Asokan 2021-02-10 20:27:14 +02:00
parent db2793c5e1
commit 87ead90e03
2 changed files with 20 additions and 4 deletions

View file

@ -342,7 +342,7 @@ func (portal *Portal) startHandling(source *User, info whatsapp.MessageInfo) *ap
if intent != nil { if intent != nil {
portal.log.Debugfln("Starting handling of %s (ts: %d)", info.Id, info.Timestamp) portal.log.Debugfln("Starting handling of %s (ts: %d)", info.Id, info.Timestamp)
} else { } 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 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: case waProto.WebMessageInfo_GROUP_PARTICIPANT_ADD, waProto.WebMessageInfo_GROUP_PARTICIPANT_INVITE:
portal.HandleWhatsAppInvite(senderJID, intent, message.Params) portal.HandleWhatsAppInvite(senderJID, intent, message.Params)
case waProto.WebMessageInfo_GROUP_PARTICIPANT_REMOVE, waProto.WebMessageInfo_GROUP_PARTICIPANT_LEAVE: 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: case waProto.WebMessageInfo_GROUP_PARTICIPANT_PROMOTE:
eventID = portal.ChangeAdminStatus(message.Params, true) eventID = portal.ChangeAdminStatus(message.Params, true)
case waProto.WebMessageInfo_GROUP_PARTICIPANT_DEMOTE: 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) sender := portal.bridge.GetPuppetByJID(senderJID)
senderIntent := sender.IntentFor(portal) senderIntent := sender.IntentFor(portal)
for _, jid := range jids { 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) puppet := portal.bridge.GetPuppetByJID(jid)
portal.removeUser(puppet.JID == sender.JID, senderIntent, puppet.MXID, puppet.DefaultIntent()) 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}) _, err := intent.InviteUser(portal.MXID, &mautrix.ReqInviteUser{UserID: puppet.MXID})
if err != nil { if err != nil {
portal.log.Warnfln("Failed to invite %s as %s: %v", puppet.MXID, intent.UserID, err) 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) err = puppet.DefaultIntent().EnsureJoined(portal.MXID)
if err != nil { if err != nil {

13
user.go
View file

@ -1145,6 +1145,17 @@ func (user *User) HandleChatUpdate(cmd whatsappExt.ChatUpdate) {
go portal.UpdateTopic(cmd.Data.AddTopic.Topic, cmd.Data.SenderJID, nil,true) go portal.UpdateTopic(cmd.Data.AddTopic.Topic, cmd.Data.SenderJID, nil,true)
case whatsappExt.ChatActionRemoveTopic: case whatsappExt.ChatActionRemoveTopic:
go portal.UpdateTopic("", cmd.Data.SenderJID, nil,true) 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 { if !user.bridge.Config.Bridge.ChatMetaSync {
@ -1164,7 +1175,7 @@ func (user *User) HandleChatUpdate(cmd whatsappExt.ChatUpdate) {
case whatsappExt.ChatActionRestrict: case whatsappExt.ChatActionRestrict:
go portal.RestrictMetadataChanges(cmd.Data.Restrict) go portal.RestrictMetadataChanges(cmd.Data.Restrict)
case whatsappExt.ChatActionRemove: 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: case whatsappExt.ChatActionAdd:
go portal.HandleWhatsAppInvite(cmd.Data.SenderJID, nil, cmd.Data.UserChange.JIDs) go portal.HandleWhatsAppInvite(cmd.Data.SenderJID, nil, cmd.Data.UserChange.JIDs)
case whatsappExt.ChatActionIntroduce: case whatsappExt.ChatActionIntroduce: