-
Notifications
You must be signed in to change notification settings - Fork 1
/
presence.go
87 lines (74 loc) · 2.71 KB
/
presence.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
76
77
78
79
80
81
82
83
84
85
86
87
package rtm2
type PresenceEventType int
const (
PresenceTypeSnapshot PresenceEventType = 0
PresenceTypeInterval PresenceEventType = 1
PresenceTypeJoinChannel PresenceEventType = 2
PresenceTypeLeaveChannel PresenceEventType = 3
PresenceTypeTimeout PresenceEventType = 4
PresenceTypeStateChange PresenceEventType = 5
PresenceTypeOutOfService PresenceEventType = 6
)
type PresenceEvent struct {
Type PresenceEventType
UserId string
Items map[string]string
// For interval
Joined []string
Left []string
Timeout []string
// For snapshot and interval event
States map[string]map[string]string
}
type UserState struct {
UserId string
State map[string]string
}
type ChannelInfo struct {
Channel string
Type ChannelType
}
type PresenceOptions struct {
UserId bool
State bool
Page string
}
type PresenceOption func(*PresenceOptions)
// WithPresenceUserId whether to display user id in query result.
func WithPresenceUserId(enabled bool) PresenceOption {
return func(c *PresenceOptions) {
c.UserId = enabled
}
}
// WithPresenceState whether to display user state in query result.
func WithPresenceState(enabled bool) PresenceOption {
return func(c *PresenceOptions) {
c.State = enabled
}
}
// WithPage stores next page index.
func WithPage(page string) PresenceOption {
return func(c *PresenceOptions) {
c.Page = page
}
}
type Presence interface {
// GetPresenceChan must be called after Presence is subscribed on certain channel.
// Otherwise, the golang chan will be blocked which will cause fatal error.
// Return error if no subscription found.
GetPresenceChan(channel string, channelType ChannelType) (map[string]*UserState, <-chan *PresenceEvent, error)
// WhoNow returns all users joined certain channel.
// Paging is supported: if there are more Users, return "next page index" on second return value.
// Use WithPage to start from "next page index".
WhoNow(channel string, channelType ChannelType, opts ...PresenceOption) (map[string]*UserState, string, error)
// WhereNow all channels certain user has joined. No matter Message Channel or Stream Channel.
WhereNow(userId string) ([]*ChannelInfo, error)
// SetState can be called before Join Stream Channel or Subscribe Message Channel.
// Cache the state and auto set after Join or Subscribe.
SetState(channel string, channelType ChannelType, data map[string]string) error
// RemoveState can be called before Join Stream Channel or Subscribe Message Channel.
// Cache state can be removed as well.
RemoveState(channel string, channelType ChannelType, keys []string) error
// GetState can query certain user's state on certain channel.
GetState(channel string, channelType ChannelType, userId string) (map[string]string, error)
}