summaryrefslogtreecommitdiffstats
path: root/bridge
diff options
context:
space:
mode:
authorSimon THOBY <git@nightmared.fr>2020-11-25 23:51:23 +0100
committerGitHub <noreply@github.com>2020-11-25 23:51:23 +0100
commit29e29439ee0f10f4485b699633e05da6bcc9e3ce (patch)
treeecb11ee69fabd493d9b0fe4d198525d3cc63fd92 /bridge
parent0c19716f44799fe0ab31b933538bad46c8cce5b9 (diff)
downloadmatterbridge-msglm-29e29439ee0f10f4485b699633e05da6bcc9e3ce.tar.gz
matterbridge-msglm-29e29439ee0f10f4485b699633e05da6bcc9e3ce.tar.bz2
matterbridge-msglm-29e29439ee0f10f4485b699633e05da6bcc9e3ce.zip
Show mxids in case of clashing usernames (matrix) (#1309)
Fixes #1302.
Diffstat (limited to 'bridge')
-rw-r--r--bridge/matrix/helpers.go29
1 files changed, 23 insertions, 6 deletions
diff --git a/bridge/matrix/helpers.go b/bridge/matrix/helpers.go
index 91aea805..8256dc7d 100644
--- a/bridge/matrix/helpers.go
+++ b/bridge/matrix/helpers.go
@@ -3,6 +3,7 @@ package bmatrix
import (
"encoding/json"
"errors"
+ "fmt"
"html"
"strings"
"time"
@@ -82,20 +83,36 @@ func (b *Bmatrix) getDisplayName(mxid string) string {
func (b *Bmatrix) cacheDisplayName(mxid string, displayName string) string {
now := time.Now()
- // scan to delete old entries, to stop memory usage from becoming too high with old entries
+ // scan to delete old entries, to stop memory usage from becoming too high with old entries.
+ // In addition, we also detect if another user have the same username, and if so, we append their mxids to their usernames to differentiate them.
toDelete := []string{}
- b.RLock()
- for k, v := range b.NicknameMap {
+ conflict := false
+
+ b.Lock()
+ for mxid, v := range b.NicknameMap {
+ // to prevent username reuse across matrix servers - or even on the same server, append
+ // the mxid to the username when there is a conflict
+ if v.displayName == displayName {
+ conflict = true
+ // TODO: it would be nice to be able to rename previous messages from this user.
+ // The current behavior is that only users with clashing usernames and *that have spoken since the bridge last started* will get their mxids shown, and I don't know if that's the expected behavior.
+ v.displayName = fmt.Sprintf("%s (%s)", displayName, mxid)
+ b.NicknameMap[mxid] = v
+ }
+
if now.Sub(v.lastUpdated) > 10*time.Minute {
- toDelete = append(toDelete, k)
+ toDelete = append(toDelete, mxid)
}
}
- b.RUnlock()
- b.Lock()
+ if conflict {
+ displayName = fmt.Sprintf("%s (%s)", displayName, mxid)
+ }
+
for _, v := range toDelete {
delete(b.NicknameMap, v)
}
+
b.NicknameMap[mxid] = NicknameCacheEntry{
displayName: displayName,
lastUpdated: now,