-
Notifications
You must be signed in to change notification settings - Fork 264
/
model.go
586 lines (516 loc) · 17.7 KB
/
model.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
583
584
585
586
package server
import (
"bytes"
"encoding/gob"
"strconv"
"strings"
"time"
"github.com/WuKongIM/WuKongIM/pkg/wkproto"
"github.com/WuKongIM/WuKongIM/pkg/wkstore"
"github.com/WuKongIM/WuKongIM/pkg/wkutil"
"github.com/pkg/errors"
)
type everyScheduler struct {
Interval time.Duration
}
func (s *everyScheduler) Next(prev time.Time) time.Time {
return prev.Add(s.Interval)
}
type Message struct {
*wkproto.RecvPacket
ToUID string // 接受者
Subscribers []string // 订阅者 如果此字段有值 则表示消息只发送给指定的订阅者
fromDeviceFlag wkproto.DeviceFlag // 发送者设备标示
fromDeviceID string // 发送者设备ID
// 重试相同的clientID
toClientID int64 // 指定接收客户端的ID
large bool // 是否是超大群
// ------- 优先队列用到 ------
index int //在切片中的索引值
pri int64 // 优先级的时间点 值越小越优先
retryCount int // 当前重试次数
}
func (m *Message) GetMessageID() int64 {
return m.MessageID
}
func (m *Message) SetSeq(seq uint32) {
m.MessageSeq = seq
}
func (m *Message) GetSeq() uint32 {
return m.MessageSeq
}
func (m *Message) Encode() []byte {
var version uint8 = 0
data := MarshalMessage(version, m)
return wkstore.EncodeMessage(m.MessageSeq, data)
}
func (m *Message) Decode(msg []byte) error {
messageSeq, data, err := wkstore.DecodeMessage(msg)
if err != nil {
return err
}
err = UnmarshalMessage(data, m)
m.MessageSeq = messageSeq
return err
}
func (m *Message) StreamStart() bool {
if strings.TrimSpace(m.StreamNo) == "" {
return false
}
return m.StreamFlag == wkproto.StreamFlagStart
}
func (m *Message) StreamIng() bool {
if strings.TrimSpace(m.StreamNo) == "" {
return false
}
return m.StreamFlag == wkproto.StreamFlagIng
}
func (m *Message) DeepCopy() (*Message, error) {
var buf bytes.Buffer
if err := gob.NewEncoder(&buf).Encode(m); err != nil {
return nil, err
}
dst := &Message{}
err := gob.NewDecoder(bytes.NewBuffer(buf.Bytes())).Decode(dst)
if err != nil {
return nil, err
}
dst.fromDeviceID = m.fromDeviceID
dst.fromDeviceFlag = m.fromDeviceFlag
dst.toClientID = m.toClientID
dst.large = m.large
dst.index = m.index
dst.pri = m.pri
dst.retryCount = m.retryCount
return dst, nil
}
// MarshalMessage MarshalMessage
func MarshalMessage(version uint8, m *Message) []byte {
enc := wkproto.NewEncoder()
defer enc.End()
enc.WriteByte(wkproto.ToFixHeaderUint8(m.RecvPacket))
enc.WriteUint8(version)
enc.WriteByte(m.Setting.Uint8())
enc.WriteInt64(m.MessageID)
enc.WriteUint32(m.MessageSeq)
enc.WriteString(m.ClientMsgNo)
if m.Setting.IsSet(wkproto.SettingStream) {
enc.WriteString(m.StreamNo)
enc.WriteUint32(m.StreamSeq)
enc.WriteUint8(uint8(m.StreamFlag))
}
enc.WriteInt32(m.Timestamp)
enc.WriteString(m.FromUID)
enc.WriteString(m.ChannelID)
enc.WriteUint8(m.ChannelType)
enc.WriteBytes(m.Payload)
return enc.Bytes()
}
// UnmarshalMessage UnmarshalMessage
func UnmarshalMessage(data []byte, m *Message) error {
dec := wkproto.NewDecoder(data)
// header
var err error
var header uint8
if header, err = dec.Uint8(); err != nil {
return err
}
recvPacket := &wkproto.RecvPacket{}
framer := wkproto.FramerFromUint8(header)
if _, err = dec.Uint8(); err != nil {
return err
}
recvPacket.Framer = framer
// setting
var setting uint8
if setting, err = dec.Uint8(); err != nil {
return err
}
m.RecvPacket = recvPacket
m.Setting = wkproto.Setting(setting)
// messageID
if m.MessageID, err = dec.Int64(); err != nil {
return err
}
// MessageSeq
if m.MessageSeq, err = dec.Uint32(); err != nil {
return err
}
// ClientMsgNo
if m.ClientMsgNo, err = dec.String(); err != nil {
return err
}
// StreamNo
if m.Setting.IsSet(wkproto.SettingStream) {
if m.StreamNo, err = dec.String(); err != nil {
return err
}
if m.StreamSeq, err = dec.Uint32(); err != nil {
return err
}
var streamFlag uint8
if streamFlag, err = dec.Uint8(); err != nil {
return err
}
m.StreamFlag = wkproto.StreamFlag(streamFlag)
}
// Timestamp
if m.Timestamp, err = dec.Int32(); err != nil {
return err
}
// FromUID
if m.FromUID, err = dec.String(); err != nil {
return err
}
// if m.QueueUID, err = dec.String(); err != nil {
// return err
// }
// ChannelID
if m.ChannelID, err = dec.String(); err != nil {
return err
}
// ChannelType
if m.ChannelType, err = dec.Uint8(); err != nil {
return err
}
// Payload
if m.Payload, err = dec.BinaryAll(); err != nil {
return err
}
return nil
}
type conversationResp struct {
ChannelID string `json:"channel_id"` // 频道ID
ChannelType uint8 `json:"channel_type"` // 频道类型
Unread int `json:"unread"` // 未读数
Timestamp int64 `json:"timestamp"`
LastMessage *MessageResp `json:"last_message"` // 最后一条消息
}
// MessageRespSlice MessageRespSlice
type MessageRespSlice []*MessageResp
func (m MessageRespSlice) Len() int { return len(m) }
func (m MessageRespSlice) Swap(i, j int) { m[i], m[j] = m[j], m[i] }
func (m MessageRespSlice) Less(i, j int) bool { return m[i].MessageSeq < m[j].MessageSeq }
// MessageResp 消息返回
type MessageResp struct {
Header MessageHeader `json:"header"` // 消息头
Setting uint8 `json:"setting"` // 设置
MessageID int64 `json:"message_id"` // 服务端的消息ID(全局唯一)
MessageIDStr string `json:"message_idstr"` // 服务端的消息ID(全局唯一)
ClientMsgNo string `json:"client_msg_no"` // 客户端消息唯一编号
StreamNo string `json:"stream_no,omitempty"` // 客户端消息唯一编号
StreamSeq uint32 `json:"stream_seq,omitempty"` // 客户端消息唯一编号
StreamFlag wkproto.StreamFlag `json:"stream_flag,omitempty"` // 客户端消息唯一编号
MessageSeq uint32 `json:"message_seq"` // 消息序列号 (用户唯一,有序递增)
FromUID string `json:"from_uid"` // 发送者UID
ChannelID string `json:"channel_id"` // 频道ID
ChannelType uint8 `json:"channel_type"` // 频道类型
Topic string `json:"topic"` // 话题ID
Timestamp int32 `json:"timestamp"` // 服务器消息时间戳(10位,到秒)
Payload []byte `json:"payload"` // 消息内容
Streams []*StreamItemResp `json:"streams,omitempty"` // 消息流内容
}
func (m *MessageResp) from(messageD *Message) {
m.Header.NoPersist = wkutil.BoolToInt(messageD.NoPersist)
m.Header.RedDot = wkutil.BoolToInt(messageD.RedDot)
m.Header.SyncOnce = wkutil.BoolToInt(messageD.SyncOnce)
m.Setting = messageD.Setting.Uint8()
m.MessageID = messageD.MessageID
m.MessageIDStr = strconv.FormatInt(messageD.MessageID, 10)
m.ClientMsgNo = messageD.ClientMsgNo
m.StreamNo = messageD.StreamNo
m.StreamSeq = messageD.StreamSeq
m.StreamFlag = messageD.StreamFlag
m.MessageSeq = messageD.MessageSeq
m.FromUID = messageD.FromUID
m.Timestamp = messageD.Timestamp
realChannelID := messageD.ChannelID
if messageD.ChannelType == wkproto.ChannelTypePerson {
if strings.Contains(messageD.ChannelID, "@") {
channelIDs := strings.Split(messageD.ChannelID, "@")
for _, channelID := range channelIDs {
if messageD.FromUID != channelID {
realChannelID = channelID
}
}
}
}
m.ChannelID = realChannelID
m.ChannelType = messageD.ChannelType
m.Topic = messageD.Topic
m.Payload = messageD.Payload
}
type StreamItemResp struct {
StreamSeq uint32 `json:"stream_seq"` // 流序号
ClientMsgNo string `json:"client_msg_no"` // 客户端消息唯一编号
Blob []byte `json:"blob"` // 消息内容
}
func newStreamItemResp(m *wkstore.StreamItem) *StreamItemResp {
return &StreamItemResp{
StreamSeq: m.StreamSeq,
ClientMsgNo: m.ClientMsgNo,
Blob: m.Blob,
}
}
type MessageOfflineNotify struct {
MessageResp
ToUIDs []string `json:"to_uids"`
Compress string `json:"compress,omitempty"` // 压缩ToUIDs 如果为空 表示不压缩 为gzip则采用gzip压缩
CompresssToUIDs []byte `json:"compress_to_uids,omitempty"` // 已压缩的to_uids
SourceID int64 `json:"source_id,omitempty"` // 来源节点ID
}
// MessageHeader Message header
type MessageHeader struct {
NoPersist int `json:"no_persist"` // Is it not persistent
RedDot int `json:"red_dot"` // Whether to show red dot
SyncOnce int `json:"sync_once"` // This message is only synchronized or consumed once
}
type clearConversationUnreadReq struct {
UID string `json:"uid"`
ChannelID string `json:"channel_id"`
ChannelType uint8 `json:"channel_type"`
MessageSeq uint32 `json:"message_seq"` // messageSeq 只有超大群才会传 因为超大群最近会话服务器不会维护,需要客户端传递messageSeq进行主动维护
}
func (req clearConversationUnreadReq) Check() error {
if req.UID == "" {
return errors.New("uid cannot be empty")
}
if req.ChannelID == "" || req.ChannelType == 0 {
return errors.New("channel_id or channel_type cannot be empty")
}
return nil
}
type deleteChannelReq struct {
UID string `json:"uid"`
ChannelID string `json:"channel_id"`
ChannelType uint8 `json:"channel_type"`
}
func (req deleteChannelReq) Check() error {
if len(req.UID) <= 0 {
return errors.New("Uid cannot be empty")
}
if req.ChannelID == "" || req.ChannelType == 0 {
return errors.New("channel_id or channel_type cannot be empty")
}
return nil
}
type syncUserConversationResp struct {
ChannelID string `json:"channel_id"` // 频道ID
ChannelType uint8 `json:"channel_type"` // 频道类型
Unread int `json:"unread"` // 未读消息
Timestamp int64 `json:"timestamp"` // 最后一次会话时间
LastMsgSeq uint32 `json:"last_msg_seq"` // 最后一条消息seq
LastClientMsgNo string `json:"last_client_msg_no"` // 最后一次消息客户端编号
OffsetMsgSeq int64 `json:"offset_msg_seq"` // 偏移位的消息seq
Version int64 `json:"version"` // 数据版本
Recents []*MessageResp `json:"recents"` // 最近N条消息
}
func newSyncUserConversationResp(conversation *wkstore.Conversation) *syncUserConversationResp {
return &syncUserConversationResp{
ChannelID: conversation.ChannelID,
ChannelType: conversation.ChannelType,
Unread: conversation.UnreadCount,
Timestamp: conversation.Timestamp,
LastMsgSeq: conversation.LastMsgSeq,
LastClientMsgNo: conversation.LastClientMsgNo,
Version: conversation.Version,
}
}
type channelRecentMessageReq struct {
ChannelID string `json:"channel_id"`
ChannelType uint8 `json:"channel_type"`
LastMsgSeq uint32 `json:"last_msg_seq"`
}
type channelRecentMessage struct {
ChannelID string `json:"channel_id"`
ChannelType uint8 `json:"channel_type"`
Messages []*MessageResp `json:"messages"`
}
// MessageSendReq 消息发送请求
type MessageSendReq struct {
Header MessageHeader `json:"header"` // 消息头
ClientMsgNo string `json:"client_msg_no"` // 客户端消息编号(相同编号,客户端只会显示一条)
StreamNo string `json:"stream_no"` // 消息流编号
FromUID string `json:"from_uid"` // 发送者UID
ChannelID string `json:"channel_id"` // 频道ID
ChannelType uint8 `json:"channel_type"` // 频道类型
Subscribers []string `json:"subscribers"` // 订阅者 如果此字段有值,表示消息只发给指定的订阅者
Payload []byte `json:"payload"` // 消息内容
}
// Check 检查输入
func (m MessageSendReq) Check() error {
if m.Payload == nil || len(m.Payload) <= 0 {
return errors.New("payload不能为空!")
}
return nil
}
// ChannelInfoReq ChannelInfoReq
type ChannelInfoReq struct {
ChannelID string `json:"channel_id"` // 频道ID
ChannelType uint8 `json:"channel_type"` // 频道类型
Large int `json:"large"` // 是否是超大群
Ban int `json:"ban"` // 是否封禁频道(封禁后此频道所有人都将不能发消息,除了系统账号)
}
func (c ChannelInfoReq) ToChannelInfo() *wkstore.ChannelInfo {
return &wkstore.ChannelInfo{
ChannelID: c.ChannelID,
ChannelType: c.ChannelType,
Large: c.Large == 1,
Ban: c.Ban == 1,
}
}
type ChannelInfoResp struct {
Large int `json:"large"` // 是否是超大群
Ban int `json:"ban"` // 是否封禁频道(封禁后此频道所有人都将不能发消息,除了系统账号)
}
func (c ChannelInfoResp) ToChannelInfo() *wkstore.ChannelInfo {
return &wkstore.ChannelInfo{
Large: c.Large == 1,
Ban: c.Ban == 1,
}
}
// ChannelCreateReq 频道创建请求
type ChannelCreateReq struct {
ChannelInfoReq
Subscribers []string `json:"subscribers"` // 订阅者
}
// Check 检查请求参数
func (r ChannelCreateReq) Check() error {
if strings.TrimSpace(r.ChannelID) == "" {
return errors.New("频道ID不能为空!")
}
if r.ChannelType == 0 {
return errors.New("频道类型错误!")
}
return nil
}
type subscriberAddReq struct {
ChannelID string `json:"channel_id"` // 频道ID
ChannelType uint8 `json:"channel_type"` // 频道类型
Reset int `json:"reset"` // 是否重置订阅者 (0.不重置 1.重置),选择重置,将删除原来的所有成员
TempSubscriber int `json:"temp_subscriber"` // 是否是临时订阅者 (1. 是 0. 否)
Subscribers []string `json:"subscribers"` // 订阅者
}
func (s subscriberAddReq) Check() error {
if strings.TrimSpace(s.ChannelID) == "" {
return errors.New("频道ID不能为空!")
}
if stringArrayIsEmpty(s.Subscribers) {
return errors.New("订阅者不能为空!")
}
return nil
}
type subscriberRemoveReq struct {
ChannelID string `json:"channel_id"`
ChannelType uint8 `json:"channel_type"`
TempSubscriber int `json:"temp_subscriber"` // 是否是临时订阅者 (1. 是 0. 否)
Subscribers []string `json:"subscribers"`
}
func (s subscriberRemoveReq) Check() error {
if strings.TrimSpace(s.ChannelID) == "" {
return errors.New("频道ID不能为空!")
}
if stringArrayIsEmpty(s.Subscribers) {
return errors.New("订阅者不能为空!")
}
return nil
}
func stringArrayIsEmpty(array []string) bool {
if len(array) == 0 {
return true
}
emptyCount := 0
for _, a := range array {
if strings.TrimSpace(a) == "" {
emptyCount++
}
}
return emptyCount >= len(array)
}
type blacklistReq struct {
ChannelID string `json:"channel_id"` // 频道ID
ChannelType uint8 `json:"channel_type"` // 频道类型
UIDs []string `json:"uids"` // 订阅者
}
func (r blacklistReq) Check() error {
if r.ChannelID == "" {
return errors.New("channel_id不能为空!")
}
if r.ChannelType == 0 {
return errors.New("频道类型不能为0!")
}
if len(r.UIDs) <= 0 {
return errors.New("uids不能为空!")
}
return nil
}
// ChannelDeleteReq 删除频道请求
type ChannelDeleteReq struct {
ChannelID string `json:"channel_id"` // 频道ID
ChannelType uint8 `json:"channel_type"` // 频道类型
}
type whitelistReq struct {
ChannelID string `json:"channel_id"` // 频道ID
ChannelType uint8 `json:"channel_type"` // 频道类型
UIDs []string `json:"uids"` // 订阅者
}
func (r whitelistReq) Check() error {
if r.ChannelID == "" {
return errors.New("channel_id不能为空!")
}
if r.ChannelType == 0 {
return errors.New("频道类型不能为0!")
}
if stringArrayIsEmpty(r.UIDs) {
return errors.New("uids不能为空!")
}
return nil
}
type syncReq struct {
UID string `json:"uid"` // 用户uid
MessageSeq uint32 `json:"message_seq"` // 客户端最大消息序列号
Limit int `json:"limit"` // 消息数量限制
}
func (r syncReq) Check() error {
if strings.TrimSpace(r.UID) == "" {
return errors.New("用户uid不能为空!")
}
if r.Limit < 0 {
return errors.New("limit不能为负数!")
}
return nil
}
type syncMessageResp struct {
StartMessageSeq uint32 `json:"start_message_seq"` // 开始序列号
EndMessageSeq uint32 `json:"end_message_seq"` // 结束序列号
More int `json:"more"` // 是否还有更多 1.是 0.否
Messages []*MessageResp `json:"messages"` // 消息数据
}
type syncackReq struct {
// 用户uid
UID string `json:"uid"`
// 最后一次同步的message_seq
LastMessageSeq uint32 `json:"last_message_seq"`
}
func (s syncackReq) Check() error {
if strings.TrimSpace(s.UID) == "" {
return errors.New("用户UID不能为空!")
}
if s.LastMessageSeq == 0 {
return errors.New("最后一次messageSeq不能为0!")
}
return nil
}
type messageStreamStartReq struct {
Header MessageHeader `json:"header"` // 消息头
ClientMsgNo string `json:"client_msg_no"` // 客户端消息编号(相同编号,客户端只会显示一条)
FromUID string `json:"from_uid"` // 发送者UID
ChannelID string `json:"channel_id"` // 频道ID
ChannelType uint8 `json:"channel_type"` // 频道类型
Payload []byte `json:"payload"` // 消息内容
}
type messageStreamEndReq struct {
StreamNo string `json:"stream_no"` // 消息流编号
ChannelID string `json:"channel_id"` // 频道ID
ChannelType uint8 `json:"channel_type"` // 频道类型
}