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.
This commit is contained in:
Simon THOBY
2020-11-06 15:31:14 +01:00
parent cbb46293ab
commit b066eafeac
3 changed files with 52 additions and 5 deletions

View File

@@ -154,7 +154,7 @@ type Protocol struct {
UseTLS bool // IRC UseTLS bool // IRC
UseDiscriminator bool // discord UseDiscriminator bool // discord
UseFirstName bool // telegram UseFirstName bool // telegram
UseUserName bool // discord UseUserName bool // discord, matrix
UseInsecureURL bool // telegram UseInsecureURL bool // telegram
VerboseJoinPart bool // IRC VerboseJoinPart bool // IRC
WebhookBindAddress string // mattermost, slack WebhookBindAddress string // mattermost, slack

View File

@@ -22,10 +22,16 @@ var (
htmlReplacementTag = regexp.MustCompile("<[^>]*>") htmlReplacementTag = regexp.MustCompile("<[^>]*>")
) )
type NicknameCacheEntry struct {
nickname string
lastUpdated time.Time
}
type Bmatrix struct { type Bmatrix struct {
mc *matrix.Client mc *matrix.Client
UserID string UserID string
RoomMap map[string]string NicknameMap map[string]NicknameCacheEntry
RoomMap map[string]string
sync.RWMutex sync.RWMutex
*bridge.Config *bridge.Config
} }
@@ -60,6 +66,7 @@ func newMatrixUsername(username string) *matrixUsername {
func New(cfg *bridge.Config) bridge.Bridger { func New(cfg *bridge.Config) bridge.Bridger {
b := &Bmatrix{Config: cfg} b := &Bmatrix{Config: cfg}
b.RoomMap = make(map[string]string) b.RoomMap = make(map[string]string)
b.NicknameMap = make(map[string]NicknameCacheEntry)
return b return b
} }
@@ -296,6 +303,43 @@ func (b *Bmatrix) handleEdit(ev *matrix.Event, rmsg config.Message) bool {
return true 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) { func (b *Bmatrix) handleEvent(ev *matrix.Event) {
b.Log.Debugf("== Receiving event: %#v", ev) b.Log.Debugf("== Receiving event: %#v", ev)
if ev.Sender != b.UserID { if ev.Sender != b.UserID {
@@ -309,7 +353,7 @@ func (b *Bmatrix) handleEvent(ev *matrix.Event) {
// Create our message // Create our message
rmsg := config.Message{ rmsg := config.Message{
Username: ev.Sender[1:], Username: b.getUserName(ev.Sender),
Channel: channel, Channel: channel,
Account: b.Account, Account: b.Account,
UserID: ev.Sender, UserID: ev.Sender,

View File

@@ -1231,6 +1231,9 @@ HTMLDisable=false
## RELOADABLE SETTINGS ## RELOADABLE SETTINGS
## Settings below can be reloaded by editing the file ## 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. #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 #Useful if username overrides for incoming webhooks isn't enabled on the
#matrix server. If you set PrefixMessagesWithNick to true, each message #matrix server. If you set PrefixMessagesWithNick to true, each message