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
}
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
}

View file

@ -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",

View file

@ -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
}

View file

@ -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)