Don't always rejoin; propagate user nicknames

This commit is contained in:
Alex 2020-02-18 18:07:35 +01:00
parent b96f8a2016
commit 85d2c215bb
1 changed files with 22 additions and 7 deletions

View File

@ -27,7 +27,9 @@ type Mattermost struct {
conn *matterclient.MMClient
handlerStopChan chan bool
usermap map[string]string // map username to user id
usermap map[string]string // map username to mm user id
sentjoinedmap map[string]bool // map username/room name to bool
userdisplaynamemap map[UserID]string // map username to last displayname
}
@ -264,6 +266,8 @@ func (mm *Mattermost) Close() {
func (mm *Mattermost) handleConnected() {
mm.handlerStopChan = make(chan bool)
mm.usermap = make(map[string]string)
mm.sentjoinedmap = make(map[string]bool)
mm.userdisplaynamemap = make(map[UserID]string)
go mm.handleLoop(mm.conn.MessageChan, mm.handlerStopChan)
fmt.Printf("Connected to mattermost\n")
@ -323,6 +327,14 @@ func (mm *Mattermost) handlePosted(msg *model.WebSocketEvent) error {
}
userId := UserID(fmt.Sprintf("%s@%s", user.Username, mm.server))
if lastdn, ok := mm.userdisplaynamemap[userId]; !ok || lastdn != user.Nickname {
log.Warnf("Update displayname %s %s", userId, user.Nickname)
mm.handler.UserInfoUpdated(userId, &UserInfo{
DisplayName: user.Nickname,
})
mm.userdisplaynamemap[userId] = user.Nickname
}
// Build message event
msg_ev := &Event{
Author: userId,
@ -348,12 +360,15 @@ func (mm *Mattermost) handlePosted(msg *model.WebSocketEvent) error {
return fmt.Errorf("Invalid channel id")
}
// TODO don't join everytime
mm.handler.Event(&Event{
Author: userId,
Room: roomId,
Type: EVENT_JOIN,
})
cache_key := fmt.Sprintf("%s / %s", userId, roomId)
if _, ok := mm.sentjoinedmap[cache_key]; !ok {
mm.handler.Event(&Event{
Author: userId,
Room: roomId,
Type: EVENT_JOIN,
})
mm.sentjoinedmap[cache_key] = true
}
if post.Type == "system_header_change" {
new_header := post.Props["new_header"].(string)