Debian Codegraph @

store.go

raw
1 package mautrix
2
3 import (
4 "maunium.net/go/mautrix/event"
5 "maunium.net/go/mautrix/id"
6 )
7
8 // Storer is an interface which must be satisfied to store client data.
9 //
10 // You can either write a struct which persists this data to disk, or you can use the
11 // provided "InMemoryStore" which just keeps data around in-memory which is lost on
12 // restarts.
13 type Storer interface {
14 SaveFilterID(userID id.UserID, filterID string)
15 LoadFilterID(userID id.UserID) string
16 SaveNextBatch(userID id.UserID, nextBatchToken string)
17 LoadNextBatch(userID id.UserID) string
18 SaveRoom(room *Room)
19 LoadRoom(roomID id.RoomID) *Room
20 }
21
22 // InMemoryStore implements the Storer interface.
23 //
24 // Everything is persisted in-memory as maps. It is not safe to load/save filter IDs
25 // or next batch tokens on any goroutine other than the syncing goroutine: the one
26 // which called Client.Sync().
27 type InMemoryStore struct {
28 Filters map[id.UserID]string
29 NextBatch map[id.UserID]string
30 Rooms map[id.RoomID]*Room
31 }
32
33 // SaveFilterID to memory.
34 func (s *InMemoryStore) SaveFilterID(userID id.UserID, filterID string) {
35 s.Filters[userID] = filterID
36 }
37
38 // LoadFilterID from memory.
39 func (s *InMemoryStore) LoadFilterID(userID id.UserID) string {
40 return s.Filters[userID]
41 }
42
43 // SaveNextBatch to memory.
44 func (s *InMemoryStore) SaveNextBatch(userID id.UserID, nextBatchToken string) {
45 s.NextBatch[userID] = nextBatchToken
46 }
47
48 // LoadNextBatch from memory.
49 func (s *InMemoryStore) LoadNextBatch(userID id.UserID) string {
50 return s.NextBatch[userID]
51 }
52
53 // SaveRoom to memory.
54 func (s *InMemoryStore) SaveRoom(room *Room) {
55 s.Rooms[room.ID] = room
56 }
57
58 // LoadRoom from memory.
59 func (s *InMemoryStore) LoadRoom(roomID id.RoomID) *Room {
60 return s.Rooms[roomID]
61 }
62
63 // UpdateState stores a state event. This can be passed to DefaultSyncer.OnEvent to keep all room state cached.
64 func (s *InMemoryStore) UpdateState(_ EventSource, evt *event.Event) {
65 if !evt.Type.IsState() {
66 return
67 }
68 room := s.LoadRoom(evt.RoomID)
69 if room == nil {
70 room = NewRoom(evt.RoomID)
71 s.SaveRoom(room)
72 }
73 room.UpdateState(evt)
74 }
75
76 // NewInMemoryStore constructs a new InMemoryStore.
77 func NewInMemoryStore() *InMemoryStore {
78 return &InMemoryStore{
79 Filters: make(map[id.UserID]string),
80 NextBatch: make(map[id.UserID]string),
81 Rooms: make(map[id.RoomID]*Room),
82 }
83 }
84
85 // AccountDataStore uses account data to store the next batch token, and
86 // reuses the InMemoryStore for all other operations.
87 type AccountDataStore struct {
88 *InMemoryStore
89 eventType string
90 client *Client
91 }
92
93 type accountData struct {
94 NextBatch string `json:"next_batch"`
95 }
96
97 // SaveNextBatch to account data.
98 func (s *AccountDataStore) SaveNextBatch(userID id.UserID, nextBatchToken string) {
99 if userID.String() != s.client.UserID.String() {
100 panic("AccountDataStore must only be used with bots")
101 }
102
103 data := accountData{
104 NextBatch: nextBatchToken,
105 }
106
107 err := s.client.SetAccountData(s.eventType, data)
108 if err != nil {
109 if s.client.Logger != nil {
110 s.client.Logger.Debugfln("failed to save next batch token to account data: %s", err.Error())
111 }
112 }
113 }
114
115 // LoadNextBatch from account data.
116 func (s *AccountDataStore) LoadNextBatch(userID id.UserID) string {
117 if userID.String() != s.client.UserID.String() {
118 panic("AccountDataStore must only be used with bots")
119 }
120
121 data := &accountData{}
122
123 err := s.client.GetAccountData(s.eventType, data)
124 if err != nil {
125 if s.client.Logger != nil {
126 s.client.Logger.Debugfln("failed to load next batch token to account data: %s", err.Error())
127 }
128 return ""
129 }
130
131 return data.NextBatch
132 }
133
134 // NewAccountDataStore returns a new AccountDataStore, which stores
135 // the next_batch token as a custom event in account data in the
136 // homeserver.
137 //
138 // AccountDataStore is only appropriate for bots, not appservices.
139 //
140 // eventType should be a reversed DNS name like tld.domain.sub.internal and
141 // must be unique for a client. The data stored in it is considered internal
142 // and must not be modified through outside means. You should also add a filter
143 // for account data changes of this event type, to avoid ending up in a sync
144 // loop:
145 //
146 // mautrix.Filter{
147 // AccountData: mautrix.FilterPart{
148 // Limit: 20,
149 // NotTypes: []event.Type{
150 // event.NewEventType(eventType),
151 // },
152 // },
153 // }
154 // mautrix.Client.CreateFilter(...)
155 //
156 func NewAccountDataStore(eventType string, client *Client) *AccountDataStore {
157 return &AccountDataStore{
158 InMemoryStore: NewInMemoryStore(),
159 eventType: eventType,
160 client: client,
161 }
162 }