From c00f74af3b0a9948d6b4c49b5b6a9ad2238b2fc1 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Fri, 13 May 2022 11:34:45 +0300 Subject: [PATCH] Sync push name on message if it got dropped before somehow --- config/bridge.go | 12 +++++++++--- portal.go | 8 ++++---- puppet.go | 5 +++-- user.go | 4 ++-- 4 files changed, 18 insertions(+), 11 deletions(-) diff --git a/config/bridge.go b/config/bridge.go index 1c652c9..3324c38 100644 --- a/config/bridge.go +++ b/config/bridge.go @@ -174,6 +174,12 @@ type legacyContactInfo struct { JID string } +const ( + NameQualityPush = 3 + NameQualityContact = 2 + NameQualityPhone = 1 +) + func (bc BridgeConfig) FormatDisplayname(jid types.JID, contact types.ContactInfo) (string, int8) { var buf strings.Builder _ = bc.displaynameTemplate.Execute(&buf, legacyContactInfo{ @@ -188,11 +194,11 @@ func (bc BridgeConfig) FormatDisplayname(jid types.JID, contact types.ContactInf var quality int8 switch { case len(contact.PushName) > 0 || len(contact.BusinessName) > 0: - quality = 3 + quality = NameQualityPush case len(contact.FullName) > 0 || len(contact.FirstName) > 0: - quality = 2 + quality = NameQualityContact default: - quality = 1 + quality = NameQualityPhone } return buf.String(), quality } diff --git a/portal.go b/portal.go index e28ce3e..8e76172 100644 --- a/portal.go +++ b/portal.go @@ -738,7 +738,7 @@ func (portal *Portal) getMessagePuppet(user *User, info *types.MessageInfo) *Pup return portal.bridge.GetPuppetByJID(portal.Key.JID) } else { puppet := portal.bridge.GetPuppetByJID(info.Sender) - puppet.SyncContact(user, true, "handling message") + puppet.SyncContact(user, true, true, "handling message") return puppet } } @@ -809,7 +809,7 @@ func (portal *Portal) SyncParticipants(source *User, metadata *types.GroupInfo) for _, participant := range metadata.Participants { participantMap[participant.JID] = true puppet := portal.bridge.GetPuppetByJID(participant.JID) - puppet.SyncContact(source, true, "group participant") + puppet.SyncContact(source, true, false, "group participant") user := portal.bridge.GetUserByJID(participant.JID) if user != nil && user != source { portal.ensureUserInvited(user) @@ -1194,7 +1194,7 @@ func (portal *Portal) CreateMatrixRoom(user *User, groupInfo *types.GroupInfo, i //var broadcastMetadata *types.BroadcastListInfo if portal.IsPrivateChat() { puppet := portal.bridge.GetPuppetByJID(portal.Key.JID) - puppet.SyncContact(user, true, "creating private chat portal") + puppet.SyncContact(user, true, false, "creating private chat portal") if portal.bridge.Config.Bridge.PrivateChatPortalMeta { portal.Name = puppet.Displayname portal.AvatarURL = puppet.AvatarURL @@ -1877,7 +1877,7 @@ func (portal *Portal) HandleWhatsAppInvite(source *User, senderJID *types.JID, j } for _, jid := range jids { puppet := portal.bridge.GetPuppetByJID(jid) - puppet.SyncContact(source, true, "handling whatsapp invite") + puppet.SyncContact(source, true, false, "handling whatsapp invite") content := event.Content{ Parsed: event.MemberEventContent{ Membership: "invite", diff --git a/puppet.go b/puppet.go index bb0e8ba..11cbee9 100644 --- a/puppet.go +++ b/puppet.go @@ -33,6 +33,7 @@ import ( "maunium.net/go/mautrix/appservice" "maunium.net/go/mautrix/id" + "maunium.net/go/mautrix-whatsapp/config" "maunium.net/go/mautrix-whatsapp/database" ) @@ -296,8 +297,8 @@ func (puppet *Puppet) updatePortalName() { }) } -func (puppet *Puppet) SyncContact(source *User, onlyIfNoName bool, reason string) { - if onlyIfNoName && len(puppet.Displayname) > 0 { +func (puppet *Puppet) SyncContact(source *User, onlyIfNoName, shouldHavePushName bool, reason string) { + if onlyIfNoName && len(puppet.Displayname) > 0 && (!shouldHavePushName || puppet.NameQuality > config.NameQualityPhone) { return } diff --git a/user.go b/user.go index 0fca9ab..b67886d 100644 --- a/user.go +++ b/user.go @@ -955,7 +955,7 @@ func (user *User) GetPortalByJID(jid types.JID) *Portal { } func (user *User) syncPuppet(jid types.JID, reason string) { - user.bridge.GetPuppetByJID(jid).SyncContact(user, false, reason) + user.bridge.GetPuppetByJID(jid).SyncContact(user, false, false, reason) } func (user *User) ResyncContacts() error { @@ -1124,7 +1124,7 @@ func (user *User) handlePictureUpdate(evt *events.Picture) { func (user *User) StartPM(jid types.JID, reason string) (*Portal, *Puppet, bool, error) { user.log.Debugln("Starting PM with", jid, "from", reason) puppet := user.bridge.GetPuppetByJID(jid) - puppet.SyncContact(user, true, reason) + puppet.SyncContact(user, true, false, reason) portal := user.GetPortalByJID(puppet.JID) if len(portal.MXID) > 0 { ok := portal.ensureUserInvited(user)