2018-03-18 16:34:42 +01:00
|
|
|
// gomuks - A terminal Matrix client written in Go.
|
|
|
|
// Copyright (C) 2018 Tulir Asokan
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2018-03-21 17:46:19 +01:00
|
|
|
package rooms
|
2018-03-18 16:34:42 +01:00
|
|
|
|
|
|
|
import (
|
2018-03-20 20:25:13 +01:00
|
|
|
"fmt"
|
2018-04-22 22:23:30 +02:00
|
|
|
"sort"
|
2018-03-22 16:36:06 +01:00
|
|
|
"sync"
|
2018-04-23 22:22:18 +02:00
|
|
|
"time"
|
2018-03-20 20:25:13 +01:00
|
|
|
|
2018-05-22 16:24:47 +02:00
|
|
|
"encoding/gob"
|
2018-03-18 16:34:42 +01:00
|
|
|
"maunium.net/go/gomatrix"
|
2018-04-24 15:51:40 +02:00
|
|
|
"maunium.net/go/gomuks/debug"
|
2018-05-17 15:29:15 +02:00
|
|
|
"os"
|
2018-03-18 16:34:42 +01:00
|
|
|
)
|
|
|
|
|
2018-05-17 15:29:15 +02:00
|
|
|
func init() {
|
|
|
|
gob.Register([]interface{}{})
|
|
|
|
gob.Register(map[string]interface{}{})
|
|
|
|
}
|
|
|
|
|
2018-04-22 20:05:42 +02:00
|
|
|
type RoomNameSource int
|
|
|
|
|
|
|
|
const (
|
2018-05-22 16:24:47 +02:00
|
|
|
ExplicitRoomName RoomNameSource = iota
|
2018-04-22 20:05:42 +02:00
|
|
|
CanonicalAliasRoomName
|
|
|
|
AliasRoomName
|
|
|
|
MemberRoomName
|
|
|
|
)
|
|
|
|
|
2018-04-24 01:13:17 +02:00
|
|
|
// RoomTag is a tag given to a specific room.
|
|
|
|
type RoomTag struct {
|
|
|
|
// The name of the tag.
|
|
|
|
Tag string
|
2018-05-03 23:55:28 +02:00
|
|
|
// The order of the tag.
|
|
|
|
Order string
|
2018-04-24 01:13:17 +02:00
|
|
|
}
|
|
|
|
|
2018-05-16 19:09:09 +02:00
|
|
|
type UnreadMessage struct {
|
2018-05-17 15:29:15 +02:00
|
|
|
EventID string
|
|
|
|
Counted bool
|
2018-05-16 19:09:09 +02:00
|
|
|
Highlight bool
|
|
|
|
}
|
|
|
|
|
2018-03-18 16:34:42 +01:00
|
|
|
// Room represents a single Matrix room.
|
|
|
|
type Room struct {
|
|
|
|
*gomatrix.Room
|
|
|
|
|
2018-04-30 21:28:29 +02:00
|
|
|
// Whether or not the user has left the room.
|
|
|
|
HasLeft bool
|
|
|
|
|
2018-03-21 22:29:58 +01:00
|
|
|
// The first batch of events that has been fetched for this room.
|
|
|
|
// Used for fetching additional history.
|
2018-03-22 16:36:06 +01:00
|
|
|
PrevBatch string
|
2018-03-21 22:29:58 +01:00
|
|
|
// The MXID of the user whose session this room was created for.
|
2018-03-22 16:36:06 +01:00
|
|
|
SessionUserID string
|
2018-03-26 16:22:47 +02:00
|
|
|
|
|
|
|
// The number of unread messages that were notified about.
|
2018-05-17 15:29:15 +02:00
|
|
|
UnreadMessages []UnreadMessage
|
2018-05-16 19:09:09 +02:00
|
|
|
unreadCountCache *int
|
2018-05-17 15:29:15 +02:00
|
|
|
highlightCache *bool
|
2018-05-16 19:09:09 +02:00
|
|
|
// Whether or not this room is marked as a direct chat.
|
|
|
|
IsDirect bool
|
2018-03-26 16:22:47 +02:00
|
|
|
|
2018-04-24 01:13:17 +02:00
|
|
|
// List of tags given to this room
|
|
|
|
RawTags []RoomTag
|
|
|
|
// Timestamp of previously received actual message.
|
2018-04-23 22:22:18 +02:00
|
|
|
LastReceivedMessage time.Time
|
|
|
|
|
2018-03-21 22:29:58 +01:00
|
|
|
// MXID -> Member cache calculated from membership events.
|
2018-03-22 16:36:06 +01:00
|
|
|
memberCache map[string]*Member
|
2018-03-23 13:44:36 +01:00
|
|
|
// The first non-SessionUserID member in the room. Calculated at
|
|
|
|
// the same time as memberCache.
|
2018-04-22 23:13:41 +02:00
|
|
|
firstMemberCache *Member
|
2018-03-23 13:44:36 +01:00
|
|
|
// The name of the room. Calculated from the state event name,
|
|
|
|
// canonical_alias or alias or the member cache.
|
2018-03-22 16:36:06 +01:00
|
|
|
nameCache string
|
2018-04-22 20:05:42 +02:00
|
|
|
// The event type from which the name cache was calculated from.
|
|
|
|
nameCacheSource RoomNameSource
|
2018-03-21 22:29:58 +01:00
|
|
|
// The topic of the room. Directly fetched from the m.room.topic state event.
|
2018-03-22 16:36:06 +01:00
|
|
|
topicCache string
|
2018-04-22 20:05:42 +02:00
|
|
|
// The canonical alias of the room. Directly fetched from the m.room.canonical_alias state event.
|
|
|
|
canonicalAliasCache string
|
|
|
|
// The list of aliases. Directly fetched from the m.room.aliases state event.
|
|
|
|
aliasesCache []string
|
2018-03-22 16:36:06 +01:00
|
|
|
|
2018-03-23 13:44:36 +01:00
|
|
|
// fetchHistoryLock is used to make sure multiple goroutines don't fetch
|
|
|
|
// history for this room at the same time.
|
2018-03-23 00:07:44 +01:00
|
|
|
fetchHistoryLock *sync.Mutex
|
2018-03-22 16:36:06 +01:00
|
|
|
}
|
|
|
|
|
2018-03-23 13:44:36 +01:00
|
|
|
// LockHistory locks the history fetching mutex.
|
|
|
|
// If the mutex is nil, it will be created.
|
2018-03-22 16:36:06 +01:00
|
|
|
func (room *Room) LockHistory() {
|
|
|
|
if room.fetchHistoryLock == nil {
|
|
|
|
room.fetchHistoryLock = &sync.Mutex{}
|
|
|
|
}
|
|
|
|
room.fetchHistoryLock.Lock()
|
|
|
|
}
|
|
|
|
|
2018-03-23 13:44:36 +01:00
|
|
|
// UnlockHistory unlocks the history fetching mutex.
|
|
|
|
// If the mutex is nil, this does nothing.
|
2018-03-22 16:36:06 +01:00
|
|
|
func (room *Room) UnlockHistory() {
|
|
|
|
if room.fetchHistoryLock != nil {
|
|
|
|
room.fetchHistoryLock.Unlock()
|
|
|
|
}
|
2018-03-18 16:34:42 +01:00
|
|
|
}
|
|
|
|
|
2018-05-17 15:29:15 +02:00
|
|
|
func (room *Room) Load(path string) error {
|
|
|
|
file, err := os.OpenFile(path, os.O_RDONLY, 0600)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
dec := gob.NewDecoder(file)
|
|
|
|
return dec.Decode(room)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (room *Room) Save(path string) error {
|
|
|
|
file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, 0600)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
enc := gob.NewEncoder(file)
|
|
|
|
return enc.Encode(room)
|
|
|
|
}
|
|
|
|
|
2018-03-26 16:22:47 +02:00
|
|
|
// MarkRead clears the new message statuses on this room.
|
2018-05-16 19:09:09 +02:00
|
|
|
func (room *Room) MarkRead(eventID string) {
|
|
|
|
readToIndex := -1
|
|
|
|
for index, unreadMessage := range room.UnreadMessages {
|
|
|
|
if unreadMessage.EventID == eventID {
|
|
|
|
readToIndex = index
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if readToIndex >= 0 {
|
|
|
|
room.UnreadMessages = room.UnreadMessages[readToIndex+1:]
|
|
|
|
room.highlightCache = nil
|
|
|
|
room.unreadCountCache = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (room *Room) UnreadCount() int {
|
|
|
|
if room.unreadCountCache == nil {
|
|
|
|
room.unreadCountCache = new(int)
|
|
|
|
for _, unreadMessage := range room.UnreadMessages {
|
|
|
|
if unreadMessage.Counted {
|
|
|
|
*room.unreadCountCache++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return *room.unreadCountCache
|
|
|
|
}
|
|
|
|
|
|
|
|
func (room *Room) Highlighted() bool {
|
|
|
|
if room.highlightCache == nil {
|
|
|
|
room.highlightCache = new(bool)
|
|
|
|
for _, unreadMessage := range room.UnreadMessages {
|
|
|
|
if unreadMessage.Highlight {
|
|
|
|
*room.highlightCache = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return *room.highlightCache
|
|
|
|
}
|
|
|
|
|
|
|
|
func (room *Room) HasNewMessages() bool {
|
|
|
|
return len(room.UnreadMessages) > 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (room *Room) AddUnread(eventID string, counted, highlight bool) {
|
|
|
|
room.UnreadMessages = append(room.UnreadMessages, UnreadMessage{
|
2018-05-17 15:29:15 +02:00
|
|
|
EventID: eventID,
|
|
|
|
Counted: counted,
|
2018-05-16 19:09:09 +02:00
|
|
|
Highlight: highlight,
|
|
|
|
})
|
|
|
|
if counted {
|
|
|
|
if room.unreadCountCache == nil {
|
|
|
|
room.unreadCountCache = new(int)
|
|
|
|
}
|
|
|
|
*room.unreadCountCache++
|
|
|
|
}
|
|
|
|
if highlight {
|
|
|
|
if room.highlightCache == nil {
|
|
|
|
room.highlightCache = new(bool)
|
|
|
|
}
|
|
|
|
*room.highlightCache = true
|
|
|
|
}
|
2018-03-26 16:22:47 +02:00
|
|
|
}
|
|
|
|
|
2018-04-24 01:13:17 +02:00
|
|
|
func (room *Room) Tags() []RoomTag {
|
|
|
|
if len(room.RawTags) == 0 {
|
2018-05-16 19:09:09 +02:00
|
|
|
if room.IsDirect {
|
|
|
|
return []RoomTag{{"net.maunium.gomuks.fake.direct", "0.5"}}
|
|
|
|
}
|
2018-05-03 23:55:28 +02:00
|
|
|
return []RoomTag{{"", "0.5"}}
|
2018-04-24 01:13:17 +02:00
|
|
|
}
|
|
|
|
return room.RawTags
|
|
|
|
}
|
|
|
|
|
2018-03-18 16:34:42 +01:00
|
|
|
// UpdateState updates the room's current state with the given Event. This will clobber events based
|
|
|
|
// on the type/state_key combination.
|
|
|
|
func (room *Room) UpdateState(event *gomatrix.Event) {
|
|
|
|
_, exists := room.State[event.Type]
|
|
|
|
if !exists {
|
|
|
|
room.State[event.Type] = make(map[string]*gomatrix.Event)
|
|
|
|
}
|
|
|
|
switch event.Type {
|
2018-04-22 20:05:42 +02:00
|
|
|
case "m.room.name":
|
|
|
|
room.nameCache = ""
|
|
|
|
case "m.room.canonical_alias":
|
|
|
|
if room.nameCacheSource >= CanonicalAliasRoomName {
|
|
|
|
room.nameCache = ""
|
|
|
|
}
|
|
|
|
room.canonicalAliasCache = ""
|
|
|
|
case "m.room.aliases":
|
|
|
|
if room.nameCacheSource >= AliasRoomName {
|
|
|
|
room.nameCache = ""
|
|
|
|
}
|
|
|
|
room.aliasesCache = nil
|
2018-03-18 16:34:42 +01:00
|
|
|
case "m.room.member":
|
|
|
|
room.memberCache = nil
|
2018-04-22 23:13:41 +02:00
|
|
|
room.firstMemberCache = nil
|
2018-04-22 20:05:42 +02:00
|
|
|
if room.nameCacheSource >= MemberRoomName {
|
|
|
|
room.nameCache = ""
|
|
|
|
}
|
2018-03-18 16:34:42 +01:00
|
|
|
case "m.room.topic":
|
|
|
|
room.topicCache = ""
|
|
|
|
}
|
2018-04-24 15:51:40 +02:00
|
|
|
|
|
|
|
stateKey := ""
|
|
|
|
if event.StateKey != nil {
|
|
|
|
stateKey = *event.StateKey
|
|
|
|
}
|
|
|
|
if event.Type != "m.room.member" {
|
2018-04-24 16:12:08 +02:00
|
|
|
debug.Printf("Updating state %s#%s for %s", event.Type, stateKey, room.ID)
|
2018-04-24 15:51:40 +02:00
|
|
|
}
|
|
|
|
|
2018-04-22 22:23:30 +02:00
|
|
|
if event.StateKey == nil {
|
|
|
|
room.State[event.Type][""] = event
|
|
|
|
} else {
|
|
|
|
room.State[event.Type][*event.StateKey] = event
|
|
|
|
}
|
2018-03-18 16:34:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetStateEvent returns the state event for the given type/state_key combo, or nil.
|
|
|
|
func (room *Room) GetStateEvent(eventType string, stateKey string) *gomatrix.Event {
|
|
|
|
stateEventMap, _ := room.State[eventType]
|
|
|
|
event, _ := stateEventMap[stateKey]
|
|
|
|
return event
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetStateEvents returns the state events for the given type.
|
|
|
|
func (room *Room) GetStateEvents(eventType string) map[string]*gomatrix.Event {
|
|
|
|
stateEventMap, _ := room.State[eventType]
|
|
|
|
return stateEventMap
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTopic returns the topic of the room.
|
|
|
|
func (room *Room) GetTopic() string {
|
|
|
|
if len(room.topicCache) == 0 {
|
|
|
|
topicEvt := room.GetStateEvent("m.room.topic", "")
|
|
|
|
if topicEvt != nil {
|
|
|
|
room.topicCache, _ = topicEvt.Content["topic"].(string)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return room.topicCache
|
|
|
|
}
|
|
|
|
|
2018-04-22 20:05:42 +02:00
|
|
|
func (room *Room) GetCanonicalAlias() string {
|
|
|
|
if len(room.canonicalAliasCache) == 0 {
|
|
|
|
canonicalAliasEvt := room.GetStateEvent("m.room.canonical_alias", "")
|
|
|
|
if canonicalAliasEvt != nil {
|
|
|
|
room.canonicalAliasCache, _ = canonicalAliasEvt.Content["alias"].(string)
|
|
|
|
} else {
|
|
|
|
room.canonicalAliasCache = "-"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if room.canonicalAliasCache == "-" {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return room.canonicalAliasCache
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAliases returns the list of aliases that point to this room.
|
|
|
|
func (room *Room) GetAliases() []string {
|
|
|
|
if room.aliasesCache == nil {
|
|
|
|
aliasEvents := room.GetStateEvents("m.room.aliases")
|
|
|
|
room.aliasesCache = []string{}
|
|
|
|
for _, event := range aliasEvents {
|
|
|
|
aliases, _ := event.Content["aliases"].([]interface{})
|
|
|
|
|
|
|
|
newAliases := make([]string, len(room.aliasesCache)+len(aliases))
|
|
|
|
copy(newAliases, room.aliasesCache)
|
|
|
|
for index, alias := range aliases {
|
2018-04-22 22:23:30 +02:00
|
|
|
newAliases[len(room.aliasesCache)+index], _ = alias.(string)
|
2018-04-22 20:05:42 +02:00
|
|
|
}
|
|
|
|
room.aliasesCache = newAliases
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return room.aliasesCache
|
|
|
|
}
|
|
|
|
|
2018-03-20 20:25:13 +01:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// updateNameFromAliases updates the room display name to be the first room alias it finds.
|
|
|
|
//
|
2018-04-22 20:05:42 +02:00
|
|
|
// Deprecated: the Client-Server API recommends against using non-canonical aliases as display name.
|
2018-03-20 20:25:13 +01:00
|
|
|
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.
|
2018-04-22 22:23:30 +02:00
|
|
|
aliases := room.GetAliases()
|
|
|
|
if len(aliases) > 0 {
|
|
|
|
sort.Sort(sort.StringSlice(aliases))
|
|
|
|
room.nameCache = aliases[0]
|
2018-03-18 16:34:42 +01:00
|
|
|
}
|
2018-03-20 20:25:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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"
|
2018-04-22 23:13:41 +02:00
|
|
|
} else if room.firstMemberCache == nil {
|
|
|
|
room.nameCache = "Room"
|
2018-03-20 20:25:13 +01:00
|
|
|
} else if len(members) == 2 {
|
2018-04-22 23:13:41 +02:00
|
|
|
room.nameCache = room.firstMemberCache.DisplayName
|
2018-03-20 20:25:13 +01:00
|
|
|
} else {
|
2018-04-22 23:13:41 +02:00
|
|
|
firstMember := room.firstMemberCache.DisplayName
|
2018-03-20 20:25:13 +01:00
|
|
|
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
|
2018-03-21 22:29:58 +01:00
|
|
|
// specified in spec section 11.2.2.5.
|
2018-03-20 20:25:13 +01:00
|
|
|
func (room *Room) updateNameCache() {
|
2018-03-18 16:34:42 +01:00
|
|
|
if len(room.nameCache) == 0 {
|
2018-03-20 20:25:13 +01:00
|
|
|
room.updateNameFromNameEvent()
|
2018-04-22 20:05:42 +02:00
|
|
|
room.nameCacheSource = ExplicitRoomName
|
2018-03-18 16:34:42 +01:00
|
|
|
}
|
|
|
|
if len(room.nameCache) == 0 {
|
2018-04-22 20:05:42 +02:00
|
|
|
room.nameCache = room.GetCanonicalAlias()
|
|
|
|
room.nameCacheSource = CanonicalAliasRoomName
|
2018-03-20 20:25:13 +01:00
|
|
|
}
|
|
|
|
if len(room.nameCache) == 0 {
|
|
|
|
room.updateNameFromAliases()
|
2018-04-22 20:05:42 +02:00
|
|
|
room.nameCacheSource = AliasRoomName
|
2018-03-18 16:34:42 +01:00
|
|
|
}
|
|
|
|
if len(room.nameCache) == 0 {
|
2018-03-20 20:25:13 +01:00
|
|
|
room.updateNameFromMembers()
|
2018-04-22 20:05:42 +02:00
|
|
|
room.nameCacheSource = MemberRoomName
|
2018-03-18 16:34:42 +01:00
|
|
|
}
|
2018-03-20 20:25:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
room.updateNameCache()
|
2018-03-18 16:34:42 +01:00
|
|
|
return room.nameCache
|
|
|
|
}
|
|
|
|
|
2018-03-20 20:25:13 +01:00
|
|
|
// createMemberCache caches all member events into a easily processable MXID -> *Member map.
|
2018-03-18 20:24:03 +01:00
|
|
|
func (room *Room) createMemberCache() map[string]*Member {
|
|
|
|
cache := make(map[string]*Member)
|
2018-03-18 16:34:42 +01:00
|
|
|
events := room.GetStateEvents("m.room.member")
|
2018-04-22 23:13:41 +02:00
|
|
|
room.firstMemberCache = nil
|
2018-03-18 16:34:42 +01:00
|
|
|
if events != nil {
|
|
|
|
for userID, event := range events {
|
|
|
|
member := eventToRoomMember(userID, event)
|
2018-04-22 23:13:41 +02:00
|
|
|
if room.firstMemberCache == nil && userID != room.SessionUserID {
|
|
|
|
room.firstMemberCache = member
|
|
|
|
}
|
2018-03-18 16:34:42 +01:00
|
|
|
if member.Membership != "leave" {
|
|
|
|
cache[member.UserID] = member
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
room.memberCache = cache
|
|
|
|
return cache
|
|
|
|
}
|
|
|
|
|
2018-03-20 20:25:13 +01:00
|
|
|
// GetMembers returns the members in this room.
|
|
|
|
//
|
|
|
|
// The members are returned from the cache.
|
|
|
|
// If the cache is empty, it is updated first.
|
2018-03-18 20:24:03 +01:00
|
|
|
func (room *Room) GetMembers() map[string]*Member {
|
2018-04-22 23:13:41 +02:00
|
|
|
if len(room.memberCache) == 0 || room.firstMemberCache == nil {
|
2018-03-18 16:34:42 +01:00
|
|
|
room.createMemberCache()
|
|
|
|
}
|
|
|
|
return room.memberCache
|
|
|
|
}
|
|
|
|
|
2018-03-20 20:25:13 +01:00
|
|
|
// GetMember returns the member with the given MXID.
|
|
|
|
// If the member doesn't exist, nil is returned.
|
2018-03-18 20:24:03 +01:00
|
|
|
func (room *Room) GetMember(userID string) *Member {
|
2018-03-18 16:34:42 +01:00
|
|
|
if len(room.memberCache) == 0 {
|
|
|
|
room.createMemberCache()
|
|
|
|
}
|
|
|
|
member, _ := room.memberCache[userID]
|
|
|
|
return member
|
|
|
|
}
|
|
|
|
|
2018-03-22 20:44:46 +01:00
|
|
|
// GetSessionOwner returns the Member instance of the user whose session this room was created for.
|
|
|
|
func (room *Room) GetSessionOwner() *Member {
|
|
|
|
return room.GetMember(room.SessionUserID)
|
|
|
|
}
|
|
|
|
|
2018-03-18 16:34:42 +01:00
|
|
|
// NewRoom creates a new Room with the given ID
|
2018-03-20 20:25:13 +01:00
|
|
|
func NewRoom(roomID, owner string) *Room {
|
2018-03-18 16:34:42 +01:00
|
|
|
return &Room{
|
2018-03-22 16:36:06 +01:00
|
|
|
Room: gomatrix.NewRoom(roomID),
|
|
|
|
fetchHistoryLock: &sync.Mutex{},
|
|
|
|
SessionUserID: owner,
|
2018-03-18 16:34:42 +01:00
|
|
|
}
|
|
|
|
}
|