@@ -127,10 +127,10 @@ type ChatMessage struct {
127
127
SessionType int `bson:"session_type" json:"session_type"`
128
128
129
129
// 发送人ID
130
- SenderID int64 `bson:"sender_id"`
130
+ SenderID int64 `bson:"sender_id" json:"sender_id" `
131
131
132
132
// 接收人ID
133
- ReceiverID int64 `bson:"receiver_id"`
133
+ ReceiverID int64 `bson:"receiver_id" json:"receiver_id" `
134
134
135
135
// 消息发送状态,1-已发送,2-未抵达,3-已抵达
136
136
SendStatus int `bson:"send_status" json:"send_status"`
@@ -278,10 +278,10 @@ func AddChatMessage(msg *ChatMessage) error {
278
278
err = jcache .Push (GlobCtx , cacheKey , msg )
279
279
if err != nil && err .Error () == "WRONGTYPE Operation against a key holding the wrong kind of value" {
280
280
jcache .Del (GlobCtx , cacheKey )
281
- err = jcache . Push ( GlobCtx , cacheKey , msg )
282
- if err != nil {
283
- log . Warn (). Err ( err ). Msg ( "缓存插入聊天消息失败" )
284
- }
281
+ }
282
+
283
+ if err == nil {
284
+ jcache . Expire ( GlobCtx , cacheKey , jcache . RandomExpirationDuration ())
285
285
}
286
286
287
287
return nil
@@ -326,12 +326,6 @@ func AddChatMessageTx(msg *ChatMessage) error {
326
326
return nil , errors .Wrap (err )
327
327
}
328
328
329
- // 将聊天数据推送到缓存定长队列中去
330
- err = jcache .Push (GlobCtx , cacheKeyFormatLastMessageList (msg .RoomID , msg .SessionType ), msg )
331
- if err != nil {
332
- log .Error ().Err (err ).Msgf ("推送消息到缓存列表失败:\n %+v" , err )
333
- }
334
-
335
329
// 更新聊天室的最后一条消息
336
330
_ , err = db .Collection (CollectionRoom ).
337
331
UpdateOne (ctxb , bson.M {
@@ -344,6 +338,18 @@ func AddChatMessageTx(msg *ChatMessage) error {
344
338
if err != nil {
345
339
return nil , errors .Wrap (err )
346
340
}
341
+
342
+ // 将聊天数据推送到缓存定长队列中去
343
+ cacheKey := cacheKeyFormatLastMessageList (msg .RoomID , msg .SessionType )
344
+ err = jcache .Push (GlobCtx , cacheKey , msg )
345
+ if err != nil {
346
+ jcache .Del (GlobCtx , cacheKey )
347
+ log .Error ().Err (err ).Msgf ("推送消息到缓存列表失败:\n %+v" , err )
348
+ }
349
+ if err == nil {
350
+ jcache .Expire (GlobCtx , cacheKey , jcache .RandomExpirationDuration ())
351
+ }
352
+
347
353
return nil , nil
348
354
}, options .Transaction ().SetWriteConcern (writeconcern .Majority ()).SetReadConcern (readconcern .Snapshot ()))
349
355
if err != nil {
@@ -416,38 +422,40 @@ func NewChatMessageListOptions() *GetChatMessageListOptions {
416
422
}
417
423
}
418
424
425
+ type GetChatMessageListFilter struct {
426
+ RoomID string `bson:"room_id"`
427
+ SessionType int `bson:"session_type"`
428
+ Sort any `bson:"sort"`
429
+ LastMessageID * int64 `bson:"last_message_id"`
430
+ Limit * int `bson:"limit"`
431
+ }
432
+
419
433
// GetChatMessageList 获取消息列表
420
- func GetChatMessageList (roomID string , sessionType int , opts ... * GetChatMessageListOptions ) ([]* ChatMessage , error ) {
421
- opt := NewChatMessageListOptions ()
434
+ func GetChatMessageList (filter * GetChatMessageListFilter , opts ... * GetOptions ) ([]* ChatMessage , error ) {
435
+ if filter .Sort == nil {
436
+ filter .Sort = bson.M {"message_id" : - 1 }
437
+ }
422
438
423
- for _ , o := range opts {
424
- if opt .Sort == nil && o .Sort != nil {
425
- opt .Sort = o .Sort
426
- }
427
- if o .LastMessageID > opt .LastMessageID {
428
- opt .LastMessageID = o .LastMessageID
429
- }
430
- if o .Limit > opt .Limit {
431
- opt .Limit = o .Limit
439
+ if filter .LastMessageID == nil {
440
+ filter .LastMessageID = new (int64 )
441
+ }
432
442
433
- if opt .Limit > defaultMaxLimit {
434
- opt .Limit = defaultMaxLimit
435
- }
436
- }
443
+ if filter .Limit == nil {
444
+ filter .Limit = new (int )
437
445
}
438
446
439
447
rs , err := GlobDB .Mongo .Database (DatabaseMongodbIM ).
440
448
Collection (CollectionMessage ).
441
449
Find (GlobCtx , bson.M {
442
- "room_id" : roomID ,
443
- "session_type" : sessionType ,
450
+ "room_id" : filter . RoomID ,
451
+ "session_type" : filter . SessionType ,
444
452
"message_id" : bson.M {
445
- "$gte" : opt .LastMessageID ,
446
- "$lt" : opt .LastMessageID + opt .Limit ,
453
+ "$gte" : * filter .LastMessageID ,
454
+ "$lt" : ( * filter .LastMessageID ) + int64 ( * filter .Limit ) ,
447
455
},
448
- }, options .Find ().SetSort (opt .Sort ))
456
+ }, options .Find ().SetSort (filter .Sort ))
449
457
if err != nil {
450
- if err == mongo . ErrNoDocuments {
458
+ if errors . IsNoRecord ( err ) {
451
459
return nil , errors .Wrap (err )
452
460
}
453
461
return nil , errors .Wrap (err )
@@ -469,6 +477,7 @@ func GetChatMessageList(roomID string, sessionType int, opts ...*GetChatMessageL
469
477
func GetLastChatMessageList (roomID string , sessionType int , opts ... * GetOptions ) ([]* ChatMessage , error ) {
470
478
opt := MergeGetOptions (opts )
471
479
cacheKey := cacheKeyFormatLastMessageList (roomID , sessionType )
480
+
472
481
// 如果有使用缓存,则从缓存中获取
473
482
if opt .UseCache () {
474
483
exists , _ := jcache .Exists (GlobCtx , cacheKey )
@@ -491,7 +500,7 @@ func GetLastChatMessageList(roomID string, sessionType int, opts ...*GetOptions)
491
500
Find (GlobCtx , bson.M {
492
501
"room_id" : roomID ,
493
502
"session_type" : sessionType ,
494
- }, options .Find ().SetSort (bson.M {"created_at " : - 1 }).SetLimit (defaultLastLimit ))
503
+ }, options .Find ().SetSort (bson.M {"message_id " : - 1 }).SetLimit (defaultLastLimit ))
495
504
496
505
if err != nil {
497
506
if errors .Is (err , mongo .ErrNoDocuments ) {
@@ -528,6 +537,9 @@ func GetLastChatMessageList(roomID string, sessionType int, opts ...*GetOptions)
528
537
log .Warn ().Err (err ).Msg ("缓存插入聊天消息失败" )
529
538
}
530
539
}
540
+ if err == nil {
541
+ jcache .Expire (GlobCtx , cacheKey , jcache .RandomExpirationDuration ())
542
+ }
531
543
532
544
// 设置缓存
533
545
return messages , nil
0 commit comments