1package gomatrix
2
3// Storer is an interface which must be satisfied to store client data.
4//
5// You can either write a struct which persists this data to disk, or you can use the
6// provided "InMemoryStore" which just keeps data around in-memory which is lost on
7// restarts.
8type Storer interface {
9	SaveFilterID(userID, filterID string)
10	LoadFilterID(userID string) string
11	SaveNextBatch(userID, nextBatchToken string)
12	LoadNextBatch(userID string) string
13	SaveRoom(room *Room)
14	LoadRoom(roomID string) *Room
15}
16
17// InMemoryStore implements the Storer interface.
18//
19// Everything is persisted in-memory as maps. It is not safe to load/save filter IDs
20// or next batch tokens on any goroutine other than the syncing goroutine: the one
21// which called Client.Sync().
22type InMemoryStore struct {
23	Filters   map[string]string
24	NextBatch map[string]string
25	Rooms     map[string]*Room
26}
27
28// SaveFilterID to memory.
29func (s *InMemoryStore) SaveFilterID(userID, filterID string) {
30	s.Filters[userID] = filterID
31}
32
33// LoadFilterID from memory.
34func (s *InMemoryStore) LoadFilterID(userID string) string {
35	return s.Filters[userID]
36}
37
38// SaveNextBatch to memory.
39func (s *InMemoryStore) SaveNextBatch(userID, nextBatchToken string) {
40	s.NextBatch[userID] = nextBatchToken
41}
42
43// LoadNextBatch from memory.
44func (s *InMemoryStore) LoadNextBatch(userID string) string {
45	return s.NextBatch[userID]
46}
47
48// SaveRoom to memory.
49func (s *InMemoryStore) SaveRoom(room *Room) {
50	s.Rooms[room.ID] = room
51}
52
53// LoadRoom from memory.
54func (s *InMemoryStore) LoadRoom(roomID string) *Room {
55	return s.Rooms[roomID]
56}
57
58// NewInMemoryStore constructs a new InMemoryStore.
59func NewInMemoryStore() *InMemoryStore {
60	return &InMemoryStore{
61		Filters:   make(map[string]string),
62		NextBatch: make(map[string]string),
63		Rooms:     make(map[string]*Room),
64	}
65}
66