Possibly fix displaying displaynames of left users

This commit is contained in:
Tulir Asokan 2019-06-21 23:23:21 +03:00
parent 772f30b2d6
commit f8a8905872
3 changed files with 52 additions and 23 deletions

2
go.mod
View File

@ -25,7 +25,7 @@ require (
gopkg.in/russross/blackfriday.v2 v2.0.1
gopkg.in/toast.v1 v1.0.0-20180812000517-0a84660828b2
gopkg.in/yaml.v2 v2.2.2
maunium.net/go/mautrix v0.1.0-alpha.3.0.20190617092349-2e611350d5a9
maunium.net/go/mautrix v0.1.0-alpha.3.0.20190621202236-a465f0da5c5e
maunium.net/go/mauview v0.0.0-20190606152754-de9e0a754a5d
maunium.net/go/tcell v0.0.0-20190606152714-9a88fc07b3ed
)

2
go.sum
View File

@ -102,6 +102,8 @@ maunium.net/go/mautrix v0.1.0-alpha.3.0.20190616174145-055ff7b0fa6a h1:DenEIDOOu
maunium.net/go/mautrix v0.1.0-alpha.3.0.20190616174145-055ff7b0fa6a/go.mod h1:O+QWJP3H7BZEzIBSrECKpnpRnEKBwaoWVEu/yZwVwxg=
maunium.net/go/mautrix v0.1.0-alpha.3.0.20190617092349-2e611350d5a9 h1:GB0c9wXX4JrFUuPft1KGvUU1RKjM04XYQp0Uaj2K3bU=
maunium.net/go/mautrix v0.1.0-alpha.3.0.20190617092349-2e611350d5a9/go.mod h1:O+QWJP3H7BZEzIBSrECKpnpRnEKBwaoWVEu/yZwVwxg=
maunium.net/go/mautrix v0.1.0-alpha.3.0.20190621202236-a465f0da5c5e h1:VIFW1Rr74XTQHYMOrxnK5c99GeKE4xkhPz7N+N8xVJ4=
maunium.net/go/mautrix v0.1.0-alpha.3.0.20190621202236-a465f0da5c5e/go.mod h1:O+QWJP3H7BZEzIBSrECKpnpRnEKBwaoWVEu/yZwVwxg=
maunium.net/go/mauview v0.0.0-20190606152754-de9e0a754a5d h1:H4wZ4vMVnOh5QFsb4xZtssgpv3DDEkBRzQ8iyEg2fX0=
maunium.net/go/mauview v0.0.0-20190606152754-de9e0a754a5d/go.mod h1:GL+akv58wNFzzX4IKLvryKx0F/AcYKHql35DiBzBc/w=
maunium.net/go/tcell v0.0.0-20190606152714-9a88fc07b3ed h1:sAcUrUZG2LFWBTkTtLKPQvHPHFM5d6huAhr5ZZuxtbQ=

View File

@ -91,7 +91,8 @@ type Room struct {
// Room state cache.
state map[mautrix.EventType]map[string]*mautrix.Event
// MXID -> Member cache calculated from membership events.
memberCache map[string]*mautrix.Member
memberCache map[string]*mautrix.Member
exMemberCache map[string]*mautrix.Member
// The first two non-SessionUserID members in the room. Calculated at
// the same time as memberCache.
firstMemberCache *mautrix.Member
@ -199,6 +200,8 @@ func (room *Room) Unload() bool {
room.CanonicalAliasCache = ""
room.firstMemberCache = nil
room.secondMemberCache = nil
room.memberCache = nil
room.exMemberCache = nil
if room.postUnload != nil {
room.postUnload()
}
@ -363,27 +366,10 @@ func (room *Room) UpdateState(event *mautrix.Event) {
}
room.aliasesCache = nil
case mautrix.StateMember:
userID := event.GetStateKey()
if userID == room.SessionUserID {
room.SessionMember = room.eventToMember(userID, &event.Content)
}
if room.memberCache != nil {
if event.Content.Membership == mautrix.MembershipLeave || event.Content.Membership == mautrix.MembershipBan {
delete(room.memberCache, userID)
} else if event.Content.Membership == mautrix.MembershipInvite || event.Content.Membership == mautrix.MembershipJoin {
member := room.eventToMember(userID, &event.Content)
existingMember, ok := room.memberCache[userID]
if ok {
*existingMember = *member
} else {
room.memberCache[userID] = member
room.updateNthMemberCache(userID, member)
}
}
}
if room.nameCacheSource <= MemberRoomName {
room.NameCache = ""
}
room.updateMemberState(event)
case mautrix.StateTopic:
room.topicCache = event.Content.Topic
}
@ -399,6 +385,34 @@ func (room *Room) UpdateState(event *mautrix.Event) {
}
}
func (room *Room) updateMemberState(event *mautrix.Event) {
userID := event.GetStateKey()
if userID == room.SessionUserID {
room.SessionMember = room.eventToMember(userID, &event.Content)
}
if room.memberCache != nil {
member := room.eventToMember(userID, &event.Content)
if event.Content.Membership.IsInviteOrJoin() {
existingMember, ok := room.memberCache[userID]
if ok {
*existingMember = *member
} else {
delete(room.exMemberCache, userID)
room.memberCache[userID] = member
room.updateNthMemberCache(userID, member)
}
} else {
existingExMember, ok := room.exMemberCache[userID]
if ok {
*existingExMember = *member
} else {
delete(room.memberCache, userID)
room.exMemberCache[userID] = member
}
}
}
}
// GetStateEvent returns the state event for the given type/state_key combo, or nil.
func (room *Room) GetStateEvent(eventType mautrix.EventType, stateKey string) *mautrix.Event {
room.Load()
@ -560,6 +574,7 @@ func (room *Room) createMemberCache() map[string]*mautrix.Member {
return room.memberCache
}
cache := make(map[string]*mautrix.Member)
exCache := make(map[string]*mautrix.Member)
room.lock.RLock()
events := room.getStateEvents(mautrix.StateMember)
room.firstMemberCache = nil
@ -567,9 +582,11 @@ func (room *Room) createMemberCache() map[string]*mautrix.Member {
if events != nil {
for userID, event := range events {
member := room.eventToMember(userID, &event.Content)
if member.Membership == mautrix.MembershipJoin || member.Membership == mautrix.MembershipInvite {
if member.Membership.IsInviteOrJoin() {
cache[userID] = member
room.updateNthMemberCache(userID, member)
} else {
exCache[userID] = member
}
if userID == room.SessionUserID {
room.SessionMember = member
@ -579,6 +596,7 @@ func (room *Room) createMemberCache() map[string]*mautrix.Member {
room.lock.RUnlock()
room.lock.Lock()
room.memberCache = cache
room.exMemberCache = cache
room.lock.Unlock()
return cache
}
@ -602,9 +620,18 @@ func (room *Room) GetMember(userID string) *mautrix.Member {
room.Load()
room.createMemberCache()
room.lock.RLock()
member, _ := room.memberCache[userID]
member, ok := room.memberCache[userID]
if ok {
room.lock.RUnlock()
return member
}
exMember, ok := room.exMemberCache[userID]
if ok {
room.lock.RUnlock()
return exMember
}
room.lock.RUnlock()
return member
return nil
}
// GetSessionOwner returns the ID of the user whose session this room was created for.