-
Notifications
You must be signed in to change notification settings - Fork 264
/
api_channel.go
582 lines (538 loc) · 16.6 KB
/
api_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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
package server
import (
"fmt"
"net/http"
"strings"
"github.com/WuKongIM/WuKongIM/pkg/wkhttp"
"github.com/WuKongIM/WuKongIM/pkg/wklog"
"github.com/WuKongIM/WuKongIM/pkg/wkproto"
"github.com/WuKongIM/WuKongIM/pkg/wkstore"
"github.com/WuKongIM/WuKongIM/pkg/wkutil"
"github.com/pkg/errors"
"go.uber.org/zap"
)
// ChannelAPI ChannelAPI
type ChannelAPI struct {
s *Server
wklog.Log
}
// NewChannelAPI 创建API
func NewChannelAPI(s *Server) *ChannelAPI {
return &ChannelAPI{
Log: wklog.NewWKLog("ChannelAPI"),
s: s,
}
}
// Route Route
func (ch *ChannelAPI) Route(r *wkhttp.WKHttp) {
//################### 频道 ###################
r.POST("/channel", ch.channelCreateOrUpdate) // 创建或修改频道
r.POST("/channel/info", ch.updateOrAddChannelInfo) // 更新或添加频道基础信息
r.POST("/channel/delete", ch.channelDelete) // 删除频道
//################### 订阅者 ###################// 删除频道
r.POST("/channel/subscriber_add", ch.addSubscriber) // 添加订阅者
r.POST("/channel/subscriber_remove", ch.removeSubscriber) // 移除订阅者
//################### 黑明单 ###################// 删除频道
r.POST("/channel/blacklist_add", ch.blacklistAdd) // 添加黑明单
r.POST("/channel/blacklist_set", ch.blacklistSet) // 设置黑明单(覆盖原来的黑名单数据)
r.POST("/channel/blacklist_remove", ch.blacklistRemove) // 移除黑名单
//################### 白名单 ###################
r.POST("/channel/whitelist_add", ch.whitelistAdd) // 添加白名单
r.POST("/channel/whitelist_set", ch.whitelistSet) // 设置白明单(覆盖
r.POST("/channel/whitelist_remove", ch.whitelistRemove)
r.GET("/channel/whitelist", func(c *wkhttp.Context) {
channelID := c.Query("channel_id")
channel, err := ch.s.channelManager.GetChannel(channelID, wkproto.ChannelTypeGroup)
if err != nil {
c.ResponseError(err)
return
}
if channel == nil {
c.ResponseError(errors.New("频道不存在!"))
return
}
whitelist := make([]string, 0)
channel.whitelist.Range(func(key, value interface{}) bool {
whitelist = append(whitelist, key.(string))
return true
})
c.JSON(http.StatusOK, whitelist)
})
//################### 频道消息 ###################
// 同步频道消息
r.POST("/channel/messagesync", ch.syncMessages)
}
func (ch *ChannelAPI) channelCreateOrUpdate(c *wkhttp.Context) {
var req ChannelCreateReq
if err := c.BindJSON(&req); err != nil {
c.ResponseError(errors.Wrap(err, "数据格式有误!"))
return
}
if err := req.Check(); err != nil {
c.ResponseError(err)
return
}
if req.ChannelType == wkproto.ChannelTypePerson {
c.ResponseError(errors.New("暂不支持个人频道!"))
return
}
channelInfo := wkstore.NewChannelInfo(req.ChannelID, req.ChannelType)
err := ch.s.store.AddOrUpdateChannel(channelInfo)
if err != nil {
c.ResponseError(err)
ch.Error("创建频道失败!", zap.Error(err))
return
}
err = ch.s.store.RemoveAllSubscriber(req.ChannelID, req.ChannelType)
if err != nil {
ch.Error("移除所有订阅者失败!", zap.Error(err))
c.ResponseError(errors.New("移除所有订阅者失败!"))
return
}
if len(req.Subscribers) > 0 {
err = ch.s.store.AddSubscribers(req.ChannelID, req.ChannelType, req.Subscribers)
if err != nil {
ch.Error("添加订阅者失败!", zap.Error(err))
c.ResponseError(err)
return
}
}
ch.s.channelManager.DeleteChannelFromCache(req.ChannelID, req.ChannelType)
c.ResponseOK()
}
// 更新或添加频道信息
func (ch *ChannelAPI) updateOrAddChannelInfo(c *wkhttp.Context) {
var req ChannelInfoReq
if err := c.BindJSON(&req); err != nil {
ch.Error("数据格式有误!", zap.Error(err))
c.ResponseError(errors.New("数据格式有误!"))
return
}
channelInfo := req.ToChannelInfo()
err := ch.s.store.AddOrUpdateChannel(channelInfo)
if err != nil {
ch.Error("添加或更新频道信息失败!", zap.Error(err))
c.ResponseError(errors.New("添加或更新频道信息失败!"))
return
}
// 如果有缓存则更新缓存
channel := ch.s.channelManager.getChannelFromCache(req.ChannelID, req.ChannelType)
if channel != nil {
channel.ChannelInfo = channelInfo
}
c.ResponseOK()
}
func (ch *ChannelAPI) addSubscriber(c *wkhttp.Context) {
var req subscriberAddReq
if err := c.BindJSON(&req); err != nil {
ch.Error("数据格式有误!", zap.Error(err))
c.ResponseError(errors.Wrap(err, "数据格式有误!"))
return
}
if err := req.Check(); err != nil {
c.ResponseError(err)
return
}
if req.ChannelType == wkproto.ChannelTypePerson {
c.ResponseError(errors.New("个人频道不支持添加订阅者!"))
return
}
if req.ChannelType == 0 {
req.ChannelType = wkproto.ChannelTypeGroup //默认为群
}
channel, err := ch.s.channelManager.GetChannel(req.ChannelID, req.ChannelType)
if err != nil {
ch.Error("获取频道失败!", zap.String("channel", req.ChannelID), zap.Error(err))
c.ResponseError(errors.Wrap(err, "获取频道失败!"))
return
}
if channel == nil {
ch.Error("频道不存在!", zap.String("channel_id", req.ChannelID), zap.Uint8("channel_type", req.ChannelType))
c.ResponseError(errors.New("频道并不存在!"))
return
}
if req.TempSubscriber == 1 {
err = ch.addTmpSubscriberWithReq(req, channel)
if err != nil {
ch.Error("添加临时频道失败!", zap.Error(err))
c.ResponseError(errors.New("添加临时频道失败!"))
return
}
} else {
err = ch.addSubscriberWithReq(req, channel)
if err != nil {
ch.Error("添加频道失败!", zap.Error(err))
c.ResponseError(errors.New("添加频道失败!"))
return
}
}
c.ResponseOK()
}
func (ch *ChannelAPI) addTmpSubscriberWithReq(req subscriberAddReq, channel *Channel) error {
if req.Reset == 1 {
channel.RemoveAllTmpSubscriber()
}
channel.AddTmpSubscribers(req.Subscribers)
return nil
}
func (ch *ChannelAPI) addSubscriberWithReq(req subscriberAddReq, channel *Channel) error {
var err error
existSubscribers := make([]string, 0)
if req.Reset == 1 {
err = ch.s.store.RemoveAllSubscriber(req.ChannelID, req.ChannelType)
if err != nil {
ch.Error("移除所有订阅者失败!", zap.Error(err))
return err
}
channel.RemoveAllSubscriber()
} else {
existSubscribers, err = ch.s.store.GetSubscribers(req.ChannelID, req.ChannelType)
if err != nil {
ch.Error("获取所有订阅者失败!", zap.Error(err))
return err
}
}
newSubscribers := make([]string, 0, len(req.Subscribers))
for _, subscriber := range req.Subscribers {
if strings.TrimSpace(subscriber) == "" {
continue
}
if !wkutil.ArrayContains(existSubscribers, subscriber) {
newSubscribers = append(newSubscribers, subscriber)
}
}
if len(newSubscribers) > 0 {
err = ch.s.store.AddSubscribers(req.ChannelID, req.ChannelType, newSubscribers)
if err != nil {
ch.Error("添加订阅者失败!", zap.Error(err))
return err
}
channel.AddSubscribers(newSubscribers)
}
return nil
}
func (ch *ChannelAPI) removeSubscriber(c *wkhttp.Context) {
var req subscriberRemoveReq
if err := c.BindJSON(&req); err != nil {
c.ResponseError(errors.Wrap(err, "数据格式有误!"))
return
}
if err := req.Check(); err != nil {
c.ResponseError(err)
return
}
channel, err := ch.s.channelManager.GetChannel(req.ChannelID, req.ChannelType)
if err != nil {
ch.Error("获取频道失败!", zap.Error(err), zap.String("channelId", req.ChannelID))
c.ResponseError(errors.Wrap(err, "获取频道失败!"))
return
}
if channel == nil {
ch.Error("频道不存在!", zap.String("channelID", req.ChannelID), zap.Uint8("channelType", req.ChannelType))
c.ResponseError(errors.New("频道不存在!"))
return
}
if req.TempSubscriber == 1 {
channel.RemoveTmpSubscribers(req.Subscribers)
} else {
err = ch.s.store.RemoveSubscribers(req.ChannelID, req.ChannelType, req.Subscribers)
if err != nil {
ch.Error("移除订阅者失败!", zap.Error(err))
c.ResponseError(err)
return
}
channel.RemoveSubscribers(req.Subscribers)
err = ch.s.conversationManager.DeleteConversation(req.Subscribers, req.ChannelID, req.ChannelType)
if err != nil {
ch.Error("删除最近会话失败!", zap.Error(err))
c.ResponseError(err)
return
}
}
c.ResponseOK()
}
func (ch *ChannelAPI) blacklistAdd(c *wkhttp.Context) {
var req blacklistReq
if err := c.BindJSON(&req); err != nil {
ch.Error("数据格式有误!", zap.Error(err))
c.ResponseError(err)
return
}
if err := req.Check(); err != nil {
c.ResponseError(err)
return
}
err := ch.s.store.AddDenylist(req.ChannelID, req.ChannelType, req.UIDs)
if err != nil {
ch.Error("添加黑名单失败!", zap.Error(err))
c.ResponseError(err)
return
}
// 增加到缓存中
channelObj, err := ch.s.channelManager.GetChannel(req.ChannelID, req.ChannelType)
if err != nil {
c.ResponseError(err)
return
}
channelObj.AddDenylist(req.UIDs)
c.ResponseOK()
}
func (ch *ChannelAPI) blacklistSet(c *wkhttp.Context) {
var req blacklistReq
if err := c.BindJSON(&req); err != nil {
ch.Error("数据格式有误!", zap.Error(err))
c.ResponseError(err)
return
}
if strings.TrimSpace(req.ChannelID) == "" {
c.ResponseError(errors.New("频道ID不能为空!"))
return
}
err := ch.s.store.RemoveAllDenylist(req.ChannelID, req.ChannelType)
if err != nil {
ch.Error("移除所有黑明单失败!", zap.Error(err))
c.ResponseError(errors.New("移除所有黑明单失败!"))
return
}
if len(req.UIDs) > 0 {
err := ch.s.store.AddDenylist(req.ChannelID, req.ChannelType, req.UIDs)
if err != nil {
ch.Error("添加黑名单失败!", zap.Error(err))
c.ResponseError(err)
return
}
}
// 增加到缓存中
channelObj, err := ch.s.channelManager.GetChannel(req.ChannelID, req.ChannelType)
if err != nil {
c.ResponseError(err)
return
}
channelObj.SetDenylist(req.UIDs)
c.ResponseOK()
}
func (ch *ChannelAPI) blacklistRemove(c *wkhttp.Context) {
var req blacklistReq
if err := c.BindJSON(&req); err != nil {
ch.Error("数据格式有误!", zap.Error(err))
c.ResponseError(err)
return
}
if err := req.Check(); err != nil {
c.ResponseError(err)
return
}
err := ch.s.store.RemoveDenylist(req.ChannelID, req.ChannelType, req.UIDs)
if err != nil {
ch.Error("移除黑名单失败!", zap.Error(err))
c.ResponseError(err)
return
}
// 缓存中移除
channelObj, err := ch.s.channelManager.GetChannel(req.ChannelID, req.ChannelType)
if err != nil {
c.ResponseError(err)
return
}
channelObj.RemoveDenylist(req.UIDs)
c.ResponseOK()
}
// 删除频道
func (ch *ChannelAPI) channelDelete(c *wkhttp.Context) {
var req ChannelDeleteReq
if err := c.BindJSON(&req); err != nil {
c.ResponseError(errors.Wrap(err, "数据格式有误!"))
return
}
err := ch.s.store.DeleteChannelAndClearMessages(req.ChannelID, req.ChannelType)
if err != nil {
c.ResponseError(err)
return
}
ch.s.channelManager.DeleteChannel(req.ChannelID, req.ChannelType)
c.ResponseOK()
}
// ----------- 白名单 -----------
// 添加白名单
func (ch *ChannelAPI) whitelistAdd(c *wkhttp.Context) {
var req whitelistReq
if err := c.BindJSON(&req); err != nil {
ch.Error("数据格式有误!", zap.Error(err))
c.ResponseError(err)
return
}
if err := req.Check(); err != nil {
c.ResponseError(err)
return
}
err := ch.s.store.AddAllowlist(req.ChannelID, req.ChannelType, req.UIDs)
if err != nil {
ch.Error("添加白名单失败!", zap.Error(err))
c.ResponseError(err)
return
}
// 增加到缓存中
channelObj, err := ch.s.channelManager.GetChannel(req.ChannelID, req.ChannelType)
if err != nil {
c.ResponseError(err)
return
}
channelObj.AddAllowlist(req.UIDs)
c.ResponseOK()
}
func (ch *ChannelAPI) whitelistSet(c *wkhttp.Context) {
var req whitelistReq
if err := c.BindJSON(&req); err != nil {
ch.Error("数据格式有误!", zap.Error(err))
c.ResponseError(err)
return
}
if strings.TrimSpace(req.ChannelID) == "" {
c.ResponseError(errors.New("频道ID不能为空!"))
return
}
err := ch.s.store.RemoveAllAllowlist(req.ChannelID, req.ChannelType)
if err != nil {
ch.Error("移除所有白明单失败!", zap.Error(err))
c.ResponseError(errors.New("移除所有白明单失败!"))
return
}
if len(req.UIDs) > 0 {
err := ch.s.store.AddAllowlist(req.ChannelID, req.ChannelType, req.UIDs)
if err != nil {
ch.Error("添加白名单失败!", zap.Error(err))
c.ResponseError(err)
return
}
}
// 增加到缓存中
channelObj, err := ch.s.channelManager.GetChannel(req.ChannelID, req.ChannelType)
if err != nil {
c.ResponseError(err)
return
}
channelObj.SetAllowlist(req.UIDs)
c.ResponseOK()
}
// 移除白名单
func (ch *ChannelAPI) whitelistRemove(c *wkhttp.Context) {
var req whitelistReq
if err := c.BindJSON(&req); err != nil {
ch.Error("数据格式有误!", zap.Error(err))
c.ResponseError(err)
return
}
if err := req.Check(); err != nil {
c.ResponseError(err)
return
}
err := ch.s.store.RemoveAllowlist(req.ChannelID, req.ChannelType, req.UIDs)
if err != nil {
ch.Error("移除白名单失败!", zap.Error(err))
c.ResponseError(err)
return
}
// 缓存中移除
channelObj, err := ch.s.channelManager.GetChannel(req.ChannelID, req.ChannelType)
if err != nil {
c.ResponseError(err)
return
}
channelObj.RemoveAllowlist(req.UIDs)
c.ResponseOK()
}
type PullMode int // 拉取模式
const (
PullModeDown PullMode = iota // 向下拉取
PullModeUp // 向上拉取
)
// 同步频道内的消息
func (ch *ChannelAPI) syncMessages(c *wkhttp.Context) {
var req struct {
LoginUID string `json:"login_uid"` // 当前登录用户的uid
ChannelID string `json:"channel_id"`
ChannelType uint8 `json:"channel_type"`
StartMessageSeq uint32 `json:"start_message_seq"` //开始消息列号(结果包含start_message_seq的消息)
EndMessageSeq uint32 `json:"end_message_seq"` // 结束消息列号(结果不包含end_message_seq的消息)
Limit int `json:"limit"` // 每次同步数量限制
PullMode PullMode `json:"pull_mode"` // 拉取模式 0:向下拉取 1:向上拉取
}
if err := c.BindJSON(&req); err != nil {
ch.Error("数据格式有误!", zap.Error(err))
c.ResponseError(errors.New("数据格式有误!"))
return
}
var (
limit = req.Limit
fakeChannelID = req.ChannelID
messages []wkstore.Message
err error
)
if limit > 10000 {
limit = 10000
}
if req.ChannelType == wkproto.ChannelTypePerson {
fakeChannelID = GetFakeChannelIDWith(req.LoginUID, req.ChannelID)
}
fmt.Println("fakeChannelID:", fakeChannelID, "req.ChannelType:", req.ChannelType, "req.StartMessageSeq:", req.StartMessageSeq, "req.EndMessageSeq:", req.EndMessageSeq, "limit:", limit)
if req.StartMessageSeq == 0 && req.EndMessageSeq == 0 {
messages, err = ch.s.store.LoadLastMsgs(fakeChannelID, req.ChannelType, limit)
} else if req.PullMode == PullModeUp { // 向上拉取
messages, err = ch.s.store.LoadNextRangeMsgs(fakeChannelID, req.ChannelType, req.StartMessageSeq, req.EndMessageSeq, limit)
} else {
messages, err = ch.s.store.LoadPrevRangeMsgs(fakeChannelID, req.ChannelType, req.StartMessageSeq, req.EndMessageSeq, limit)
}
if err != nil {
c.ResponseError(err)
return
}
messageResps := make([]*MessageResp, 0, len(messages))
if len(messages) > 0 {
for _, message := range messages {
messageResp := &MessageResp{}
messageResp.from(message.(*Message))
if strings.TrimSpace(messageResp.StreamNo) != "" {
streamItems, err := ch.s.store.GetStreamItems(messageResp.ChannelID, messageResp.ChannelType, messageResp.StreamNo)
if err != nil {
ch.Error("获取streamItems失败!", zap.Error(err))
continue
}
if len(streamItems) > 0 {
streamItemResps := make([]*StreamItemResp, 0, len(streamItems))
for _, streamItem := range streamItems {
streamItemResps = append(streamItemResps, newStreamItemResp(streamItem))
}
messageResp.Streams = streamItemResps
}
}
messageResps = append(messageResps, messageResp)
}
}
var more bool = true // 是否有更多数据
if len(messageResps) < limit {
more = false
}
if len(messageResps) > 0 {
if req.PullMode == PullModeDown {
if req.EndMessageSeq != 0 {
messageSeq := messageResps[0].MessageSeq
if req.EndMessageSeq == messageSeq {
more = false
}
}
} else {
if req.EndMessageSeq != 0 {
messageSeq := messageResps[len(messageResps)-1].MessageSeq
if req.EndMessageSeq == messageSeq {
more = false
}
}
}
}
c.JSON(http.StatusOK, syncMessageResp{
StartMessageSeq: req.StartMessageSeq,
EndMessageSeq: req.EndMessageSeq,
More: wkutil.BoolToInt(more),
Messages: messageResps,
})
}