-
Notifications
You must be signed in to change notification settings - Fork 3
/
presence.go
98 lines (80 loc) · 2.34 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
88
89
90
91
92
93
94
95
96
97
98
// Package presence provides an advanced presence system
package presence
const (
// Unknown is for errored requests
Unknown Status = iota
// Offline is for displaying user as offline in the system
Offline // do not handle unset variable as offline
// Online is for displaying user as online in the system
Online
)
const (
online = "ONLINE"
offline = "OFFLINE"
unknown = "UNKNOWN"
)
// Status defines what is the current status of a user in presence system
type Status int
// String implements the Stringer interface
func (s Status) String() string {
switch s {
case Offline:
return offline
case Online:
return online
}
// if status is not set or not a known value
return unknown
}
// Backend represents basic interface for all required backend operations for
// presence package
type Backend interface {
Online(...string) error
Offline(...string) error
Status(...string) ([]Event, error)
Close() error
Error() chan error
ListenStatusChanges() chan Event
}
// Event is the data type for occuring events in the system
type Event struct {
// ID is the given key by the application
ID string
// Status holds the changing type of event
Status Status
}
// Session holds the backend and provides accessor methods for communication
type Session struct {
// holds the interface
backend Backend
}
// New creates a session for any broker system that is architected to use,
// communicate, forward events to the presence system
func New(backend Backend) (*Session, error) {
return &Session{backend: backend}, nil
}
// Online sets given ids as online
func (s *Session) Online(ids ...string) error {
return s.backend.Online(ids...)
}
// Offline sets given ids as offline
func (s *Session) Offline(ids ...string) error {
return s.backend.Offline(ids...)
}
// Status returns the current status of multiple keys from system
func (s *Session) Status(ids ...string) ([]Event, error) {
return s.backend.Status(ids...)
}
// Close closes the backend connection gracefully
func (s *Session) Close() error {
return s.backend.Close()
}
// Error returns error if it happens while listening to status changes
func (s *Session) Error() chan error {
return s.backend.Error()
}
// ListenStatusChanges subscribes the backend and gets online and offline status
// changes from it
func (s *Session) ListenStatusChanges() chan Event {
return s.backend.ListenStatusChanges()
}