This repository has been archived by the owner on Oct 9, 2023. It is now read-only.
forked from GetStream/stream-chat-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
channel.go
295 lines (227 loc) · 7.49 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package stream_chat
import (
"errors"
"net/http"
"net/url"
"path"
"time"
)
type ChannelMember struct {
UserID string `json:"user_id,omitempty"`
User *User `json:"user,omitempty"`
IsModerator bool `json:"is_moderator,omitempty"`
Invited bool `json:"invited,omitempty"`
InviteAcceptedAt *time.Time `json:"invite_accepted_at,omitempty"`
InviteRejectedAt *time.Time `json:"invite_rejected_at,omitempty"`
Role string `json:"role,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
UpdatedAt time.Time `json:"updated_at,omitempty"`
}
type Channel struct {
ID string `json:"id"`
Type string `json:"type"`
CID string `json:"cid"` // full id in format channel_type:channel_ID
Config ChannelConfig `json:"config"`
CreatedBy *User `json:"created_by"`
Frozen bool `json:"frozen"`
MemberCount int `json:"member_count"`
Members []*ChannelMember `json:"members"`
Messages []*Message `json:"messages"`
Read []*User `json:"read"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
LastMessageAt time.Time `json:"last_message_at"`
client *Client
}
type queryResponse struct {
Channel *Channel `json:"channel,omitempty"`
Messages []*Message `json:"messages,omitempty"`
Members []*ChannelMember `json:"members,omitempty"`
Read []*User `json:"read,omitempty"`
}
func (q queryResponse) updateChannel(ch *Channel) {
if q.Channel != nil {
// save client pointer but update channel information
client := ch.client
*ch = *q.Channel
ch.client = client
}
if q.Members != nil {
ch.Members = q.Members
}
if q.Messages != nil {
ch.Messages = q.Messages
}
if q.Read != nil {
ch.Read = q.Read
}
}
// query makes request to channel api and updates channel internal state
func (ch *Channel) query(options map[string]interface{}, data map[string]interface{}) (err error) {
payload := map[string]interface{}{
"state": true,
}
for k, v := range options {
payload[k] = v
}
if data == nil {
data = map[string]interface{}{}
}
data["created_by"] = map[string]interface{}{"id": ch.CreatedBy.ID}
payload["data"] = data
p := path.Join("channels", url.PathEscape(ch.Type), url.PathEscape(ch.ID), "query")
var resp queryResponse
err = ch.client.makeRequest(http.MethodPost, p, nil, payload, &resp)
if err != nil {
return err
}
resp.updateChannel(ch)
return nil
}
// Update edits the channel's custom properties
//
// options: the object to update the custom properties of this channel with
// message: optional update message
func (ch *Channel) Update(options map[string]interface{}, message string) error {
payload := map[string]interface{}{
"data": options,
"message": message,
}
p := path.Join("channels", url.PathEscape(ch.Type), url.PathEscape(ch.ID))
return ch.client.makeRequest(http.MethodPost, p, nil, payload, nil)
}
// Delete removes the channel. Messages are permanently removed.
func (ch *Channel) Delete() error {
p := path.Join("channels", url.PathEscape(ch.Type), url.PathEscape(ch.ID))
return ch.client.makeRequest(http.MethodDelete, p, nil, nil, nil)
}
// Truncate removes all messages from the channel
func (ch *Channel) Truncate() error {
p := path.Join("channels", url.PathEscape(ch.Type), url.PathEscape(ch.ID), "truncate")
return ch.client.makeRequest(http.MethodPost, p, nil, nil, nil)
}
// AddMembers adds members with given user IDs to the channel
func (ch *Channel) AddMembers(userIDs ...string) error {
if len(userIDs) == 0 {
return errors.New("user IDs are empty")
}
data := map[string]interface{}{
"add_members": userIDs,
}
p := path.Join("channels", url.PathEscape(ch.Type), url.PathEscape(ch.ID))
return ch.client.makeRequest(http.MethodPost, p, nil, data, nil)
}
// RemoveMembers deletes members with given IDs from the channel
func (ch *Channel) RemoveMembers(userIDs ...string) error {
if len(userIDs) == 0 {
return errors.New("user IDs are empty")
}
data := map[string]interface{}{
"remove_members": userIDs,
}
p := path.Join("channels", url.PathEscape(ch.Type), url.PathEscape(ch.ID))
var resp queryResponse
err := ch.client.makeRequest(http.MethodPost, p, nil, data, &resp)
if err != nil {
return err
}
resp.updateChannel(ch)
return nil
}
// AddModerators adds moderators with given IDs to the channel
func (ch *Channel) AddModerators(userIDs ...string) error {
if len(userIDs) == 0 {
return errors.New("user IDs are empty")
}
data := map[string]interface{}{
"add_moderators": userIDs,
}
p := path.Join("channels", url.PathEscape(ch.Type), url.PathEscape(ch.ID))
return ch.client.makeRequest(http.MethodPost, p, nil, data, nil)
}
// DemoteModerators moderators with given IDs from the channel
func (ch *Channel) DemoteModerators(userIDs ...string) error {
if len(userIDs) == 0 {
return errors.New("user IDs are empty")
}
data := map[string]interface{}{
"demote_moderators": userIDs,
}
p := path.Join("channels", url.PathEscape(ch.Type), url.PathEscape(ch.ID))
return ch.client.makeRequest(http.MethodPost, p, nil, data, nil)
}
// MarkRead send the mark read event for user with given ID, only works if the `read_events` setting is enabled
// options: additional data, ie {"messageID": last_messageID}
func (ch *Channel) MarkRead(userID string, options map[string]interface{}) error {
switch {
case userID == "":
return errors.New("user ID must be not empty")
case options == nil:
options = map[string]interface{}{}
}
p := path.Join("channels", url.PathEscape(ch.Type), url.PathEscape(ch.ID), "read")
options["user"] = map[string]interface{}{"id": userID}
return ch.client.makeRequest(http.MethodPost, p, nil, options, nil)
}
// BanUser bans target user ID from this channel
// userID: user who bans target
// options: additional ban options, ie {"timeout": 3600, "reason": "offensive language is not allowed here"}
func (ch *Channel) BanUser(targetID string, userID string, options map[string]interface{}) error {
switch {
case targetID == "":
return errors.New("target ID is empty")
case userID == "":
return errors.New("user ID is empty")
case options == nil:
options = map[string]interface{}{}
}
options["type"] = ch.Type
options["id"] = ch.ID
return ch.client.BanUser(targetID, userID, options)
}
// UnBanUser removes the ban for target user ID on this channel
func (ch *Channel) UnBanUser(targetID string, options map[string]string) error {
switch {
case targetID == "":
return errors.New("target ID must be not empty")
case options == nil:
options = map[string]string{}
}
options["type"] = ch.Type
options["id"] = ch.ID
return ch.client.UnBanUser(targetID, options)
}
// CreateChannel creates new channel of given type and id or returns already created one
func (c *Client) CreateChannel(chanType string, chanID string, userID string, data map[string]interface{}) (*Channel, error) {
switch {
case chanType == "":
return nil, errors.New("channel type is empty")
case chanID == "":
return nil, errors.New("channel ID is empty")
case userID == "":
return nil, errors.New("user ID is empty")
}
ch := &Channel{
Type: chanType,
ID: chanID,
client: c,
CreatedBy: &User{ID: userID},
}
options := map[string]interface{}{
"watch": false,
"state": true,
"presence": false,
}
err := ch.query(options, data)
return ch, err
}
// todo: cleanup this
func (ch *Channel) refresh() error {
options := map[string]interface{}{
"watch": false,
"state": true,
"presence": false,
}
err := ch.query(options, nil)
return err
}