From b066eafeac5449699fbda3a755c624bd017d9b86 Mon Sep 17 00:00:00 2001 From: Simon THOBY Date: Fri, 6 Nov 2020 15:31:14 +0100 Subject: [PATCH] matrix: send the display name (the nickname in matrix parlance) instead of the user name There is also the option UseUserName (already in use by the discord bridge) to turn back to the old behavior. --- bridge/config/config.go | 2 +- bridge/matrix/matrix.go | 52 ++++++++++++++++++++++++++++++++++++---- matterbridge.toml.sample | 3 +++ 3 files changed, 52 insertions(+), 5 deletions(-) diff --git a/bridge/config/config.go b/bridge/config/config.go index 67a7dc13..7f8d3a4a 100644 --- a/bridge/config/config.go +++ b/bridge/config/config.go @@ -154,7 +154,7 @@ type Protocol struct { UseTLS bool // IRC UseDiscriminator bool // discord UseFirstName bool // telegram - UseUserName bool // discord + UseUserName bool // discord, matrix UseInsecureURL bool // telegram VerboseJoinPart bool // IRC WebhookBindAddress string // mattermost, slack diff --git a/bridge/matrix/matrix.go b/bridge/matrix/matrix.go index fa2a3f80..edbbbe39 100644 --- a/bridge/matrix/matrix.go +++ b/bridge/matrix/matrix.go @@ -22,10 +22,16 @@ var ( htmlReplacementTag = regexp.MustCompile("<[^>]*>") ) +type NicknameCacheEntry struct { + nickname string + lastUpdated time.Time +} + type Bmatrix struct { - mc *matrix.Client - UserID string - RoomMap map[string]string + mc *matrix.Client + UserID string + NicknameMap map[string]NicknameCacheEntry + RoomMap map[string]string sync.RWMutex *bridge.Config } @@ -60,6 +66,7 @@ func newMatrixUsername(username string) *matrixUsername { func New(cfg *bridge.Config) bridge.Bridger { b := &Bmatrix{Config: cfg} b.RoomMap = make(map[string]string) + b.NicknameMap = make(map[string]NicknameCacheEntry) return b } @@ -296,6 +303,43 @@ func (b *Bmatrix) handleEdit(ev *matrix.Event, rmsg config.Message) bool { return true } +func (b *Bmatrix) getUserName(mxid string) string { + if b.GetBool("UseUserName") { + return mxid[1:] + } + + b.RLock() + if val, present := b.NicknameMap[mxid]; present { + if time.Since(val.lastUpdated) < 10*time.Minute { + b.RUnlock() + return val.nickname + } + } + b.RUnlock() + + nickname, err := b.mc.GetDisplayName(mxid) + if err != nil && err.(matrix.HTTPError).Code != 404 { + b.Log.Warnf("Couldn't retrieve the display name for %s", mxid) + } + + b.Lock() + defer b.Unlock() + + if err != nil { + b.NicknameMap[mxid] = NicknameCacheEntry{ + nickname: mxid[1:], + lastUpdated: time.Now(), + } + } else { + b.NicknameMap[mxid] = NicknameCacheEntry{ + nickname: nickname.DisplayName, + lastUpdated: time.Now(), + } + } + + return b.NicknameMap[mxid].nickname +} + func (b *Bmatrix) handleEvent(ev *matrix.Event) { b.Log.Debugf("== Receiving event: %#v", ev) if ev.Sender != b.UserID { @@ -309,7 +353,7 @@ func (b *Bmatrix) handleEvent(ev *matrix.Event) { // Create our message rmsg := config.Message{ - Username: ev.Sender[1:], + Username: b.getUserName(ev.Sender), Channel: channel, Account: b.Account, UserID: ev.Sender, diff --git a/matterbridge.toml.sample b/matterbridge.toml.sample index 6b38580f..fcaac2a4 100644 --- a/matterbridge.toml.sample +++ b/matterbridge.toml.sample @@ -1231,6 +1231,9 @@ HTMLDisable=false ## RELOADABLE SETTINGS ## Settings below can be reloaded by editing the file +# UseUserName shows the username instead of the server nickname +UseUserName=false + #Whether to prefix messages from other bridges to matrix with the sender's nick. #Useful if username overrides for incoming webhooks isn't enabled on the #matrix server. If you set PrefixMessagesWithNick to true, each message