Try another algorithm for DM participant deduplication

This commit is contained in:
Tulir Asokan 2024-03-01 13:10:46 +02:00
parent e9ec6b5e64
commit 04828bef39

View file

@ -1269,7 +1269,6 @@ func (portal *Portal) markHandled(cm *ConvertedMessage, eventID id.EventID, medi
func (portal *Portal) SyncParticipants(ctx context.Context, source *User, metadata *gmproto.Conversation) (userIDs []id.UserID, changed bool) { func (portal *Portal) SyncParticipants(ctx context.Context, source *User, metadata *gmproto.Conversation) (userIDs []id.UserID, changed bool) {
filteredParticipants := make([]*gmproto.Participant, 0, len(metadata.Participants)) filteredParticipants := make([]*gmproto.Participant, 0, len(metadata.Participants))
manyContactIDs := false
for _, participant := range metadata.Participants { for _, participant := range metadata.Participants {
if participant.IsMe { if participant.IsMe {
err := source.AddSelfParticipantID(ctx, participant.ID.ParticipantID) err := source.AddSelfParticipantID(ctx, participant.ID.ParticipantID)
@ -1283,27 +1282,36 @@ func (portal *Portal) SyncParticipants(ctx context.Context, source *User, metada
portal.zlog.Warn().Interface("participant", participant).Msg("No number found in non-self participant entry") portal.zlog.Warn().Interface("participant", participant).Msg("No number found in non-self participant entry")
continue continue
} }
if len(filteredParticipants) > 0 && filteredParticipants[0].ContactID != participant.ContactID {
manyContactIDs = true
}
filteredParticipants = append(filteredParticipants, participant) filteredParticipants = append(filteredParticipants, participant)
} }
if len(filteredParticipants) > 1 && !manyContactIDs && !metadata.IsGroupChat { if len(filteredParticipants) > 1 && !metadata.IsGroupChat {
bestParticipant := filteredParticipants[0] var bestParticipant *gmproto.Participant
for _, participant := range filteredParticipants[1:] { var foundMultiple bool
bestNumber := bestParticipant.GetID().GetNumber() for _, participant := range filteredParticipants {
thisNumber := participant.GetID().GetNumber() if participant.GetSomeInt() == 1 && participant.GetFullName() == metadata.GetName() {
// If this number is a substring of the previous number, prefer this one. if bestParticipant != nil {
// Duplicates often have an extra random country code, so we want the one that isn't a substring of the others. foundMultiple = true
if thisNumber != bestNumber && strings.HasPrefix(thisNumber, "+") && strings.Contains(bestNumber, thisNumber[1:]) { break
} else {
bestParticipant = participant bestParticipant = participant
} }
} }
}
if foundMultiple {
portal.zlog.Warn().
Any("participants", filteredParticipants).
Msg("Didn't apply hacky deduplication to DM participants: found multiple matches")
} else if bestParticipant != nil {
portal.zlog.Debug(). portal.zlog.Debug().
Any("participants", filteredParticipants). Any("participants", filteredParticipants).
Any("chosen_participant", bestParticipant). Any("chosen_participant", bestParticipant).
Msg("Applied hacky deduplication to DM participants with same contact ID") Msg("Applied hacky deduplication to DM participants")
filteredParticipants = []*gmproto.Participant{bestParticipant} filteredParticipants = []*gmproto.Participant{bestParticipant}
} else {
portal.zlog.Warn().
Any("participants", filteredParticipants).
Msg("Didn't apply hacky deduplication to DM participants: no match found")
}
} }
for _, participant := range filteredParticipants { for _, participant := range filteredParticipants {
puppet := source.GetPuppetByID(participant.ID.ParticipantID, participant.ID.Number) puppet := source.GetPuppetByID(participant.ID.ParticipantID, participant.ID.Number)