-
Notifications
You must be signed in to change notification settings - Fork 1
/
user.go
151 lines (130 loc) · 3.42 KB
/
user.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package swirl
import (
"github.com/ischenkx/swirl/internal/pubsub/message"
"time"
)
type localUser struct {
id string
app *App
}
func (u localUser) Emit(name string, options ...interface{}) {
var args []interface{}
var metaInfo interface{}
var timeStamp int64
for _, opt := range options {
switch o := opt.(type) {
case MetaInfo:
metaInfo = o
case TimeStamp:
timeStamp = time.Time(o).UnixNano()
case Args:
args = o
}
}
if timeStamp == 0 {
timeStamp = time.Now().UnixNano()
}
data, err := u.app.emitter.EncodeRawData(name, args)
if err != nil {
u.app.events.callError(EncodingError{
Reason: err,
EventOptions: EventOptions{
Name: name,
Args: args,
TimeStamp: timeStamp,
MetaInfo: metaInfo,
},
})
return
}
u.app.pubsub.Users().Iter(u.id, func(client string) {
u.app.pubsub.SendToClient(client, message.New(data))
})
u.app.events.callEmit(EmitOptions{
Users: []string{u.id},
EventOptions: EventOptions{
Name: name,
Args: args,
TimeStamp: timeStamp,
MetaInfo: metaInfo,
},
})
}
func (u localUser) Subscribe(options SubscribeOptions) {
var subscribedTopics []string
for _, topic := range options.Topics {
if err := u.app.pubsub.Users().Subscribe(u.id, topic, options.TimeStamp); err == nil {
subscribedTopics = append(subscribedTopics, topic)
}
}
u.app.pubsub.Users().Iter(u.id, func(client string) {
for _, topic := range subscribedTopics {
u.app.pubsub.Subscribe(client, topic, options.TimeStamp)
}
})
for _, topic := range subscribedTopics {
u.app.events.callUserSubscribe(u, topic, options.TimeStamp)
}
}
func (u localUser) Active() (isActive bool) {
return u.app.pubsub.Users().IsActive(u.id)
}
func (u localUser) Unsubscribe(options UnsubscribeOptions) {
var unsubscribedTopics []string
for _, topic := range options.Topics {
if err := u.app.pubsub.Users().Unsubscribe(u.id, topic, options.TimeStamp); err == nil {
unsubscribedTopics = append(unsubscribedTopics, topic)
}
}
u.app.pubsub.Users().Iter(u.id, func(client string) {
for _, topic := range unsubscribedTopics {
u.app.pubsub.Unsubscribe(client, topic, options.TimeStamp)
}
})
for _, topic := range unsubscribedTopics {
u.app.events.callUserUnsubscribe(u, topic, options.TimeStamp)
}
}
func (u localUser) Disconnect(options DisconnectOptions) {
u.app.pubsub.Users().Iter(u.id, func(client string) {
u.app.pubsub.Disconnect(client, options.TimeStamp)
})
}
func (u localUser) Clients() []string {
clients := make([]string, 0)
u.app.pubsub.Users().Iter(u.id, func(id string) {
clients = append(clients, id)
})
return clients
}
func (u localUser) Subscriptions() SubscriptionList {
return variadicSubList{
count: func(list SubscriptionList) int {
return u.app.pubsub.Users().CountSubscriptions(u.id)
},
array: func(list SubscriptionList) []Subscription {
subs := make([]Subscription, 0, list.Count())
u.app.pubsub.Users().IterSubscriptions(u.id, func(s Subscription) {
subs = append(subs, s)
})
return subs
},
}
}
func (u localUser) Events() UserEvents {
return u.app.events.forUser(u.id)
}
func (u localUser) ID() string {
return u.id
}
type User interface {
ID() string
Active() bool
Emit(name string, data ...interface{})
Subscribe(options SubscribeOptions)
Unsubscribe(options UnsubscribeOptions)
Disconnect(options DisconnectOptions)
Clients() []string
Events() UserEvents
Subscriptions() SubscriptionList
}