Finish spec-compliant room naming and add docs for room.go
This commit is contained in:
parent
095b982603
commit
dd009a9dbc
@ -96,7 +96,7 @@ func (s *Session) LoadNextBatch(_ string) string {
|
|||||||
func (s *Session) GetRoom(mxid string) *rooms.Room {
|
func (s *Session) GetRoom(mxid string) *rooms.Room {
|
||||||
room, _ := s.Rooms[mxid]
|
room, _ := s.Rooms[mxid]
|
||||||
if room == nil {
|
if room == nil {
|
||||||
room = rooms.NewRoom(mxid)
|
room = rooms.NewRoom(mxid, s.MXID)
|
||||||
s.Rooms[room.ID] = room
|
s.Rooms[room.ID] = room
|
||||||
}
|
}
|
||||||
return room
|
return room
|
||||||
|
@ -17,6 +17,8 @@
|
|||||||
package room
|
package room
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
"maunium.net/go/gomatrix"
|
"maunium.net/go/gomatrix"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -24,10 +26,12 @@ import (
|
|||||||
type Room struct {
|
type Room struct {
|
||||||
*gomatrix.Room
|
*gomatrix.Room
|
||||||
|
|
||||||
PrevBatch string
|
PrevBatch string
|
||||||
memberCache map[string]*Member
|
Owner string
|
||||||
nameCache string
|
memberCache map[string]*Member
|
||||||
topicCache string
|
firstMemberCache string
|
||||||
|
nameCache string
|
||||||
|
topicCache string
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpdateState updates the room's current state with the given Event. This will clobber events based
|
// UpdateState updates the room's current state with the given Event. This will clobber events based
|
||||||
@ -40,8 +44,12 @@ func (room *Room) UpdateState(event *gomatrix.Event) {
|
|||||||
switch event.Type {
|
switch event.Type {
|
||||||
case "m.room.member":
|
case "m.room.member":
|
||||||
room.memberCache = nil
|
room.memberCache = nil
|
||||||
|
room.firstMemberCache = ""
|
||||||
|
fallthrough
|
||||||
case "m.room.name":
|
case "m.room.name":
|
||||||
|
fallthrough
|
||||||
case "m.room.canonical_alias":
|
case "m.room.canonical_alias":
|
||||||
|
fallthrough
|
||||||
case "m.room.alias":
|
case "m.room.alias":
|
||||||
room.nameCache = ""
|
room.nameCache = ""
|
||||||
case "m.room.topic":
|
case "m.room.topic":
|
||||||
@ -74,44 +82,94 @@ func (room *Room) GetTopic() string {
|
|||||||
return room.topicCache
|
return room.topicCache
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetTitle returns the display title of the room.
|
// updateNameFromNameEvent updates the room display name to be the name set in the name event.
|
||||||
|
func (room *Room) updateNameFromNameEvent() {
|
||||||
|
nameEvt := room.GetStateEvent("m.room.name", "")
|
||||||
|
if nameEvt != nil {
|
||||||
|
room.nameCache, _ = nameEvt.Content["name"].(string)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// updateNameFromCanonicalAlias updates the room display name to be the canonical alias of the room.
|
||||||
|
func (room *Room) updateNameFromCanonicalAlias() {
|
||||||
|
canonicalAliasEvt := room.GetStateEvent("m.room.canonical_alias", "")
|
||||||
|
if canonicalAliasEvt != nil {
|
||||||
|
room.nameCache, _ = canonicalAliasEvt.Content["alias"].(string)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// updateNameFromAliases updates the room display name to be the first room alias it finds.
|
||||||
|
//
|
||||||
|
// Deprecated: the Client-Server API recommends against using aliases as display name.
|
||||||
|
func (room *Room) updateNameFromAliases() {
|
||||||
|
// TODO the spec says clients should not use m.room.aliases for room names.
|
||||||
|
// However, Riot also uses m.room.aliases, so this is here now.
|
||||||
|
aliasEvents := room.GetStateEvents("m.room.aliases")
|
||||||
|
for _, event := range aliasEvents {
|
||||||
|
aliases, _ := event.Content["aliases"].([]interface{})
|
||||||
|
if len(aliases) > 0 {
|
||||||
|
room.nameCache, _ = aliases[0].(string)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// updateNameFromMembers updates the room display name based on the members in this room.
|
||||||
|
//
|
||||||
|
// The room name depends on the number of users:
|
||||||
|
// Less than two users -> "Empty room"
|
||||||
|
// Exactly two users -> The display name of the other user.
|
||||||
|
// More than two users -> The display name of one of the other users, followed
|
||||||
|
// by "and X others", where X is the number of users
|
||||||
|
// excluding the local user and the named user.
|
||||||
|
func (room *Room) updateNameFromMembers() {
|
||||||
|
members := room.GetMembers()
|
||||||
|
if len(members) <= 1 {
|
||||||
|
room.nameCache = "Empty room"
|
||||||
|
} else if len(members) == 2 {
|
||||||
|
room.nameCache = members[room.firstMemberCache].DisplayName
|
||||||
|
} else {
|
||||||
|
firstMember := members[room.firstMemberCache].DisplayName
|
||||||
|
room.nameCache = fmt.Sprintf("%s and %d others", firstMember, len(members)-2)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// updateNameCache updates the room display name based on the room state in the order
|
||||||
|
// specified in section 11.2.2.5 of r0.3.0 of the Client-Server API specification.
|
||||||
|
func (room *Room) updateNameCache() {
|
||||||
|
if len(room.nameCache) == 0 {
|
||||||
|
room.updateNameFromNameEvent()
|
||||||
|
}
|
||||||
|
if len(room.nameCache) == 0 {
|
||||||
|
room.updateNameFromCanonicalAlias()
|
||||||
|
}
|
||||||
|
if len(room.nameCache) == 0 {
|
||||||
|
room.updateNameFromAliases()
|
||||||
|
}
|
||||||
|
if len(room.nameCache) == 0 {
|
||||||
|
room.updateNameFromMembers()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetTitle returns the display name of the room.
|
||||||
|
//
|
||||||
|
// The display name is returned from the cache.
|
||||||
|
// If the cache is empty, it is updated first.
|
||||||
func (room *Room) GetTitle() string {
|
func (room *Room) GetTitle() string {
|
||||||
if len(room.nameCache) == 0 {
|
room.updateNameCache()
|
||||||
nameEvt := room.GetStateEvent("m.room.name", "")
|
|
||||||
if nameEvt != nil {
|
|
||||||
room.nameCache, _ = nameEvt.Content["name"].(string)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if len(room.nameCache) == 0 {
|
|
||||||
canonicalAliasEvt := room.GetStateEvent("m.room.canonical_alias", "")
|
|
||||||
if canonicalAliasEvt != nil {
|
|
||||||
room.nameCache, _ = canonicalAliasEvt.Content["alias"].(string)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if len(room.nameCache) == 0 {
|
|
||||||
// TODO the spec says clients should not use m.room.aliases for room names.
|
|
||||||
// However, Riot also uses m.room.aliases, so this is here now.
|
|
||||||
aliasEvents := room.GetStateEvents("m.room.aliases")
|
|
||||||
for _, event := range aliasEvents {
|
|
||||||
aliases, _ := event.Content["aliases"].([]interface{})
|
|
||||||
if len(aliases) > 0 {
|
|
||||||
room.nameCache, _ = aliases[0].(string)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if len(room.nameCache) == 0 {
|
|
||||||
// TODO follow other title rules in spec
|
|
||||||
room.nameCache = room.ID
|
|
||||||
}
|
|
||||||
return room.nameCache
|
return room.nameCache
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// createMemberCache caches all member events into a easily processable MXID -> *Member map.
|
||||||
func (room *Room) createMemberCache() map[string]*Member {
|
func (room *Room) createMemberCache() map[string]*Member {
|
||||||
cache := make(map[string]*Member)
|
cache := make(map[string]*Member)
|
||||||
events := room.GetStateEvents("m.room.member")
|
events := room.GetStateEvents("m.room.member")
|
||||||
|
room.firstMemberCache = ""
|
||||||
if events != nil {
|
if events != nil {
|
||||||
for userID, event := range events {
|
for userID, event := range events {
|
||||||
|
if len(room.firstMemberCache) == 0 && userID != room.Owner {
|
||||||
|
room.firstMemberCache = userID
|
||||||
|
}
|
||||||
member := eventToRoomMember(userID, event)
|
member := eventToRoomMember(userID, event)
|
||||||
if member.Membership != "leave" {
|
if member.Membership != "leave" {
|
||||||
cache[member.UserID] = member
|
cache[member.UserID] = member
|
||||||
@ -122,6 +180,10 @@ func (room *Room) createMemberCache() map[string]*Member {
|
|||||||
return cache
|
return cache
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetMembers returns the members in this room.
|
||||||
|
//
|
||||||
|
// The members are returned from the cache.
|
||||||
|
// If the cache is empty, it is updated first.
|
||||||
func (room *Room) GetMembers() map[string]*Member {
|
func (room *Room) GetMembers() map[string]*Member {
|
||||||
if len(room.memberCache) == 0 {
|
if len(room.memberCache) == 0 {
|
||||||
room.createMemberCache()
|
room.createMemberCache()
|
||||||
@ -129,6 +191,8 @@ func (room *Room) GetMembers() map[string]*Member {
|
|||||||
return room.memberCache
|
return room.memberCache
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetMember returns the member with the given MXID.
|
||||||
|
// If the member doesn't exist, nil is returned.
|
||||||
func (room *Room) GetMember(userID string) *Member {
|
func (room *Room) GetMember(userID string) *Member {
|
||||||
if len(room.memberCache) == 0 {
|
if len(room.memberCache) == 0 {
|
||||||
room.createMemberCache()
|
room.createMemberCache()
|
||||||
@ -138,8 +202,9 @@ func (room *Room) GetMember(userID string) *Member {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NewRoom creates a new Room with the given ID
|
// NewRoom creates a new Room with the given ID
|
||||||
func NewRoom(roomID string) *Room {
|
func NewRoom(roomID, owner string) *Room {
|
||||||
return &Room{
|
return &Room{
|
||||||
Room: gomatrix.NewRoom(roomID),
|
Room: gomatrix.NewRoom(roomID),
|
||||||
|
Owner: owner,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user