2018-03-18 16:34:42 +01:00
|
|
|
// gomuks - A terminal Matrix client written in Go.
|
2020-04-19 17:10:14 +02:00
|
|
|
// Copyright (C) 2020 Tulir Asokan
|
2018-03-18 16:34:42 +01:00
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
2019-01-17 13:13:25 +01:00
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
2018-03-18 16:34:42 +01:00
|
|
|
// 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
|
2019-01-17 13:13:25 +01:00
|
|
|
// GNU Affero General Public License for more details.
|
2018-03-18 16:34:42 +01:00
|
|
|
//
|
2019-01-17 13:13:25 +01:00
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2018-03-18 16:34:42 +01:00
|
|
|
|
2018-03-21 17:46:19 +01:00
|
|
|
package rooms
|
2018-03-18 16:34:42 +01:00
|
|
|
|
|
|
|
import (
|
2019-06-15 00:11:51 +02:00
|
|
|
"compress/gzip"
|
2019-01-17 13:13:25 +01:00
|
|
|
"encoding/gob"
|
2020-02-22 00:17:52 +01:00
|
|
|
"encoding/json"
|
2018-03-20 20:25:13 +01:00
|
|
|
"fmt"
|
2019-01-17 13:13:25 +01:00
|
|
|
"os"
|
2018-04-23 22:22:18 +02:00
|
|
|
"time"
|
2018-03-20 20:25:13 +01:00
|
|
|
|
2019-04-27 14:02:52 +02:00
|
|
|
sync "github.com/sasha-s/go-deadlock"
|
|
|
|
|
2018-11-13 23:00:35 +01:00
|
|
|
"maunium.net/go/mautrix"
|
2020-04-16 18:27:35 +02:00
|
|
|
"maunium.net/go/mautrix/event"
|
|
|
|
"maunium.net/go/mautrix/id"
|
2019-01-17 13:13:25 +01:00
|
|
|
|
2018-04-24 15:51:40 +02:00
|
|
|
"maunium.net/go/gomuks/debug"
|
2018-03-18 16:34:42 +01:00
|
|
|
)
|
|
|
|
|
2018-05-17 15:29:15 +02:00
|
|
|
func init() {
|
|
|
|
gob.Register(map[string]interface{}{})
|
2019-06-15 00:11:51 +02:00
|
|
|
gob.Register([]interface{}{})
|
2018-05-17 15:29:15 +02:00
|
|
|
}
|
|
|
|
|
2018-04-22 20:05:42 +02:00
|
|
|
type RoomNameSource int
|
|
|
|
|
|
|
|
const (
|
2019-06-15 00:11:51 +02:00
|
|
|
UnknownRoomName RoomNameSource = iota
|
2018-04-22 20:05:42 +02:00
|
|
|
MemberRoomName
|
2019-06-15 00:11:51 +02:00
|
|
|
CanonicalAliasRoomName
|
|
|
|
ExplicitRoomName
|
2018-04-22 20:05:42 +02:00
|
|
|
)
|
|
|
|
|
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.
|
2020-02-22 00:17:52 +01:00
|
|
|
Order json.Number
|
2018-04-24 01:13:17 +02:00
|
|
|
}
|
|
|
|
|
2018-05-16 19:09:09 +02:00
|
|
|
type UnreadMessage struct {
|
2020-04-16 18:27:35 +02:00
|
|
|
EventID id.EventID
|
2018-05-17 15:29:15 +02:00
|
|
|
Counted bool
|
2018-05-16 19:09:09 +02:00
|
|
|
Highlight bool
|
|
|
|
}
|
|
|
|
|
2020-02-22 00:17:52 +01:00
|
|
|
type Member struct {
|
2020-04-19 14:00:49 +02:00
|
|
|
event.MemberEventContent
|
2020-02-22 00:17:52 +01:00
|
|
|
|
|
|
|
// The user who sent the membership event
|
2020-04-16 18:27:35 +02:00
|
|
|
Sender id.UserID `json:"-"`
|
2020-02-22 00:17:52 +01:00
|
|
|
}
|
|
|
|
|
2018-03-18 16:34:42 +01:00
|
|
|
// Room represents a single Matrix room.
|
|
|
|
type Room struct {
|
2019-06-15 00:11:51 +02:00
|
|
|
// The room ID.
|
2020-04-16 18:27:35 +02:00
|
|
|
ID id.RoomID
|
2018-03-18 16:34:42 +01:00
|
|
|
|
2018-04-30 21:28:29 +02:00
|
|
|
// Whether or not the user has left the room.
|
|
|
|
HasLeft bool
|
2020-04-16 18:27:35 +02:00
|
|
|
// Whether or not the room is encrypted.
|
|
|
|
Encrypted bool
|
2018-04-30 21:28:29 +02:00
|
|
|
|
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
|
2020-02-21 23:03:57 +01:00
|
|
|
// The last_batch field from the most recent sync. Used for fetching member lists.
|
|
|
|
LastPrevBatch string
|
2018-03-21 22:29:58 +01:00
|
|
|
// The MXID of the user whose session this room was created for.
|
2020-04-16 18:27:35 +02:00
|
|
|
SessionUserID id.UserID
|
2020-02-22 00:17:52 +01:00
|
|
|
SessionMember *Member
|
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
|
2020-04-16 18:27:35 +02:00
|
|
|
lastMarkedRead id.EventID
|
2018-05-16 19:09:09 +02:00
|
|
|
// Whether or not this room is marked as a direct chat.
|
2020-09-12 23:13:08 +02:00
|
|
|
IsDirect bool
|
|
|
|
OtherUser id.UserID
|
2018-03-26 16:22:47 +02:00
|
|
|
|
2019-06-15 00:11:51 +02:00
|
|
|
// List of tags given to this room.
|
2018-04-24 01:13:17 +02:00
|
|
|
RawTags []RoomTag
|
|
|
|
// Timestamp of previously received actual message.
|
2018-04-23 22:22:18 +02:00
|
|
|
LastReceivedMessage time.Time
|
|
|
|
|
2020-02-21 23:03:57 +01:00
|
|
|
// The lazy loading summary for this room.
|
|
|
|
Summary mautrix.LazyLoadSummary
|
|
|
|
// Whether or not the members for this room have been fetched from the server.
|
|
|
|
MembersFetched bool
|
2019-06-15 00:11:51 +02:00
|
|
|
// Room state cache.
|
2020-04-16 18:27:35 +02:00
|
|
|
state map[event.Type]map[string]*event.Event
|
2018-03-21 22:29:58 +01:00
|
|
|
// MXID -> Member cache calculated from membership events.
|
2020-04-16 18:27:35 +02:00
|
|
|
memberCache map[id.UserID]*Member
|
|
|
|
exMemberCache map[id.UserID]*Member
|
2019-06-15 00:11:51 +02:00
|
|
|
// The first two non-SessionUserID members in the room. Calculated at
|
2018-03-23 13:44:36 +01:00
|
|
|
// the same time as memberCache.
|
2020-02-22 00:17:52 +01:00
|
|
|
firstMemberCache *Member
|
|
|
|
secondMemberCache *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.
|
2019-06-15 00:11:51 +02: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.
|
2020-04-16 18:27:35 +02:00
|
|
|
CanonicalAliasCache id.RoomAlias
|
2020-02-20 00:10:26 +01:00
|
|
|
// Whether or not the room has been tombstoned.
|
|
|
|
replacedCache bool
|
|
|
|
// The room ID that replaced this room.
|
2020-04-16 18:27:35 +02:00
|
|
|
replacedByCache *id.RoomID
|
2018-03-22 16:36:06 +01:00
|
|
|
|
2019-06-15 00:11:51 +02:00
|
|
|
// Path for state store file.
|
|
|
|
path string
|
|
|
|
// Room cache object
|
|
|
|
cache *RoomCache
|
|
|
|
// Lock for state and other room stuff.
|
2019-04-27 14:02:21 +02:00
|
|
|
lock sync.RWMutex
|
2019-06-15 17:03:28 +02:00
|
|
|
// Pre/post un/load hooks
|
|
|
|
preUnload func() bool
|
|
|
|
preLoad func() bool
|
|
|
|
postUnload func()
|
|
|
|
postLoad func()
|
2019-06-15 16:04:08 +02:00
|
|
|
// Whether or not the room state has changed
|
|
|
|
changed bool
|
2019-06-15 00:11:51 +02:00
|
|
|
|
|
|
|
// Room state cache linked list.
|
|
|
|
prev *Room
|
|
|
|
next *Room
|
|
|
|
touch int64
|
2018-03-18 16:34:42 +01:00
|
|
|
}
|
|
|
|
|
2019-06-15 00:11:51 +02:00
|
|
|
func debugPrintError(fn func() error, message string) {
|
|
|
|
if err := fn(); err != nil {
|
|
|
|
debug.Printf("%s: %v", message, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (room *Room) Loaded() bool {
|
|
|
|
return room.state != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (room *Room) Load() {
|
2019-06-15 16:04:08 +02:00
|
|
|
room.cache.TouchNode(room)
|
2019-06-15 00:11:51 +02:00
|
|
|
if room.Loaded() {
|
|
|
|
return
|
2018-05-17 15:29:15 +02:00
|
|
|
}
|
2019-06-15 17:03:28 +02:00
|
|
|
if room.preLoad != nil && !room.preLoad() {
|
2019-06-15 16:04:08 +02:00
|
|
|
return
|
|
|
|
}
|
2019-04-27 14:02:21 +02:00
|
|
|
room.lock.Lock()
|
2019-06-15 00:11:51 +02:00
|
|
|
room.load()
|
|
|
|
room.lock.Unlock()
|
2019-06-16 13:29:03 +02:00
|
|
|
if room.postLoad != nil {
|
|
|
|
room.postLoad()
|
|
|
|
}
|
2018-05-17 15:29:15 +02:00
|
|
|
}
|
|
|
|
|
2019-06-15 00:11:51 +02:00
|
|
|
func (room *Room) load() {
|
|
|
|
if room.Loaded() {
|
|
|
|
return
|
|
|
|
}
|
2019-06-15 16:04:08 +02:00
|
|
|
debug.Print("Loading state for room", room.ID, "from disk")
|
2020-04-16 18:27:35 +02:00
|
|
|
room.state = make(map[event.Type]map[string]*event.Event)
|
2019-06-15 00:11:51 +02:00
|
|
|
file, err := os.OpenFile(room.path, os.O_RDONLY, 0600)
|
2018-05-17 15:29:15 +02:00
|
|
|
if err != nil {
|
2019-06-15 00:11:51 +02:00
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
debug.Print("Failed to open room state file for reading:", err)
|
|
|
|
} else {
|
|
|
|
debug.Print("Room state file for", room.ID, "does not exist")
|
|
|
|
}
|
|
|
|
return
|
2018-05-17 15:29:15 +02:00
|
|
|
}
|
2019-06-15 00:11:51 +02:00
|
|
|
defer debugPrintError(file.Close, "Failed to close room state file after reading")
|
|
|
|
cmpReader, err := gzip.NewReader(file)
|
|
|
|
if err != nil {
|
|
|
|
debug.Print("Failed to open room state gzip reader:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer debugPrintError(cmpReader.Close, "Failed to close room state gzip reader")
|
|
|
|
dec := gob.NewDecoder(cmpReader)
|
|
|
|
if err = dec.Decode(&room.state); err != nil {
|
|
|
|
debug.Print("Failed to decode room state:", err)
|
|
|
|
}
|
2019-06-15 16:04:08 +02:00
|
|
|
room.changed = false
|
2019-06-15 00:11:51 +02:00
|
|
|
}
|
|
|
|
|
2019-06-15 16:04:08 +02:00
|
|
|
func (room *Room) Touch() {
|
|
|
|
room.cache.TouchNode(room)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (room *Room) Unload() bool {
|
2019-06-15 17:03:28 +02:00
|
|
|
if room.preUnload != nil && !room.preUnload() {
|
2019-06-15 16:04:08 +02:00
|
|
|
return false
|
|
|
|
}
|
2019-06-15 00:11:51 +02:00
|
|
|
debug.Print("Unloading", room.ID)
|
|
|
|
room.Save()
|
|
|
|
room.state = nil
|
2020-04-20 21:46:41 +02:00
|
|
|
room.memberCache = nil
|
|
|
|
room.exMemberCache = nil
|
|
|
|
room.firstMemberCache = nil
|
|
|
|
room.secondMemberCache = nil
|
2019-06-15 17:03:28 +02:00
|
|
|
if room.postUnload != nil {
|
|
|
|
room.postUnload()
|
|
|
|
}
|
2019-06-15 16:04:08 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-06-15 17:03:28 +02:00
|
|
|
func (room *Room) SetPreUnload(fn func() bool) {
|
|
|
|
room.preUnload = fn
|
|
|
|
}
|
|
|
|
|
|
|
|
func (room *Room) SetPreLoad(fn func() bool) {
|
|
|
|
room.preLoad = fn
|
|
|
|
}
|
|
|
|
|
|
|
|
func (room *Room) SetPostUnload(fn func()) {
|
|
|
|
room.postUnload = fn
|
2019-06-15 16:04:08 +02:00
|
|
|
}
|
|
|
|
|
2019-06-15 17:03:28 +02:00
|
|
|
func (room *Room) SetPostLoad(fn func()) {
|
|
|
|
room.postLoad = fn
|
2019-06-15 00:11:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (room *Room) Save() {
|
|
|
|
if !room.Loaded() {
|
2019-06-15 16:04:08 +02:00
|
|
|
debug.Print("Failed to save room", room.ID, "state: room not loaded")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !room.changed {
|
|
|
|
debug.Print("Not saving", room.ID, "as state hasn't changed")
|
2019-06-15 00:11:51 +02:00
|
|
|
return
|
|
|
|
}
|
2019-06-15 16:04:08 +02:00
|
|
|
debug.Print("Saving state for room", room.ID, "to disk")
|
2019-06-15 00:11:51 +02:00
|
|
|
file, err := os.OpenFile(room.path, os.O_WRONLY|os.O_CREATE, 0600)
|
|
|
|
if err != nil {
|
|
|
|
debug.Print("Failed to open room state file for writing:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer debugPrintError(file.Close, "Failed to close room state file after writing")
|
|
|
|
cmpWriter := gzip.NewWriter(file)
|
|
|
|
defer debugPrintError(cmpWriter.Close, "Failed to close room state gzip writer")
|
|
|
|
enc := gob.NewEncoder(cmpWriter)
|
2019-04-27 14:02:21 +02:00
|
|
|
room.lock.RLock()
|
|
|
|
defer room.lock.RUnlock()
|
2019-06-15 00:11:51 +02:00
|
|
|
if err := enc.Encode(&room.state); err != nil {
|
|
|
|
debug.Print("Failed to encode room state:", err)
|
|
|
|
}
|
2018-05-17 15:29:15 +02:00
|
|
|
}
|
|
|
|
|
2018-03-26 16:22:47 +02:00
|
|
|
// MarkRead clears the new message statuses on this room.
|
2020-04-16 18:27:35 +02:00
|
|
|
func (room *Room) MarkRead(eventID id.EventID) bool {
|
2019-04-27 14:02:21 +02:00
|
|
|
room.lock.Lock()
|
|
|
|
defer room.lock.Unlock()
|
2019-04-05 22:44:17 +02:00
|
|
|
if room.lastMarkedRead == eventID {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
room.lastMarkedRead = eventID
|
2018-05-16 19:09:09 +02:00
|
|
|
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
|
|
|
|
}
|
2019-04-05 22:44:17 +02:00
|
|
|
return true
|
2018-05-16 19:09:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (room *Room) UnreadCount() int {
|
2019-04-27 14:02:21 +02:00
|
|
|
room.lock.Lock()
|
|
|
|
defer room.lock.Unlock()
|
2018-05-16 19:09:09 +02:00
|
|
|
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 {
|
2019-04-27 14:02:21 +02:00
|
|
|
room.lock.Lock()
|
|
|
|
defer room.lock.Unlock()
|
2018-05-16 19:09:09 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-04-16 18:27:35 +02:00
|
|
|
func (room *Room) AddUnread(eventID id.EventID, counted, highlight bool) {
|
2019-04-27 14:02:21 +02:00
|
|
|
room.lock.Lock()
|
|
|
|
defer room.lock.Unlock()
|
2018-05-16 19:09:09 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-04-16 18:27:35 +02:00
|
|
|
var (
|
|
|
|
tagDirect = RoomTag{"net.maunium.gomuks.fake.direct", "0.5"}
|
|
|
|
tagInvite = RoomTag{"net.maunium.gomuks.fake.invite", "0.5"}
|
|
|
|
tagDefault = RoomTag{"", "0.5"}
|
|
|
|
tagLeave = RoomTag{"net.maunium.gomuks.fake.leave", "0.5"}
|
|
|
|
)
|
|
|
|
|
2018-04-24 01:13:17 +02:00
|
|
|
func (room *Room) Tags() []RoomTag {
|
2019-04-27 14:02:21 +02:00
|
|
|
room.lock.RLock()
|
|
|
|
defer room.lock.RUnlock()
|
2018-04-24 01:13:17 +02:00
|
|
|
if len(room.RawTags) == 0 {
|
2018-05-16 19:09:09 +02:00
|
|
|
if room.IsDirect {
|
2020-04-16 18:27:35 +02:00
|
|
|
return []RoomTag{tagDirect}
|
|
|
|
} else if room.SessionMember != nil && room.SessionMember.Membership == event.MembershipInvite {
|
|
|
|
return []RoomTag{tagInvite}
|
|
|
|
} else if room.SessionMember != nil && room.SessionMember.Membership != event.MembershipJoin {
|
|
|
|
return []RoomTag{tagLeave}
|
2018-05-16 19:09:09 +02:00
|
|
|
}
|
2020-04-16 18:27:35 +02:00
|
|
|
return []RoomTag{tagDefault}
|
2018-04-24 01:13:17 +02:00
|
|
|
}
|
|
|
|
return room.RawTags
|
|
|
|
}
|
|
|
|
|
2020-02-21 23:03:57 +01:00
|
|
|
func (room *Room) UpdateSummary(summary mautrix.LazyLoadSummary) {
|
|
|
|
if summary.JoinedMemberCount != nil {
|
|
|
|
room.Summary.JoinedMemberCount = summary.JoinedMemberCount
|
|
|
|
}
|
|
|
|
if summary.InvitedMemberCount != nil {
|
|
|
|
room.Summary.InvitedMemberCount = summary.InvitedMemberCount
|
|
|
|
}
|
|
|
|
if summary.Heroes != nil {
|
|
|
|
room.Summary.Heroes = summary.Heroes
|
|
|
|
}
|
|
|
|
if room.nameCacheSource <= MemberRoomName {
|
|
|
|
room.NameCache = ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.
|
2020-04-16 18:27:35 +02:00
|
|
|
func (room *Room) UpdateState(evt *event.Event) {
|
|
|
|
if evt.StateKey == nil {
|
2020-02-20 00:10:26 +01:00
|
|
|
panic("Tried to UpdateState() event with no state key.")
|
|
|
|
}
|
2019-06-15 00:11:51 +02:00
|
|
|
room.Load()
|
2019-04-27 14:02:21 +02:00
|
|
|
room.lock.Lock()
|
|
|
|
defer room.lock.Unlock()
|
2019-06-15 16:04:08 +02:00
|
|
|
room.changed = true
|
2020-04-16 18:27:35 +02:00
|
|
|
_, exists := room.state[evt.Type]
|
2018-03-18 16:34:42 +01:00
|
|
|
if !exists {
|
2020-04-16 18:27:35 +02:00
|
|
|
room.state[evt.Type] = make(map[string]*event.Event)
|
2018-03-18 16:34:42 +01:00
|
|
|
}
|
2020-04-19 14:00:49 +02:00
|
|
|
switch content := evt.Content.Parsed.(type) {
|
|
|
|
case *event.RoomNameEventContent:
|
|
|
|
room.NameCache = content.Name
|
2019-06-15 16:04:08 +02:00
|
|
|
room.nameCacheSource = ExplicitRoomName
|
2020-04-19 14:00:49 +02:00
|
|
|
case *event.CanonicalAliasEventContent:
|
2019-06-15 00:11:51 +02:00
|
|
|
if room.nameCacheSource <= CanonicalAliasRoomName {
|
2020-04-19 14:00:49 +02:00
|
|
|
room.NameCache = string(content.Alias)
|
2019-06-15 16:04:08 +02:00
|
|
|
room.nameCacheSource = CanonicalAliasRoomName
|
2018-04-22 20:05:42 +02:00
|
|
|
}
|
2020-04-19 14:00:49 +02:00
|
|
|
room.CanonicalAliasCache = content.Alias
|
|
|
|
case *event.MemberEventContent:
|
2019-06-15 00:11:51 +02:00
|
|
|
if room.nameCacheSource <= MemberRoomName {
|
|
|
|
room.NameCache = ""
|
2018-04-22 20:05:42 +02:00
|
|
|
}
|
2020-04-19 14:00:49 +02:00
|
|
|
room.updateMemberState(id.UserID(evt.GetStateKey()), evt.Sender, content)
|
|
|
|
case *event.TopicEventContent:
|
|
|
|
room.topicCache = content.Topic
|
|
|
|
case *event.EncryptionEventContent:
|
2020-04-26 23:38:04 +02:00
|
|
|
if content.Algorithm == id.AlgorithmMegolmV1 {
|
2020-04-16 19:16:44 +02:00
|
|
|
room.Encrypted = true
|
|
|
|
}
|
2018-03-18 16:34:42 +01:00
|
|
|
}
|
2018-04-24 15:51:40 +02:00
|
|
|
|
2020-04-16 18:27:35 +02:00
|
|
|
if evt.Type != event.StateMember {
|
|
|
|
debug.Printf("Updating state %s#%s for %s", evt.Type.String(), evt.GetStateKey(), room.ID)
|
2018-04-24 15:51:40 +02:00
|
|
|
}
|
|
|
|
|
2020-04-16 18:27:35 +02:00
|
|
|
room.state[evt.Type][*evt.StateKey] = evt
|
2018-03-18 16:34:42 +01:00
|
|
|
}
|
|
|
|
|
2020-04-19 14:00:49 +02:00
|
|
|
func (room *Room) updateMemberState(userID, sender id.UserID, content *event.MemberEventContent) {
|
2019-06-21 22:23:21 +02:00
|
|
|
if userID == room.SessionUserID {
|
2020-04-19 14:00:49 +02:00
|
|
|
debug.Print("Updating session user state:", content)
|
|
|
|
room.SessionMember = room.eventToMember(userID, sender, content)
|
2019-06-21 22:23:21 +02:00
|
|
|
}
|
|
|
|
if room.memberCache != nil {
|
2020-04-19 14:00:49 +02:00
|
|
|
member := room.eventToMember(userID, sender, content)
|
2020-02-21 23:03:57 +01:00
|
|
|
if member.Membership.IsInviteOrJoin() {
|
2019-06-21 22:23:21 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-18 16:34:42 +01:00
|
|
|
// GetStateEvent returns the state event for the given type/state_key combo, or nil.
|
2020-04-16 18:27:35 +02:00
|
|
|
func (room *Room) GetStateEvent(eventType event.Type, stateKey string) *event.Event {
|
2019-06-15 00:11:51 +02:00
|
|
|
room.Load()
|
2019-04-27 14:02:21 +02:00
|
|
|
room.lock.RLock()
|
|
|
|
defer room.lock.RUnlock()
|
2019-06-15 00:11:51 +02:00
|
|
|
stateEventMap, _ := room.state[eventType]
|
2020-04-16 19:16:44 +02:00
|
|
|
evt, _ := stateEventMap[stateKey]
|
|
|
|
return evt
|
2018-03-18 16:34:42 +01:00
|
|
|
}
|
|
|
|
|
2019-04-27 14:02:21 +02:00
|
|
|
// getStateEvents returns the state events for the given type.
|
2020-04-16 18:27:35 +02:00
|
|
|
func (room *Room) getStateEvents(eventType event.Type) map[string]*event.Event {
|
2019-06-15 00:11:51 +02:00
|
|
|
stateEventMap, _ := room.state[eventType]
|
2018-03-18 16:34:42 +01:00
|
|
|
return stateEventMap
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTopic returns the topic of the room.
|
|
|
|
func (room *Room) GetTopic() string {
|
|
|
|
if len(room.topicCache) == 0 {
|
2020-04-16 18:27:35 +02:00
|
|
|
topicEvt := room.GetStateEvent(event.StateTopic, "")
|
2018-03-18 16:34:42 +01:00
|
|
|
if topicEvt != nil {
|
2020-04-19 14:00:49 +02:00
|
|
|
room.topicCache = topicEvt.Content.AsTopic().Topic
|
2018-03-18 16:34:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return room.topicCache
|
|
|
|
}
|
|
|
|
|
2020-04-16 18:27:35 +02:00
|
|
|
func (room *Room) GetCanonicalAlias() id.RoomAlias {
|
2019-06-18 16:31:55 +02:00
|
|
|
if len(room.CanonicalAliasCache) == 0 {
|
2020-04-16 18:27:35 +02:00
|
|
|
canonicalAliasEvt := room.GetStateEvent(event.StateCanonicalAlias, "")
|
2018-04-22 20:05:42 +02:00
|
|
|
if canonicalAliasEvt != nil {
|
2020-04-19 14:00:49 +02:00
|
|
|
room.CanonicalAliasCache = canonicalAliasEvt.Content.AsCanonicalAlias().Alias
|
2018-04-22 20:05:42 +02:00
|
|
|
} else {
|
2019-06-18 16:31:55 +02:00
|
|
|
room.CanonicalAliasCache = "-"
|
2018-04-22 20:05:42 +02:00
|
|
|
}
|
|
|
|
}
|
2019-06-18 16:31:55 +02:00
|
|
|
if room.CanonicalAliasCache == "-" {
|
2018-04-22 20:05:42 +02:00
|
|
|
return ""
|
|
|
|
}
|
2019-06-18 16:31:55 +02:00
|
|
|
return room.CanonicalAliasCache
|
2018-04-22 20:05:42 +02:00
|
|
|
}
|
|
|
|
|
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() {
|
2020-04-16 18:27:35 +02:00
|
|
|
nameEvt := room.GetStateEvent(event.StateRoomName, "")
|
2018-03-20 20:25:13 +01:00
|
|
|
if nameEvt != nil {
|
2020-04-19 14:00:49 +02:00
|
|
|
room.NameCache = nameEvt.Content.AsRoomName().Name
|
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 {
|
2019-06-15 00:11:51 +02:00
|
|
|
room.NameCache = "Empty room"
|
2018-04-22 23:13:41 +02:00
|
|
|
} else if room.firstMemberCache == nil {
|
2019-06-15 00:11:51 +02:00
|
|
|
room.NameCache = "Room"
|
2018-03-20 20:25:13 +01:00
|
|
|
} else if len(members) == 2 {
|
2019-06-15 00:11:51 +02:00
|
|
|
room.NameCache = room.firstMemberCache.Displayname
|
|
|
|
} else if len(members) == 3 && room.secondMemberCache != nil {
|
|
|
|
room.NameCache = fmt.Sprintf("%s and %s", room.firstMemberCache.Displayname, room.secondMemberCache.Displayname)
|
2018-03-20 20:25:13 +01:00
|
|
|
} else {
|
2019-06-15 00:11:51 +02:00
|
|
|
members := room.firstMemberCache.Displayname
|
|
|
|
count := len(members) - 2
|
|
|
|
if room.secondMemberCache != nil {
|
|
|
|
members += ", " + room.secondMemberCache.Displayname
|
|
|
|
count--
|
|
|
|
}
|
|
|
|
room.NameCache = fmt.Sprintf("%s and %d others", members, count)
|
2018-03-20 20:25:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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() {
|
2019-06-15 00:11:51 +02: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
|
|
|
}
|
2019-06-15 00:11:51 +02:00
|
|
|
if len(room.NameCache) == 0 {
|
2020-04-16 18:27:35 +02:00
|
|
|
room.NameCache = string(room.GetCanonicalAlias())
|
2018-04-22 20:05:42 +02:00
|
|
|
room.nameCacheSource = CanonicalAliasRoomName
|
2018-03-20 20:25:13 +01:00
|
|
|
}
|
2019-06-15 00:11:51 +02: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()
|
2019-06-15 00:11:51 +02:00
|
|
|
return room.NameCache
|
2018-03-18 16:34:42 +01:00
|
|
|
}
|
|
|
|
|
2020-02-20 00:10:26 +01:00
|
|
|
func (room *Room) IsReplaced() bool {
|
|
|
|
if room.replacedByCache == nil {
|
2020-04-16 18:27:35 +02:00
|
|
|
evt := room.GetStateEvent(event.StateTombstone, "")
|
|
|
|
var replacement id.RoomID
|
2020-02-20 00:10:26 +01:00
|
|
|
if evt != nil {
|
2020-04-19 14:00:49 +02:00
|
|
|
content, ok := evt.Content.Parsed.(*event.TombstoneEventContent)
|
|
|
|
if ok {
|
|
|
|
replacement = content.ReplacementRoom
|
|
|
|
}
|
2020-02-20 00:10:26 +01:00
|
|
|
}
|
|
|
|
room.replacedCache = evt != nil
|
|
|
|
room.replacedByCache = &replacement
|
|
|
|
}
|
|
|
|
return room.replacedCache
|
|
|
|
}
|
|
|
|
|
2020-04-16 18:27:35 +02:00
|
|
|
func (room *Room) ReplacedBy() id.RoomID {
|
2020-02-20 00:10:26 +01:00
|
|
|
if room.replacedByCache == nil {
|
|
|
|
room.IsReplaced()
|
|
|
|
}
|
|
|
|
return *room.replacedByCache
|
|
|
|
}
|
|
|
|
|
2020-04-19 14:00:49 +02:00
|
|
|
func (room *Room) eventToMember(userID, sender id.UserID, member *event.MemberEventContent) *Member {
|
2019-06-15 16:04:08 +02:00
|
|
|
if len(member.Displayname) == 0 {
|
2020-04-16 18:27:35 +02:00
|
|
|
member.Displayname = string(userID)
|
2019-06-15 16:04:08 +02:00
|
|
|
}
|
2020-02-22 00:17:52 +01:00
|
|
|
return &Member{
|
2020-04-19 14:00:49 +02:00
|
|
|
MemberEventContent: *member,
|
|
|
|
Sender: sender,
|
2020-02-22 00:17:52 +01:00
|
|
|
}
|
2019-06-15 16:04:08 +02:00
|
|
|
}
|
|
|
|
|
2020-04-16 18:27:35 +02:00
|
|
|
func (room *Room) updateNthMemberCache(userID id.UserID, member *Member) {
|
2019-06-15 16:04:08 +02:00
|
|
|
if userID != room.SessionUserID {
|
|
|
|
if room.firstMemberCache == nil {
|
|
|
|
room.firstMemberCache = member
|
|
|
|
} else if room.secondMemberCache == nil {
|
|
|
|
room.secondMemberCache = member
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-20 20:25:13 +01:00
|
|
|
// createMemberCache caches all member events into a easily processable MXID -> *Member map.
|
2020-04-16 18:27:35 +02:00
|
|
|
func (room *Room) createMemberCache() map[id.UserID]*Member {
|
2019-06-15 00:11:51 +02:00
|
|
|
if len(room.memberCache) > 0 {
|
|
|
|
return room.memberCache
|
|
|
|
}
|
2020-04-16 18:27:35 +02:00
|
|
|
cache := make(map[id.UserID]*Member)
|
|
|
|
exCache := make(map[id.UserID]*Member)
|
2019-04-27 14:02:21 +02:00
|
|
|
room.lock.RLock()
|
2020-04-16 18:27:35 +02:00
|
|
|
memberEvents := room.getStateEvents(event.StateMember)
|
2018-04-22 23:13:41 +02:00
|
|
|
room.firstMemberCache = nil
|
2019-06-15 00:11:51 +02:00
|
|
|
room.secondMemberCache = nil
|
2020-04-16 18:27:35 +02:00
|
|
|
if memberEvents != nil {
|
|
|
|
for userIDStr, evt := range memberEvents {
|
|
|
|
userID := id.UserID(userIDStr)
|
2020-04-19 14:00:49 +02:00
|
|
|
member := room.eventToMember(userID, evt.Sender, evt.Content.AsMember())
|
2019-06-21 22:23:21 +02:00
|
|
|
if member.Membership.IsInviteOrJoin() {
|
2018-09-05 09:55:48 +02:00
|
|
|
cache[userID] = member
|
2019-06-15 16:04:08 +02:00
|
|
|
room.updateNthMemberCache(userID, member)
|
2019-06-21 22:23:21 +02:00
|
|
|
} else {
|
|
|
|
exCache[userID] = member
|
2018-03-18 16:34:42 +01:00
|
|
|
}
|
2019-06-16 14:15:30 +02:00
|
|
|
if userID == room.SessionUserID {
|
|
|
|
room.SessionMember = member
|
|
|
|
}
|
2018-03-18 16:34:42 +01:00
|
|
|
}
|
|
|
|
}
|
2020-02-21 23:03:57 +01:00
|
|
|
if len(room.Summary.Heroes) > 1 {
|
|
|
|
room.firstMemberCache, _ = cache[room.Summary.Heroes[0]]
|
|
|
|
}
|
|
|
|
if len(room.Summary.Heroes) > 2 {
|
|
|
|
room.secondMemberCache, _ = cache[room.Summary.Heroes[1]]
|
|
|
|
}
|
2019-04-27 14:02:21 +02:00
|
|
|
room.lock.RUnlock()
|
|
|
|
room.lock.Lock()
|
2018-03-18 16:34:42 +01:00
|
|
|
room.memberCache = cache
|
2020-02-21 23:03:57 +01:00
|
|
|
room.exMemberCache = exCache
|
2019-04-27 14:02:21 +02:00
|
|
|
room.lock.Unlock()
|
2018-03-18 16:34:42 +01:00
|
|
|
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.
|
2020-04-16 18:27:35 +02:00
|
|
|
func (room *Room) GetMembers() map[id.UserID]*Member {
|
2019-06-15 00:11:51 +02:00
|
|
|
room.Load()
|
|
|
|
room.createMemberCache()
|
2018-03-18 16:34:42 +01:00
|
|
|
return room.memberCache
|
|
|
|
}
|
|
|
|
|
2020-04-27 23:58:26 +02:00
|
|
|
func (room *Room) GetMemberList() []id.UserID {
|
|
|
|
members := room.GetMembers()
|
|
|
|
memberList := make([]id.UserID, len(members))
|
|
|
|
index := 0
|
|
|
|
for userID, _ := range members {
|
|
|
|
memberList[index] = userID
|
|
|
|
index++
|
|
|
|
}
|
|
|
|
return memberList
|
|
|
|
}
|
|
|
|
|
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.
|
2020-04-16 18:27:35 +02:00
|
|
|
func (room *Room) GetMember(userID id.UserID) *Member {
|
2019-06-16 14:15:30 +02:00
|
|
|
if userID == room.SessionUserID && room.SessionMember != nil {
|
|
|
|
return room.SessionMember
|
|
|
|
}
|
2019-06-15 00:11:51 +02:00
|
|
|
room.Load()
|
|
|
|
room.createMemberCache()
|
2019-04-27 14:02:21 +02:00
|
|
|
room.lock.RLock()
|
2019-06-21 22:23:21 +02:00
|
|
|
member, ok := room.memberCache[userID]
|
|
|
|
if ok {
|
|
|
|
room.lock.RUnlock()
|
|
|
|
return member
|
|
|
|
}
|
|
|
|
exMember, ok := room.exMemberCache[userID]
|
|
|
|
if ok {
|
|
|
|
room.lock.RUnlock()
|
|
|
|
return exMember
|
|
|
|
}
|
2019-04-27 14:02:21 +02:00
|
|
|
room.lock.RUnlock()
|
2019-06-21 22:23:21 +02:00
|
|
|
return nil
|
2018-03-18 16:34:42 +01:00
|
|
|
}
|
|
|
|
|
2020-04-16 18:27:35 +02:00
|
|
|
func (room *Room) GetMemberCount() int {
|
|
|
|
if room.memberCache == nil && room.Summary.JoinedMemberCount != nil {
|
|
|
|
return *room.Summary.JoinedMemberCount
|
|
|
|
}
|
|
|
|
return len(room.GetMembers())
|
|
|
|
}
|
|
|
|
|
2018-11-14 00:11:40 +01:00
|
|
|
// GetSessionOwner returns the ID of the user whose session this room was created for.
|
2020-04-16 18:27:35 +02:00
|
|
|
func (room *Room) GetOwnDisplayname() string {
|
|
|
|
member := room.GetMember(room.SessionUserID)
|
|
|
|
if member != nil {
|
|
|
|
return member.Displayname
|
|
|
|
}
|
|
|
|
return ""
|
2018-03-22 20:44:46 +01:00
|
|
|
}
|
|
|
|
|
2018-03-18 16:34:42 +01:00
|
|
|
// NewRoom creates a new Room with the given ID
|
2020-04-16 18:27:35 +02:00
|
|
|
func NewRoom(roomID id.RoomID, cache *RoomCache) *Room {
|
2018-03-18 16:34:42 +01:00
|
|
|
return &Room{
|
2019-06-15 00:11:51 +02:00
|
|
|
ID: roomID,
|
2020-04-16 18:27:35 +02:00
|
|
|
state: make(map[event.Type]map[string]*event.Event),
|
2019-06-15 00:11:51 +02:00
|
|
|
path: cache.roomPath(roomID),
|
|
|
|
cache: cache,
|
|
|
|
|
|
|
|
SessionUserID: cache.getOwner(),
|
2018-03-18 16:34:42 +01:00
|
|
|
}
|
|
|
|
}
|