-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstate.go
75 lines (62 loc) · 2.9 KB
/
state.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package gotrix
import (
"errors"
"github.com/chanbakjsd/gotrix/api"
"github.com/chanbakjsd/gotrix/event"
"github.com/chanbakjsd/gotrix/matrix"
"github.com/chanbakjsd/gotrix/state"
)
// ErrInvalidStateEvent is returned by (*Client).RoomState and (*Client).RoomStates when homeserver returns an event
// that is not a known state event when a state event is expected.
var ErrInvalidStateEvent = errors.New("invalid state event has been returned by homeserver")
// ErrStopIter is an error used to denote that the iteration on EachRoomState should be stopped.
var ErrStopIter = errors.New("stop iterating on EachRoomState")
func init() {
state.ErrStopIter = ErrStopIter
}
// State represents the required functions for a state.
type State interface {
// RoomState returns the latest event in a room with the specified type.
// If it is found in the cache, error will be nil.
// Note that (nil, nil) should be returned if the cache can be certain the event type never occurred.
RoomState(roomID matrix.RoomID, eventType event.Type, stateKey string) (event.StateEvent, error)
// EachRoomState calls f for every event stored in the state.
// To abort iteration, f should return ErrStopIter.
// This function can return the error returned by f or errors while getting data for iteration.
EachRoomState(roomID matrix.RoomID, eventType event.Type, f func(key string, e event.StateEvent) error) error
// RoomSummary returns the summary of a room as received in sync response.
RoomSummary(roomID matrix.RoomID) (api.SyncRoomSummary, error)
// AddEvent adds the needed events from the given sync response.
// It is up to the implementation to pick and add the needed events inside the response.
AddEvents(*api.SyncResponse) error
}
// RoomState queries the internal State for the given RoomEvent.
// If the State does not have that event, it queries the homeserver directly.
func (c *Client) RoomState(roomID matrix.RoomID, eventType event.Type, key string) (event.StateEvent, error) {
e, err := c.State.RoomState(roomID, eventType, key)
if err == nil {
return e, nil
}
raw, err := c.Client.RoomState(roomID, eventType, key)
if err != nil {
return nil, err
}
parsed, err := event.Parse(raw)
if err != nil {
return nil, err
}
stateEvent, ok := parsed.(event.StateEvent)
if !ok {
return nil, ErrInvalidStateEvent
}
return stateEvent, nil
}
// EachRoomState iterates through all events with the specified type, stopping if f returns ErrIterStop.
func (c *Client) EachRoomState(roomID matrix.RoomID, typ event.Type, f func(string, event.StateEvent) error) error {
return c.State.EachRoomState(roomID, typ, f)
}
// RoomSummary queries the State for the summary of a room, commonly used for generating room name.
// To follow the room name generation strategy of the specification, use Client.RoomName instead.
func (c *Client) RoomSummary(roomID matrix.RoomID) (api.SyncRoomSummary, error) {
return c.State.RoomSummary(roomID)
}