diff --git a/CHANGELOG.md b/CHANGELOG.md index d0a8e42..7db3253 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ * Added initial support for re-fetching old media from phone. * Added support for bridging audio message waveforms in both directions. * Added support for sending URL previews to WhatsApp (both custom and autogenerated). +* Updated formatter to get Matrix user displayname when converting WhatsApp mentions. * Fixed some issues with read receipt bridging * Fixed `!wa open` not working with new-style group IDs. * Fixed panic in disappearing message handling code if a portal is deleted with diff --git a/formatting.go b/formatting.go index f1afac7..9ff9144 100644 --- a/formatting.go +++ b/formatting.go @@ -93,18 +93,22 @@ func NewFormatter(bridge *Bridge) *Formatter { return formatter } -func (formatter *Formatter) getMatrixInfoByJID(jid types.JID) (mxid id.UserID, displayname string) { - if user := formatter.bridge.GetUserByJID(jid); user != nil { - mxid = user.MXID - displayname = string(user.MXID) - } else if puppet := formatter.bridge.GetPuppetByJID(jid); puppet != nil { +func (formatter *Formatter) getMatrixInfoByJID(roomID id.RoomID, jid types.JID) (mxid id.UserID, displayname string) { + if puppet := formatter.bridge.GetPuppetByJID(jid); puppet != nil { mxid = puppet.MXID displayname = puppet.Displayname } + if user := formatter.bridge.GetUserByJID(jid); user != nil { + mxid = user.MXID + member := formatter.bridge.StateStore.GetMember(roomID, user.MXID) + if len(member.Displayname) > 0 { + displayname = member.Displayname + } + } return } -func (formatter *Formatter) ParseWhatsApp(content *event.MessageEventContent, mentionedJIDs []string) { +func (formatter *Formatter) ParseWhatsApp(roomID id.RoomID, content *event.MessageEventContent, mentionedJIDs []string) { output := html.EscapeString(content.Body) for regex, replacement := range formatter.waReplString { output = regex.ReplaceAllString(output, replacement) @@ -119,7 +123,7 @@ func (formatter *Formatter) ParseWhatsApp(content *event.MessageEventContent, me } else if jid.Server == types.LegacyUserServer { jid.Server = types.DefaultUserServer } - mxid, displayname := formatter.getMatrixInfoByJID(jid) + mxid, displayname := formatter.getMatrixInfoByJID(roomID, jid) number := "@" + jid.User output = strings.ReplaceAll(output, number, fmt.Sprintf(`%s`, mxid, displayname)) content.Body = strings.ReplaceAll(content.Body, number, displayname) diff --git a/portal.go b/portal.go index e6c4694..7523cb5 100644 --- a/portal.go +++ b/portal.go @@ -1517,7 +1517,7 @@ func (portal *Portal) convertTextMessage(intent *appservice.IntentAPI, source *U } contextInfo := msg.GetExtendedTextMessage().GetContextInfo() - portal.bridge.Formatter.ParseWhatsApp(content, contextInfo.GetMentionedJid()) + portal.bridge.Formatter.ParseWhatsApp(portal.MXID, content, contextInfo.GetMentionedJid()) replyTo := contextInfo.GetStanzaId() expiresIn := contextInfo.GetExpiration() extraAttrs := map[string]interface{}{} @@ -2002,7 +2002,7 @@ func (portal *Portal) convertMediaMessageContent(intent *appservice.IntentAPI, m MsgType: event.MsgNotice, } - portal.bridge.Formatter.ParseWhatsApp(captionContent, msg.GetContextInfo().GetMentionedJid()) + portal.bridge.Formatter.ParseWhatsApp(portal.MXID, captionContent, msg.GetContextInfo().GetMentionedJid()) } return &ConvertedMessage{