Эх сурвалжийг харах

Get displayname when converting mentions of Matrix users. Fixes #338

Tulir Asokan 3 жил өмнө
parent
commit
e215850fcf
3 өөрчлөгдсөн 14 нэмэгдсэн , 9 устгасан
  1. 1 0
      CHANGELOG.md
  2. 11 7
      formatting.go
  3. 2 2
      portal.go

+ 1 - 0
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

+ 11 - 7
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(`<a href="https://matrix.to/#/%s">%s</a>`, mxid, displayname))
 		content.Body = strings.ReplaceAll(content.Body, number, displayname)

+ 2 - 2
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{