-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchannel.go
224 lines (187 loc) · 5.26 KB
/
channel.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package rc
import (
"net/url"
"strconv"
"time"
)
type ChannelList struct {
Channels []Channel `json:"channels"`
Offset int64 `json:"offset"`
Count int64 `json:"count"`
Total int64 `json:"total"`
Success bool `json:"success"`
}
type Channel struct {
ID string `json:"_id"`
Name string `json:"name"`
Type string `json:"t"`
Usernames []string `json:"usernames"`
Msgs int64 `json:"msgs"`
User ChannelUser `json:"u"`
Timestamp string `json:"ts"`
ReadOnly bool `json:"ro"`
SysMes bool `json:"sysMes"`
UpdatedAt string `json:"_updatedAt"`
}
type ChannelUser struct {
ID string `json:"_id"`
Username string `json:"username"`
}
type ChannelInfoEnv struct {
Channel ChannelInfo `json:"channel"`
Success bool `json:"success"`
}
type ChannelInfo struct {
ID string `json:"_id"`
Timestamp string `json:"ts"`
Type string `json:"t"`
Name string `json:"name"`
Usernames []string `json:"usernames"`
Msgs int64 `json:"msgs"`
Default bool `json:"default"`
UpdatedAt string `json:"_updatedAt"`
LM string `json:"lm"`
}
type ChannelHistory struct {
Messages []ChannelMessage `json:"messages"`
Success bool `json:"success"`
}
type ChannelMessage struct {
ID string `json:"_id"`
RoomID string `json:"rid"`
Msg string `json:"msg"`
Timestamp string `json:"ts"`
User ChannelUser `json:"u"`
UpdatedAt string `json:"_updatedAt"`
ChannelType *string `json:"t,omitempty"`
Groupable *bool `json:"groupable,omitempty"`
}
type ChannelCounters struct {
Joined bool `json:"joined"`
Members int64 `json:"members"`
Unreads int64 `json:"unreads"`
UnreadsFrom string `json:"unreadsFrom"`
Msgs int64 `json:"msgs"`
Latest string `json:"latest"`
UserMentions int64 `json:"userMentions"`
Success bool `json:"success"`
}
type ChannelMembers struct {
Members []ChannelMember `json:"members"`
Count int64 `json:"count"`
Offset int64 `json:"offset"`
Total int64 `json:"total"`
Success bool `json:"success"`
}
type ChannelMember struct {
ID string `json:"_id"`
Username string `json:"username"`
Name string `json:"name"`
Status string `json:"status"`
}
type ChannelOnline struct {
Online []ChannelUser `json:"online"`
Success bool `json:"success"`
}
type ChannelRoles struct {
Roles []ChannelRole `json:"roles"`
Success bool `json:"success"`
}
type ChannelRole struct {
RoomID string `json:"rid"`
User ChannelMember `json:"u"`
Roles []string `json:"roles"`
ID string `json:"_id"`
}
func (c *Client) GetChannelList() (*ChannelList, error) {
chlist := &ChannelList{}
if err := c.c.get("/channels.list", nil).JSON(chlist); err != nil {
return nil, err
}
return chlist, nil
}
func (c *Client) GetChannelInfo(roomID string) (*ChannelInfo, error) {
chinfo := &ChannelInfoEnv{}
q := query("roomId", roomID)
if err := c.c.get("/channels.info", q.Q()).JSON(chinfo); err != nil {
return nil, err
}
ch := chinfo.Channel
return &ch, nil
}
func (c *Client) GetChannelCounters(roomID string) (*ChannelCounters, error) {
chcounters := &ChannelCounters{}
q := query("roomId", roomID)
if err := c.c.get("/channels.counters", q.Q()).JSON(chcounters); err != nil {
return nil, err
}
return chcounters, nil
}
func (c *Client) GetChannelMembers(roomID string) (*ChannelMembers, error) {
chmembers := &ChannelMembers{}
q := query("roomId", roomID)
if err := c.c.get("/channels.members", q.Q()).JSON(chmembers); err != nil {
return nil, err
}
return chmembers, nil
}
func (c *Client) GetChannelOnline(roomID string) ([]ChannelUser, error) {
chonline := &ChannelOnline{}
vals := url.Values{}
if roomID != "*" && roomID != "" {
vals = query("_id", roomID).Q()
}
if err := c.c.get("/channels.online", vals).JSON(chonline); err != nil {
return nil, err
}
o := chonline.Online
return o, nil
}
func (c *Client) GetChannelRoles(roomID string) ([]ChannelRole, error) {
chroles := &ChannelRoles{}
q := query("roomId", roomID)
if err := c.c.get("/channels.roles", q.Q()).JSON(chroles); err != nil {
return nil, err
}
r := chroles.Roles
return r, nil
}
func (c *Client) GetChannelHistory(q HistoryQuery) (*ChannelHistory, error) {
chhistory := &ChannelHistory{}
if err := c.c.get("/channels.history", q.Q()).JSON(chhistory); err != nil {
return nil, err
}
return chhistory, nil
}
type HistoryQuery struct {
RoomID string
Latest *time.Time
Oldest *time.Time
Inclusive bool
Offset int
Count int
IncludeUnreads bool
}
func (h *HistoryQuery) Q() url.Values {
vals := url.Values{}
vals["roomId"] = []string{h.RoomID}
if h.Latest != nil {
vals["latest"] = []string{h.Latest.Format(TimeFormat)}
}
if h.Oldest != nil {
vals["oldest"] = []string{h.Oldest.Format(TimeFormat)}
}
if h.Inclusive {
vals["inclusive"] = []string{"true"}
}
if h.Offset > 0 {
vals["offset"] = []string{strconv.Itoa(h.Offset)}
}
if h.Count > 0 {
vals["count"] = []string{strconv.Itoa(h.Count)}
}
if h.IncludeUnreads {
vals["unreads"] = []string{"true"}
}
return vals
}