@@ -222,30 +222,42 @@ func (c *Conversation) doMsgNew(c2v common.Cmd2Value) {
222
222
//var unreadMessages []*model_struct.LocalConversationUnreadMessage
223
223
var newMessages sdk_struct.NewMsgList
224
224
// var reactionMsgModifierList, reactionMsgDeleterList sdk_struct.NewMsgList
225
+
225
226
var isUnreadCount , isConversationUpdate , isHistory , isNotPrivate , isSenderConversationUpdate bool
227
+
226
228
conversationChangedSet := make (map [string ]* model_struct.LocalConversation )
227
229
newConversationSet := make (map [string ]* model_struct.LocalConversation )
228
230
conversationSet := make (map [string ]* model_struct.LocalConversation )
229
231
phConversationChangedSet := make (map [string ]* model_struct.LocalConversation )
230
232
phNewConversationSet := make (map [string ]* model_struct.LocalConversation )
233
+
231
234
log .ZDebug (ctx , "message come here conversation ch" , "conversation length" , len (allMsg ))
232
235
b := time .Now ()
236
+
233
237
onlineMap := make (map [onlineMsgKey ]struct {})
238
+
234
239
for conversationID , msgs := range allMsg {
235
240
log .ZDebug (ctx , "parse message in one conversation" , "conversationID" ,
236
241
conversationID , "message length" , len (msgs .Msgs ))
237
242
var insertMessage , selfInsertMessage , othersInsertMessage []* model_struct.LocalChatLog
238
243
var updateMessage []* model_struct.LocalChatLog
244
+
239
245
for _ , v := range msgs .Msgs {
240
246
log .ZDebug (ctx , "parse message " , "conversationID" , conversationID , "msg" , v )
241
247
isHistory = utils .GetSwitchFromOptions (v .Options , constant .IsHistory )
248
+
242
249
isUnreadCount = utils .GetSwitchFromOptions (v .Options , constant .IsUnreadCount )
250
+
243
251
isConversationUpdate = utils .GetSwitchFromOptions (v .Options , constant .IsConversationUpdate )
252
+
244
253
isNotPrivate = utils .GetSwitchFromOptions (v .Options , constant .IsNotPrivate )
254
+
245
255
isSenderConversationUpdate = utils .GetSwitchFromOptions (v .Options , constant .IsSenderConversationUpdate )
256
+
246
257
msg := & sdk_struct.MsgStruct {}
247
258
copier .Copy (msg , v )
248
259
msg .Content = string (v .Content )
260
+
249
261
var attachedInfo sdk_struct.AttachedInfoElem
250
262
_ = utils .JsonStringToStruct (v .AttachedInfo , & attachedInfo )
251
263
msg .AttachedInfoElem = & attachedInfo
@@ -255,7 +267,9 @@ func (c *Conversation) doMsgNew(c2v common.Cmd2Value) {
255
267
insertMessage = append (insertMessage , c .msgStructToLocalChatLog (msg ))
256
268
continue
257
269
}
270
+
258
271
msg .Status = constant .MsgStatusSendSuccess
272
+
259
273
//De-analyze data
260
274
err := c .msgHandleByContentType (msg )
261
275
if err != nil {
@@ -367,17 +381,21 @@ func (c *Conversation) doMsgNew(c2v common.Cmd2Value) {
367
381
368
382
}
369
383
}
384
+
385
+ //todo The lock granularity needs to be optimized to the conversation level.
370
386
c .conversationSyncMutex .Lock ()
371
387
defer c .conversationSyncMutex .Unlock ()
372
- //todo The lock granularity needs to be optimized to the conversation level.
388
+
373
389
list , err := c .db .GetAllConversationListDB (ctx )
374
390
if err != nil {
375
391
log .ZError (ctx , "GetAllConversationListDB" , err )
376
392
}
393
+
377
394
m := make (map [string ]* model_struct.LocalConversation )
378
395
listToMap (list , m )
379
396
log .ZDebug (ctx , "listToMap: " , "local conversation" , list , "generated c map" ,
380
397
string (stringutil .StructToJsonBytes (conversationSet )))
398
+
381
399
c .diff (ctx , m , conversationSet , conversationChangedSet , newConversationSet )
382
400
log .ZInfo (ctx , "trigger map is :" , "newConversations" , string (stringutil .StructToJsonBytes (newConversationSet )),
383
401
"changedConversations" , string (stringutil .StructToJsonBytes (conversationChangedSet )))
@@ -429,7 +447,7 @@ func (c *Conversation) doMsgNew(c2v common.Cmd2Value) {
429
447
log .ZDebug (ctx , "before trigger msg" , "cost time" , time .Since (b ).Seconds (), "len" , len (allMsg ))
430
448
431
449
if c .batchMsgListener () != nil {
432
- c .batchNewMessages (ctx , newMessages )
450
+ c .batchNewMessages (ctx , newMessages , conversationChangedSet , newConversationSet , onlineMap )
433
451
} else {
434
452
c .newMessage (ctx , newMessages , conversationChangedSet , newConversationSet , onlineMap )
435
453
}
@@ -451,6 +469,7 @@ func (c *Conversation) doMsgNew(c2v common.Cmd2Value) {
451
469
}
452
470
}
453
471
}
472
+
454
473
log .ZDebug (ctx , "insert msg" , "cost time" , time .Since (b ).Seconds (), "len" , len (allMsg ))
455
474
}
456
475
@@ -886,15 +905,52 @@ func (c *Conversation) newMessage(ctx context.Context, newMessagesList sdk_struc
886
905
}
887
906
}
888
907
889
- func (c * Conversation ) batchNewMessages (ctx context.Context , newMessagesList sdk_struct.NewMsgList ) {
890
- sort .Sort (newMessagesList )
891
- if len (newMessagesList ) > 0 {
892
- c .batchMsgListener ().OnRecvNewMessages (utils .StructToJsonString (newMessagesList ))
893
- //if c.IsBackground {
894
- // c.batchMsgListener.OnRecvOfflineNewMessages(utils.StructToJsonString(newMessagesList))
895
- //}
908
+ func (c * Conversation ) batchNewMessages (ctx context.Context , newMessagesList sdk_struct.NewMsgList , conversationChanged , newConversation map [string ]* model_struct.LocalConversation , onlineMsg map [onlineMsgKey ]struct {}) {
909
+ if len (newMessagesList ) == 0 {
910
+ log .ZWarn (ctx , "newMessagesList is empty" , errs .New ("newMessagesList is empty" ))
911
+ return
896
912
}
897
913
914
+ sort .Sort (newMessagesList )
915
+ var needNotificationMsgList sdk_struct.NewMsgList
916
+
917
+ // offline
918
+ if c .GetBackground () {
919
+ u , err := c .user .GetSelfUserInfo (ctx )
920
+ if err != nil {
921
+ log .ZWarn (ctx , "GetSelfUserInfo err" , err )
922
+ }
923
+
924
+ if u .GlobalRecvMsgOpt != constant .ReceiveMessage {
925
+ return
926
+ }
927
+
928
+ for _ , w := range newMessagesList {
929
+ conversationID := utils .GetConversationIDByMsg (w )
930
+ if v , ok := conversationChanged [conversationID ]; ok && v .RecvMsgOpt == constant .ReceiveMessage {
931
+ needNotificationMsgList = append (needNotificationMsgList , w )
932
+ }
933
+ if v , ok := newConversation [conversationID ]; ok && v .RecvMsgOpt == constant .ReceiveMessage {
934
+ needNotificationMsgList = append (needNotificationMsgList , w )
935
+ }
936
+ }
937
+
938
+ if len (needNotificationMsgList ) != 0 {
939
+ c .batchMsgListener ().OnRecvOfflineNewMessages (utils .StructToJsonString (needNotificationMsgList ))
940
+ }
941
+ } else { // online
942
+ for _ , w := range newMessagesList {
943
+ if w .ContentType == constant .Typing {
944
+ continue
945
+ }
946
+
947
+ needNotificationMsgList = append (needNotificationMsgList , w )
948
+ }
949
+
950
+ if len (needNotificationMsgList ) != 0 {
951
+ c .batchMsgListener ().OnRecvNewMessages (utils .StructToJsonString (needNotificationMsgList ))
952
+ }
953
+ }
898
954
}
899
955
900
956
func (c * Conversation ) msgConvert (msg * sdk_struct.MsgStruct ) (err error ) {
0 commit comments