Sync push name on message if it got dropped before somehow

This commit is contained in:
Tulir Asokan 2022-05-13 11:34:45 +03:00
parent 1f6cedf948
commit c00f74af3b
4 changed files with 18 additions and 11 deletions

View file

@ -174,6 +174,12 @@ type legacyContactInfo struct {
JID string JID string
} }
const (
NameQualityPush = 3
NameQualityContact = 2
NameQualityPhone = 1
)
func (bc BridgeConfig) FormatDisplayname(jid types.JID, contact types.ContactInfo) (string, int8) { func (bc BridgeConfig) FormatDisplayname(jid types.JID, contact types.ContactInfo) (string, int8) {
var buf strings.Builder var buf strings.Builder
_ = bc.displaynameTemplate.Execute(&buf, legacyContactInfo{ _ = bc.displaynameTemplate.Execute(&buf, legacyContactInfo{
@ -188,11 +194,11 @@ func (bc BridgeConfig) FormatDisplayname(jid types.JID, contact types.ContactInf
var quality int8 var quality int8
switch { switch {
case len(contact.PushName) > 0 || len(contact.BusinessName) > 0: case len(contact.PushName) > 0 || len(contact.BusinessName) > 0:
quality = 3 quality = NameQualityPush
case len(contact.FullName) > 0 || len(contact.FirstName) > 0: case len(contact.FullName) > 0 || len(contact.FirstName) > 0:
quality = 2 quality = NameQualityContact
default: default:
quality = 1 quality = NameQualityPhone
} }
return buf.String(), quality return buf.String(), quality
} }

View file

@ -738,7 +738,7 @@ func (portal *Portal) getMessagePuppet(user *User, info *types.MessageInfo) *Pup
return portal.bridge.GetPuppetByJID(portal.Key.JID) return portal.bridge.GetPuppetByJID(portal.Key.JID)
} else { } else {
puppet := portal.bridge.GetPuppetByJID(info.Sender) puppet := portal.bridge.GetPuppetByJID(info.Sender)
puppet.SyncContact(user, true, "handling message") puppet.SyncContact(user, true, true, "handling message")
return puppet return puppet
} }
} }
@ -809,7 +809,7 @@ func (portal *Portal) SyncParticipants(source *User, metadata *types.GroupInfo)
for _, participant := range metadata.Participants { for _, participant := range metadata.Participants {
participantMap[participant.JID] = true participantMap[participant.JID] = true
puppet := portal.bridge.GetPuppetByJID(participant.JID) 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) user := portal.bridge.GetUserByJID(participant.JID)
if user != nil && user != source { if user != nil && user != source {
portal.ensureUserInvited(user) portal.ensureUserInvited(user)
@ -1194,7 +1194,7 @@ func (portal *Portal) CreateMatrixRoom(user *User, groupInfo *types.GroupInfo, i
//var broadcastMetadata *types.BroadcastListInfo //var broadcastMetadata *types.BroadcastListInfo
if portal.IsPrivateChat() { if portal.IsPrivateChat() {
puppet := portal.bridge.GetPuppetByJID(portal.Key.JID) 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 { if portal.bridge.Config.Bridge.PrivateChatPortalMeta {
portal.Name = puppet.Displayname portal.Name = puppet.Displayname
portal.AvatarURL = puppet.AvatarURL portal.AvatarURL = puppet.AvatarURL
@ -1877,7 +1877,7 @@ func (portal *Portal) HandleWhatsAppInvite(source *User, senderJID *types.JID, j
} }
for _, jid := range jids { for _, jid := range jids {
puppet := portal.bridge.GetPuppetByJID(jid) puppet := portal.bridge.GetPuppetByJID(jid)
puppet.SyncContact(source, true, "handling whatsapp invite") puppet.SyncContact(source, true, false, "handling whatsapp invite")
content := event.Content{ content := event.Content{
Parsed: event.MemberEventContent{ Parsed: event.MemberEventContent{
Membership: "invite", Membership: "invite",

View file

@ -33,6 +33,7 @@ import (
"maunium.net/go/mautrix/appservice" "maunium.net/go/mautrix/appservice"
"maunium.net/go/mautrix/id" "maunium.net/go/mautrix/id"
"maunium.net/go/mautrix-whatsapp/config"
"maunium.net/go/mautrix-whatsapp/database" "maunium.net/go/mautrix-whatsapp/database"
) )
@ -296,8 +297,8 @@ func (puppet *Puppet) updatePortalName() {
}) })
} }
func (puppet *Puppet) SyncContact(source *User, onlyIfNoName bool, reason string) { func (puppet *Puppet) SyncContact(source *User, onlyIfNoName, shouldHavePushName bool, reason string) {
if onlyIfNoName && len(puppet.Displayname) > 0 { if onlyIfNoName && len(puppet.Displayname) > 0 && (!shouldHavePushName || puppet.NameQuality > config.NameQualityPhone) {
return return
} }

View file

@ -955,7 +955,7 @@ func (user *User) GetPortalByJID(jid types.JID) *Portal {
} }
func (user *User) syncPuppet(jid types.JID, reason string) { 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 { 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) { func (user *User) StartPM(jid types.JID, reason string) (*Portal, *Puppet, bool, error) {
user.log.Debugln("Starting PM with", jid, "from", reason) user.log.Debugln("Starting PM with", jid, "from", reason)
puppet := user.bridge.GetPuppetByJID(jid) puppet := user.bridge.GetPuppetByJID(jid)
puppet.SyncContact(user, true, reason) puppet.SyncContact(user, true, false, reason)
portal := user.GetPortalByJID(puppet.JID) portal := user.GetPortalByJID(puppet.JID)
if len(portal.MXID) > 0 { if len(portal.MXID) > 0 {
ok := portal.ensureUserInvited(user) ok := portal.ensureUserInvited(user)