瀏覽代碼

Ignore group leaves in message history

Tulir Asokan 4 年之前
父節點
當前提交
87ead90e03
共有 2 個文件被更改,包括 20 次插入4 次删除
  1. 8 3
      portal.go
  2. 12 1
      user.go

+ 8 - 3
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 {

+ 12 - 1
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: