forked from openimsdk/openim-sdk-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
1000 lines (932 loc) · 36.4 KB
/
api.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
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package conversation_msg
import (
"context"
"fmt"
"net/url"
"os"
"path/filepath"
"sort"
"strings"
"sync"
"time"
"github.com/openimsdk/tools/errs"
"github.com/openimsdk/openim-sdk-core/v3/internal/third/file"
"github.com/openimsdk/openim-sdk-core/v3/open_im_sdk_callback"
"github.com/openimsdk/openim-sdk-core/v3/pkg/common"
"github.com/openimsdk/openim-sdk-core/v3/pkg/constant"
"github.com/openimsdk/openim-sdk-core/v3/pkg/content_type"
"github.com/openimsdk/openim-sdk-core/v3/pkg/db/model_struct"
"github.com/openimsdk/openim-sdk-core/v3/pkg/sdk_params_callback"
"github.com/openimsdk/openim-sdk-core/v3/pkg/sdkerrs"
"github.com/openimsdk/openim-sdk-core/v3/pkg/server_api_params"
"github.com/openimsdk/openim-sdk-core/v3/pkg/utils"
"github.com/openimsdk/openim-sdk-core/v3/sdk_struct"
"github.com/openimsdk/tools/log"
pbConversation "github.com/openimsdk/protocol/conversation"
"github.com/openimsdk/protocol/sdkws"
"github.com/jinzhu/copier"
)
func (c *Conversation) GetAllConversationList(ctx context.Context) ([]*model_struct.LocalConversation, error) {
return c.db.GetAllConversationListDB(ctx)
}
func (c *Conversation) GetConversationListSplit(ctx context.Context, offset, count int) ([]*model_struct.LocalConversation, error) {
return c.db.GetConversationListSplitDB(ctx, offset, count)
}
func (c *Conversation) HideConversation(ctx context.Context, conversationID string) error {
err := c.db.ResetConversation(ctx, conversationID)
if err != nil {
return err
}
return nil
}
func (c *Conversation) GetAtAllTag(_ context.Context) string {
return constant.AtAllString
}
func (c *Conversation) GetOneConversation(ctx context.Context, sessionType int32, sourceID string) (*model_struct.LocalConversation, error) {
conversationID := c.getConversationIDBySessionType(sourceID, int(sessionType))
lc, err := c.db.GetConversation(ctx, conversationID)
if err == nil {
return lc, nil
} else {
var newConversation model_struct.LocalConversation
newConversation.ConversationID = conversationID
newConversation.ConversationType = sessionType
switch sessionType {
case constant.SingleChatType:
newConversation.UserID = sourceID
faceUrl, name, err := c.getUserNameAndFaceURL(ctx, sourceID)
if err != nil {
return nil, err
}
newConversation.ShowName = name
newConversation.FaceURL = faceUrl
case constant.WriteGroupChatType, constant.ReadGroupChatType:
newConversation.GroupID = sourceID
g, err := c.group.FetchGroupOrError(ctx, sourceID)
if err != nil {
return nil, err
}
newConversation.ShowName = g.GroupName
newConversation.FaceURL = g.FaceURL
}
//double check if the conversation exists
lc, err := c.db.GetConversation(ctx, conversationID)
if err == nil {
return lc, nil
}
return &newConversation, nil
}
}
func (c *Conversation) GetMultipleConversation(ctx context.Context, conversationIDList []string) ([]*model_struct.LocalConversation, error) {
conversations, err := c.db.GetMultipleConversationDB(ctx, conversationIDList)
if err != nil {
return nil, err
}
return conversations, nil
}
func (c *Conversation) HideAllConversations(ctx context.Context) error {
err := c.db.ResetAllConversation(ctx)
if err != nil {
return err
}
return nil
}
func (c *Conversation) SetConversationDraft(ctx context.Context, conversationID, draftText string) error {
if draftText != "" {
err := c.db.SetConversationDraftDB(ctx, conversationID, draftText)
if err != nil {
return err
}
} else {
err := c.db.RemoveConversationDraft(ctx, conversationID, draftText)
if err != nil {
return err
}
}
_ = common.TriggerCmdUpdateConversation(ctx, common.UpdateConNode{Action: constant.ConChange, Args: []string{conversationID}}, c.GetCh())
return nil
}
func (c *Conversation) SetConversation(ctx context.Context, conversationID string, req *pbConversation.ConversationReq) error {
c.conversationSyncMutex.Lock()
defer c.conversationSyncMutex.Unlock()
lc, err := c.db.GetConversation(ctx, conversationID)
if err != nil {
return err
}
apiReq := &pbConversation.SetConversationsReq{Conversation: req}
err = c.setConversation(ctx, apiReq, lc)
if err != nil {
return err
}
return c.IncrSyncConversations(ctx)
}
func (c *Conversation) GetTotalUnreadMsgCount(ctx context.Context) (totalUnreadCount int32, err error) {
return c.db.GetTotalUnreadMsgCountDB(ctx)
}
func (c *Conversation) SetConversationListener(listener func() open_im_sdk_callback.OnConversationListener) {
c.ConversationListener = listener
}
func (c *Conversation) updateMsgStatusAndTriggerConversation(ctx context.Context, clientMsgID, serverMsgID string, sendTime int64, status int32, s *sdk_struct.MsgStruct,
lc *model_struct.LocalConversation, isOnlineOnly bool) {
log.ZDebug(ctx, "this is test send message ", "sendTime", sendTime, "status", status, "clientMsgID", clientMsgID, "serverMsgID", serverMsgID)
if isOnlineOnly {
return
}
s.SendTime = sendTime
s.Status = status
s.ServerMsgID = serverMsgID
err := c.db.UpdateMessageTimeAndStatus(ctx, lc.ConversationID, clientMsgID, serverMsgID, sendTime, status)
if err != nil {
log.ZWarn(ctx, "send message update message status error", err,
"sendTime", sendTime, "status", status, "clientMsgID", clientMsgID, "serverMsgID", serverMsgID)
}
err = c.db.DeleteSendingMessage(ctx, lc.ConversationID, clientMsgID)
if err != nil {
log.ZWarn(ctx, "send message delete sending message error", err)
}
lc.LatestMsg = utils.StructToJsonString(s)
lc.LatestMsgSendTime = sendTime
_ = common.TriggerCmdUpdateConversation(ctx, common.UpdateConNode{ConID: lc.ConversationID, Action: constant.AddConOrUpLatMsg, Args: *lc}, c.GetCh())
}
func (c *Conversation) fileName(ftype string, id string) string {
return fmt.Sprintf("msg_%s_%s", ftype, id)
}
func (c *Conversation) checkID(ctx context.Context, s *sdk_struct.MsgStruct,
recvID, groupID string, options map[string]bool) (*model_struct.LocalConversation, error) {
if recvID == "" && groupID == "" {
return nil, sdkerrs.ErrArgs
}
s.SendID = c.loginUserID
s.SenderPlatformID = c.platform
lc := &model_struct.LocalConversation{LatestMsgSendTime: s.CreateTime}
//assemble messages and conversations based on single or group chat types
if recvID == "" {
g, err := c.group.FetchGroupOrError(ctx, groupID)
if err != nil {
return nil, err
}
lc.ShowName = g.GroupName
lc.FaceURL = g.FaceURL
switch g.GroupType {
case constant.NormalGroup:
s.SessionType = constant.WriteGroupChatType
lc.ConversationType = constant.WriteGroupChatType
lc.ConversationID = c.getConversationIDBySessionType(groupID, constant.WriteGroupChatType)
case constant.SuperGroup, constant.WorkingGroup:
s.SessionType = constant.ReadGroupChatType
lc.ConversationID = c.getConversationIDBySessionType(groupID, constant.ReadGroupChatType)
lc.ConversationType = constant.ReadGroupChatType
}
s.GroupID = groupID
lc.GroupID = groupID
gm, err := c.db.GetGroupMemberInfoByGroupIDUserID(ctx, groupID, c.loginUserID)
if err == nil && gm != nil {
if gm.Nickname != "" {
s.SenderNickname = gm.Nickname
}
} else { //Maybe the group member information hasn't been pulled locally yet.
gm, err := c.group.GetSpecifiedGroupMembersInfo(ctx, groupID, []string{c.loginUserID})
if err == nil && gm != nil {
if gm[0].Nickname != "" {
s.SenderNickname = gm[0].Nickname
}
}
}
var attachedInfo sdk_struct.AttachedInfoElem
attachedInfo.GroupHasReadInfo.GroupMemberCount = g.MemberCount
s.AttachedInfoElem = &attachedInfo
} else {
s.SessionType = constant.SingleChatType
s.RecvID = recvID
lc.ConversationID = utils.GetConversationIDByMsg(s)
lc.UserID = recvID
lc.ConversationType = constant.SingleChatType
oldLc, err := c.db.GetConversation(ctx, lc.ConversationID)
if err == nil && oldLc.IsPrivateChat {
options[constant.IsNotPrivate] = false
var attachedInfo sdk_struct.AttachedInfoElem
attachedInfo.IsPrivateChat = true
attachedInfo.BurnDuration = oldLc.BurnDuration
s.AttachedInfoElem = &attachedInfo
}
if err != nil {
t := time.Now()
faceUrl, name, err := c.getUserNameAndFaceURL(ctx, recvID)
log.ZDebug(ctx, "GetUserNameAndFaceURL", "cost time", time.Since(t))
if err != nil {
return nil, err
}
lc.FaceURL = faceUrl
lc.ShowName = name
}
}
return lc, nil
}
func (c *Conversation) getConversationIDBySessionType(sourceID string, sessionType int) string {
switch sessionType {
case constant.SingleChatType:
l := []string{c.loginUserID, sourceID}
sort.Strings(l)
return "si_" + strings.Join(l, "_") // single chat
case constant.WriteGroupChatType:
return "g_" + sourceID // group chat
case constant.ReadGroupChatType:
return "sg_" + sourceID // super group chat
case constant.NotificationChatType:
return "sn_" + sourceID + "_" + c.loginUserID // server notification chat
}
return ""
}
func (c *Conversation) GetConversationIDBySessionType(_ context.Context, sourceID string, sessionType int) string {
return c.getConversationIDBySessionType(sourceID, sessionType)
}
func (c *Conversation) SendMessage(ctx context.Context, s *sdk_struct.MsgStruct, recvID, groupID string, p *sdkws.OfflinePushInfo, isOnlineOnly bool) (*sdk_struct.MsgStruct, error) {
filepathExt := func(name ...string) string {
for _, path := range name {
if ext := filepath.Ext(path); ext != "" {
return ext
}
}
return ""
}
options := make(map[string]bool, 2)
lc, err := c.checkID(ctx, s, recvID, groupID, options)
if err != nil {
return nil, err
}
callback, _ := ctx.Value("callback").(open_im_sdk_callback.SendMsgCallBack)
log.ZDebug(ctx, "before insert message is", "message", *s)
if !isOnlineOnly {
oldMessage, err := c.db.GetMessage(ctx, lc.ConversationID, s.ClientMsgID)
if err != nil {
localMessage := MsgStructToLocalChatLog(s)
err := c.db.InsertMessage(ctx, lc.ConversationID, localMessage)
if err != nil {
return nil, err
}
err = c.db.InsertSendingMessage(ctx, &model_struct.LocalSendingMessages{
ConversationID: lc.ConversationID,
ClientMsgID: localMessage.ClientMsgID,
})
if err != nil {
return nil, err
}
} else {
if oldMessage.Status != constant.MsgStatusSendFailed {
return nil, sdkerrs.ErrMsgRepeated
} else {
s.Status = constant.MsgStatusSending
err = c.db.InsertSendingMessage(ctx, &model_struct.LocalSendingMessages{
ConversationID: lc.ConversationID,
ClientMsgID: s.ClientMsgID,
})
if err != nil {
return nil, err
}
}
}
lc.LatestMsg = utils.StructToJsonString(s)
log.ZDebug(ctx, "send message come here", "conversion", *lc)
_ = common.TriggerCmdUpdateConversation(ctx, common.UpdateConNode{ConID: lc.ConversationID, Action: constant.AddConOrUpLatMsg, Args: *lc}, c.GetCh())
}
var delFile []string
//media file handle
switch s.ContentType {
case constant.Picture:
if s.Status == constant.MsgStatusSendSuccess {
s.Content = utils.StructToJsonString(s.PictureElem)
break
}
var sourcePath string
if utils.FileExist(s.PictureElem.SourcePath) {
sourcePath = s.PictureElem.SourcePath
delFile = append(delFile, utils.FileTmpPath(s.PictureElem.SourcePath, c.DataDir))
} else {
sourcePath = utils.FileTmpPath(s.PictureElem.SourcePath, c.DataDir)
delFile = append(delFile, sourcePath)
}
log.ZDebug(ctx, "send picture", "path", sourcePath)
res, err := c.file.UploadFile(ctx, &file.UploadFileReq{
ContentType: s.PictureElem.SourcePicture.Type,
Filepath: sourcePath,
Uuid: s.PictureElem.SourcePicture.UUID,
Name: c.fileName("picture", s.ClientMsgID) + filepathExt(s.PictureElem.SourcePicture.UUID, sourcePath),
Cause: "msg-picture",
}, NewUploadFileCallback(ctx, callback.OnProgress, s, lc.ConversationID, c.db))
if err != nil {
c.updateMsgStatusAndTriggerConversation(ctx, s.ClientMsgID, "", s.CreateTime, constant.MsgStatusSendFailed, s, lc, isOnlineOnly)
return nil, err
}
s.PictureElem.SourcePicture.Url = res.URL
s.PictureElem.BigPicture = s.PictureElem.SourcePicture
u, err := url.Parse(res.URL)
if err == nil {
snapshot := u.Query()
snapshot.Set("type", "image")
snapshot.Set("width", "640")
snapshot.Set("height", "640")
u.RawQuery = snapshot.Encode()
s.PictureElem.SnapshotPicture = &sdk_struct.PictureBaseInfo{
Width: 640,
Height: 640,
Url: u.String(),
}
} else {
log.ZError(ctx, "parse url failed", err, "url", res.URL, "err", err)
s.PictureElem.SnapshotPicture = s.PictureElem.SourcePicture
}
s.Content = utils.StructToJsonString(s.PictureElem)
case constant.Sound:
if s.Status == constant.MsgStatusSendSuccess {
s.Content = utils.StructToJsonString(s.SoundElem)
break
}
var sourcePath string
if utils.FileExist(s.SoundElem.SoundPath) {
sourcePath = s.SoundElem.SoundPath
delFile = append(delFile, utils.FileTmpPath(s.SoundElem.SoundPath, c.DataDir))
} else {
sourcePath = utils.FileTmpPath(s.SoundElem.SoundPath, c.DataDir)
delFile = append(delFile, sourcePath)
}
// log.Info("", "file", sourcePath, delFile)
res, err := c.file.UploadFile(ctx, &file.UploadFileReq{
ContentType: s.SoundElem.SoundType,
Filepath: sourcePath,
Uuid: s.SoundElem.UUID,
Name: c.fileName("voice", s.ClientMsgID) + filepathExt(s.SoundElem.UUID, sourcePath),
Cause: "msg-voice",
}, NewUploadFileCallback(ctx, callback.OnProgress, s, lc.ConversationID, c.db))
if err != nil {
c.updateMsgStatusAndTriggerConversation(ctx, s.ClientMsgID, "", s.CreateTime, constant.MsgStatusSendFailed, s, lc, isOnlineOnly)
return nil, err
}
s.SoundElem.SourceURL = res.URL
s.Content = utils.StructToJsonString(s.SoundElem)
case constant.Video:
if s.Status == constant.MsgStatusSendSuccess {
s.Content = utils.StructToJsonString(s.VideoElem)
break
}
var videoPath string
var snapPath string
if utils.FileExist(s.VideoElem.VideoPath) {
videoPath = s.VideoElem.VideoPath
snapPath = s.VideoElem.SnapshotPath
delFile = append(delFile, utils.FileTmpPath(s.VideoElem.VideoPath, c.DataDir))
delFile = append(delFile, utils.FileTmpPath(s.VideoElem.SnapshotPath, c.DataDir))
} else {
videoPath = utils.FileTmpPath(s.VideoElem.VideoPath, c.DataDir)
snapPath = utils.FileTmpPath(s.VideoElem.SnapshotPath, c.DataDir)
delFile = append(delFile, videoPath)
delFile = append(delFile, snapPath)
}
log.ZDebug(ctx, "file", "videoPath", videoPath, "snapPath", snapPath, "delFile", delFile)
var wg sync.WaitGroup
wg.Add(2)
var putErrs error
go func() {
defer wg.Done()
snapRes, err := c.file.UploadFile(ctx, &file.UploadFileReq{
ContentType: s.VideoElem.SnapshotType,
Filepath: snapPath,
Uuid: s.VideoElem.SnapshotUUID,
Name: c.fileName("videoSnapshot", s.ClientMsgID) + filepathExt(s.VideoElem.SnapshotUUID, snapPath),
Cause: "msg-video-snapshot",
}, nil)
if err != nil {
log.ZWarn(ctx, "upload video snapshot failed", err)
return
}
s.VideoElem.SnapshotURL = snapRes.URL
}()
go func() {
defer wg.Done()
res, err := c.file.UploadFile(ctx, &file.UploadFileReq{
ContentType: content_type.GetType(s.VideoElem.VideoType, filepath.Ext(s.VideoElem.VideoPath)),
Filepath: videoPath,
Uuid: s.VideoElem.VideoUUID,
Name: c.fileName("video", s.ClientMsgID) + filepathExt(s.VideoElem.VideoUUID, videoPath),
Cause: "msg-video",
}, NewUploadFileCallback(ctx, callback.OnProgress, s, lc.ConversationID, c.db))
if err != nil {
c.updateMsgStatusAndTriggerConversation(ctx, s.ClientMsgID, "", s.CreateTime, constant.MsgStatusSendFailed, s, lc, isOnlineOnly)
putErrs = err
return
}
if res != nil {
s.VideoElem.VideoURL = res.URL
}
}()
wg.Wait()
if err := putErrs; err != nil {
return nil, err
}
s.Content = utils.StructToJsonString(s.VideoElem)
case constant.File:
if s.Status == constant.MsgStatusSendSuccess {
s.Content = utils.StructToJsonString(s.FileElem)
break
}
name := s.FileElem.FileName
if name == "" {
name = s.FileElem.FilePath
}
if name == "" {
name = fmt.Sprintf("msg_file_%s.unknown", s.ClientMsgID)
}
var sourcePath string
if utils.FileExist(s.FileElem.FilePath) {
sourcePath = s.FileElem.FilePath
delFile = append(delFile, utils.FileTmpPath(s.FileElem.FilePath, c.DataDir))
} else {
sourcePath = utils.FileTmpPath(s.FileElem.FilePath, c.DataDir)
delFile = append(delFile, sourcePath)
}
res, err := c.file.UploadFile(ctx, &file.UploadFileReq{
ContentType: content_type.GetType(s.FileElem.FileType, filepath.Ext(s.FileElem.FilePath), filepath.Ext(s.FileElem.FileName)),
Filepath: sourcePath,
Uuid: s.FileElem.UUID,
Name: c.fileName("file", s.ClientMsgID) + "/" + filepath.Base(name),
Cause: "msg-file",
}, NewUploadFileCallback(ctx, callback.OnProgress, s, lc.ConversationID, c.db))
if err != nil {
c.updateMsgStatusAndTriggerConversation(ctx, s.ClientMsgID, "", s.CreateTime, constant.MsgStatusSendFailed, s, lc, isOnlineOnly)
return nil, err
}
s.FileElem.SourceURL = res.URL
s.Content = utils.StructToJsonString(s.FileElem)
case constant.Text:
s.Content = utils.StructToJsonString(s.TextElem)
case constant.AtText:
s.Content = utils.StructToJsonString(s.AtTextElem)
case constant.Location:
s.Content = utils.StructToJsonString(s.LocationElem)
case constant.Custom:
s.Content = utils.StructToJsonString(s.CustomElem)
case constant.Merger:
s.Content = utils.StructToJsonString(s.MergeElem)
case constant.Quote:
quoteMessage, err := c.db.GetMessage(ctx, lc.ConversationID, s.QuoteElem.QuoteMessage.ClientMsgID)
if err != nil {
log.ZWarn(ctx, "quote message not found", err)
}
s.QuoteElem.QuoteMessage.Seq = quoteMessage.Seq
s.Content = utils.StructToJsonString(s.QuoteElem)
case constant.Card:
s.Content = utils.StructToJsonString(s.CardElem)
case constant.Face:
s.Content = utils.StructToJsonString(s.FaceElem)
case constant.AdvancedText:
s.Content = utils.StructToJsonString(s.AdvancedTextElem)
default:
return nil, sdkerrs.ErrMsgContentTypeNotSupport
}
if utils.IsContainInt(int(s.ContentType), []int{constant.Picture, constant.Sound, constant.Video, constant.File}) {
if !isOnlineOnly {
localMessage := MsgStructToLocalChatLog(s)
log.ZDebug(ctx, "update message is ", "localMessage", localMessage)
err = c.db.UpdateMessage(ctx, lc.ConversationID, localMessage)
if err != nil {
return nil, err
}
}
}
return c.sendMessageToServer(ctx, s, lc, callback, delFile, p, options, isOnlineOnly)
}
func (c *Conversation) SendMessageNotOss(ctx context.Context, s *sdk_struct.MsgStruct, recvID, groupID string,
p *sdkws.OfflinePushInfo, isOnlineOnly bool) (*sdk_struct.MsgStruct, error) {
options := make(map[string]bool, 2)
lc, err := c.checkID(ctx, s, recvID, groupID, options)
if err != nil {
return nil, err
}
callback, _ := ctx.Value("callback").(open_im_sdk_callback.SendMsgCallBack)
if !isOnlineOnly {
oldMessage, err := c.db.GetMessage(ctx, lc.ConversationID, s.ClientMsgID)
if err != nil {
localMessage := MsgStructToLocalChatLog(s)
err := c.db.InsertMessage(ctx, lc.ConversationID, localMessage)
if err != nil {
return nil, err
}
err = c.db.InsertSendingMessage(ctx, &model_struct.LocalSendingMessages{
ConversationID: lc.ConversationID,
ClientMsgID: localMessage.ClientMsgID,
})
if err != nil {
return nil, err
}
} else {
if oldMessage.Status != constant.MsgStatusSendFailed {
return nil, sdkerrs.ErrMsgRepeated
} else {
s.Status = constant.MsgStatusSending
err = c.db.InsertSendingMessage(ctx, &model_struct.LocalSendingMessages{
ConversationID: lc.ConversationID,
ClientMsgID: s.ClientMsgID,
})
if err != nil {
return nil, err
}
}
}
}
lc.LatestMsg = utils.StructToJsonString(s)
var delFile []string
switch s.ContentType {
case constant.Picture:
s.Content = utils.StructToJsonString(s.PictureElem)
case constant.Sound:
s.Content = utils.StructToJsonString(s.SoundElem)
case constant.Video:
s.Content = utils.StructToJsonString(s.VideoElem)
case constant.File:
s.Content = utils.StructToJsonString(s.FileElem)
case constant.Text:
s.Content = utils.StructToJsonString(s.TextElem)
case constant.AtText:
s.Content = utils.StructToJsonString(s.AtTextElem)
case constant.Location:
s.Content = utils.StructToJsonString(s.LocationElem)
case constant.Custom:
s.Content = utils.StructToJsonString(s.CustomElem)
case constant.Merger:
s.Content = utils.StructToJsonString(s.MergeElem)
case constant.Quote:
s.Content = utils.StructToJsonString(s.QuoteElem)
case constant.Card:
s.Content = utils.StructToJsonString(s.CardElem)
case constant.Face:
s.Content = utils.StructToJsonString(s.FaceElem)
case constant.AdvancedText:
s.Content = utils.StructToJsonString(s.AdvancedTextElem)
default:
return nil, sdkerrs.ErrMsgContentTypeNotSupport
}
if utils.IsContainInt(int(s.ContentType), []int{constant.Picture, constant.Sound, constant.Video, constant.File}) {
if isOnlineOnly {
localMessage := MsgStructToLocalChatLog(s)
err = c.db.UpdateMessage(ctx, lc.ConversationID, localMessage)
if err != nil {
return nil, err
}
}
}
return c.sendMessageToServer(ctx, s, lc, callback, delFile, p, options, isOnlineOnly)
}
func (c *Conversation) sendMessageToServer(ctx context.Context, s *sdk_struct.MsgStruct, lc *model_struct.LocalConversation, callback open_im_sdk_callback.SendMsgCallBack,
delFiles []string, offlinePushInfo *sdkws.OfflinePushInfo, options map[string]bool, isOnlineOnly bool) (*sdk_struct.MsgStruct, error) {
if isOnlineOnly {
utils.SetSwitchFromOptions(options, constant.IsHistory, false)
utils.SetSwitchFromOptions(options, constant.IsPersistent, false)
utils.SetSwitchFromOptions(options, constant.IsSenderSync, false)
utils.SetSwitchFromOptions(options, constant.IsConversationUpdate, false)
utils.SetSwitchFromOptions(options, constant.IsSenderConversationUpdate, false)
utils.SetSwitchFromOptions(options, constant.IsUnreadCount, false)
utils.SetSwitchFromOptions(options, constant.IsOfflinePush, false)
}
//Protocol conversion
var wsMsgData sdkws.MsgData
copier.Copy(&wsMsgData, s)
wsMsgData.AttachedInfo = utils.StructToJsonString(s.AttachedInfoElem)
wsMsgData.Content = []byte(s.Content)
wsMsgData.CreateTime = s.CreateTime
wsMsgData.SendTime = 0
wsMsgData.Options = options
if wsMsgData.ContentType == constant.AtText {
wsMsgData.AtUserIDList = s.AtTextElem.AtUserList
}
wsMsgData.OfflinePushInfo = offlinePushInfo
s.Content = ""
var sendMsgResp sdkws.UserSendMsgResp
err := c.LongConnMgr.SendReqWaitResp(ctx, &wsMsgData, constant.SendMsg, &sendMsgResp)
if err != nil {
//if send message network timeout need to double-check message has received by db.
if sdkerrs.ErrNetworkTimeOut.Is(err) && !isOnlineOnly {
oldMessage, _ := c.db.GetMessage(ctx, lc.ConversationID, s.ClientMsgID)
if oldMessage.Status == constant.MsgStatusSendSuccess {
sendMsgResp.SendTime = oldMessage.SendTime
sendMsgResp.ClientMsgID = oldMessage.ClientMsgID
sendMsgResp.ServerMsgID = oldMessage.ServerMsgID
} else {
log.ZError(ctx, "send msg to server failed", err, "message", s)
c.updateMsgStatusAndTriggerConversation(ctx, s.ClientMsgID, "", s.CreateTime,
constant.MsgStatusSendFailed, s, lc, isOnlineOnly)
return s, err
}
} else {
log.ZError(ctx, "send msg to server failed", err, "message", s)
c.updateMsgStatusAndTriggerConversation(ctx, s.ClientMsgID, "", s.CreateTime,
constant.MsgStatusSendFailed, s, lc, isOnlineOnly)
return s, err
}
}
s.SendTime = sendMsgResp.SendTime
s.Status = constant.MsgStatusSendSuccess
s.ServerMsgID = sendMsgResp.ServerMsgID
go func() {
//remove media cache file
for _, file := range delFiles {
err := os.Remove(file)
if err != nil {
log.ZError(ctx, "delete temp File is failed", err, "filePath", file)
}
// log.ZDebug(ctx, "remove temp file:", "file", file)
}
c.updateMsgStatusAndTriggerConversation(ctx, sendMsgResp.ClientMsgID, sendMsgResp.ServerMsgID, sendMsgResp.SendTime, constant.MsgStatusSendSuccess, s, lc, isOnlineOnly)
}()
return s, nil
}
func (c *Conversation) FindMessageList(ctx context.Context, req []*sdk_params_callback.ConversationArgs) (*sdk_params_callback.FindMessageListCallback, error) {
var r sdk_params_callback.FindMessageListCallback
type tempConversationAndMessageList struct {
conversation *model_struct.LocalConversation
msgIDList []string
}
var s []*tempConversationAndMessageList
for _, conversationsArgs := range req {
localConversation, err := c.db.GetConversation(ctx, conversationsArgs.ConversationID)
if err != nil {
log.ZError(ctx, "GetConversation err", err, "conversationsArgs", conversationsArgs)
} else {
t := new(tempConversationAndMessageList)
t.conversation = localConversation
t.msgIDList = conversationsArgs.ClientMsgIDList
s = append(s, t)
}
}
for _, v := range s {
messages, err := c.db.GetMessagesByClientMsgIDs(ctx, v.conversation.ConversationID, v.msgIDList)
if err == nil {
var tempMessageList []*sdk_struct.MsgStruct
for _, message := range messages {
temp := LocalChatLogToMsgStruct(message)
tempMessageList = append(tempMessageList, temp)
}
findResultItem := sdk_params_callback.SearchByConversationResult{}
findResultItem.ConversationID = v.conversation.ConversationID
findResultItem.FaceURL = v.conversation.FaceURL
findResultItem.ShowName = v.conversation.ShowName
findResultItem.ConversationType = v.conversation.ConversationType
findResultItem.MessageList = tempMessageList
findResultItem.MessageCount = len(findResultItem.MessageList)
r.FindResultItems = append(r.FindResultItems, &findResultItem)
r.TotalCount += findResultItem.MessageCount
} else {
log.ZError(ctx, "GetMessagesByClientMsgIDs err", err, "conversationID", v.conversation.ConversationID, "msgIDList", v.msgIDList)
}
}
return &r, nil
}
func (c *Conversation) GetAdvancedHistoryMessageList(ctx context.Context, req sdk_params_callback.GetAdvancedHistoryMessageListParams) (*sdk_params_callback.GetAdvancedHistoryMessageListCallback, error) {
result, err := c.getAdvancedHistoryMessageList(ctx, req, false)
if err != nil {
return nil, err
}
if len(result.MessageList) == 0 {
s := make([]*sdk_struct.MsgStruct, 0)
result.MessageList = s
}
return result, nil
}
func (c *Conversation) GetAdvancedHistoryMessageListReverse(ctx context.Context, req sdk_params_callback.GetAdvancedHistoryMessageListParams) (*sdk_params_callback.GetAdvancedHistoryMessageListCallback, error) {
result, err := c.getAdvancedHistoryMessageList(ctx, req, true)
if err != nil {
return nil, err
}
if len(result.MessageList) == 0 {
s := make([]*sdk_struct.MsgStruct, 0)
result.MessageList = s
}
return result, nil
}
func (c *Conversation) RevokeMessage(ctx context.Context, conversationID, clientMsgID string) error {
return c.revokeOneMessage(ctx, conversationID, clientMsgID)
}
func (c *Conversation) TypingStatusUpdate(ctx context.Context, recvID, msgTip string) error {
return c.typingStatusUpdate(ctx, recvID, msgTip)
}
func (c *Conversation) MarkConversationMessageAsRead(ctx context.Context, conversationID string) error {
return c.markConversationMessageAsRead(ctx, conversationID)
}
func (c *Conversation) MarkAllConversationMessageAsRead(ctx context.Context) error {
conversationIDs, err := c.db.FindAllUnreadConversationConversationID(ctx)
if err != nil {
return err
}
for _, conversationID := range conversationIDs {
if err = c.markConversationMessageAsRead(ctx, conversationID); err != nil {
return err
}
}
return nil
}
// deprecated
func (c *Conversation) MarkMessagesAsReadByMsgID(ctx context.Context, conversationID string, clientMsgIDs []string) error {
return c.markMessagesAsReadByMsgID(ctx, conversationID, clientMsgIDs)
}
func (c *Conversation) DeleteMessageFromLocalStorage(ctx context.Context, conversationID string, clientMsgID string) error {
return c.deleteMessageFromLocal(ctx, conversationID, clientMsgID)
}
func (c *Conversation) DeleteMessage(ctx context.Context, conversationID string, clientMsgID string) error {
return c.deleteMessage(ctx, conversationID, clientMsgID)
}
func (c *Conversation) DeleteAllMsgFromLocalAndServer(ctx context.Context) error {
return c.deleteAllMsgFromLocalAndServer(ctx)
}
func (c *Conversation) DeleteAllMessageFromLocalStorage(ctx context.Context) error {
return c.deleteAllMsgFromLocal(ctx, true)
}
func (c *Conversation) ClearConversationAndDeleteAllMsg(ctx context.Context, conversationID string) error {
return c.clearConversationFromLocalAndServer(ctx, conversationID, c.db.ClearConversation)
}
func (c *Conversation) DeleteConversationAndDeleteAllMsg(ctx context.Context, conversationID string) error {
return c.clearConversationFromLocalAndServer(ctx, conversationID, c.db.ResetConversation)
}
func (c *Conversation) InsertSingleMessageToLocalStorage(ctx context.Context, s *sdk_struct.MsgStruct, recvID, sendID string) (*sdk_struct.MsgStruct, error) {
if recvID == "" || sendID == "" {
return nil, sdkerrs.ErrArgs
}
var conversation model_struct.LocalConversation
if sendID != c.loginUserID {
faceUrl, name, err := c.getUserNameAndFaceURL(ctx, sendID)
if err != nil {
//log.Error(operationID, "GetUserNameAndFaceURL err", err.Error(), sendID)
}
s.SenderFaceURL = faceUrl
s.SenderNickname = name
conversation.FaceURL = faceUrl
conversation.ShowName = name
conversation.UserID = sendID
conversation.ConversationID = c.getConversationIDBySessionType(sendID, constant.SingleChatType)
} else {
conversation.UserID = recvID
conversation.ConversationID = c.getConversationIDBySessionType(recvID, constant.SingleChatType)
_, err := c.db.GetConversation(ctx, conversation.ConversationID)
if err != nil {
faceUrl, name, err := c.getUserNameAndFaceURL(ctx, recvID)
if err != nil {
return nil, err
}
conversation.FaceURL = faceUrl
conversation.ShowName = name
}
}
s.SendID = sendID
s.RecvID = recvID
s.ClientMsgID = utils.GetMsgID(s.SendID)
s.SendTime = utils.GetCurrentTimestampByMill()
s.SessionType = constant.SingleChatType
s.Status = constant.MsgStatusSendSuccess
localMessage := MsgStructToLocalChatLog(s)
conversation.LatestMsg = utils.StructToJsonString(s)
conversation.ConversationType = constant.SingleChatType
conversation.LatestMsgSendTime = s.SendTime
err := c.insertMessageToLocalStorage(ctx, conversation.ConversationID, localMessage)
if err != nil {
return nil, err
}
_ = common.TriggerCmdUpdateConversation(ctx, common.UpdateConNode{ConID: conversation.ConversationID, Action: constant.AddConOrUpLatMsg, Args: conversation}, c.GetCh())
return s, nil
}
func (c *Conversation) InsertGroupMessageToLocalStorage(ctx context.Context, s *sdk_struct.MsgStruct, groupID, sendID string) (*sdk_struct.MsgStruct, error) {
if groupID == "" || sendID == "" {
return nil, sdkerrs.ErrArgs
}
var conversation model_struct.LocalConversation
var err error
_, conversation.ConversationType, err = c.getConversationTypeByGroupID(ctx, groupID)
if err != nil {
return nil, err
}
conversation.ConversationID = c.getConversationIDBySessionType(groupID, int(conversation.ConversationType))
if sendID != c.loginUserID {
faceUrl, name, err := c.getUserNameAndFaceURL(ctx, sendID)
if err != nil {
// log.Error("", "getUserNameAndFaceUrlByUid err", err.Error(), sendID)
}
s.SenderFaceURL = faceUrl
s.SenderNickname = name
}
s.SendID = sendID
s.RecvID = groupID
s.GroupID = groupID
s.ClientMsgID = utils.GetMsgID(s.SendID)
s.SendTime = utils.GetCurrentTimestampByMill()
s.SessionType = conversation.ConversationType
s.Status = constant.MsgStatusSendSuccess
localMessage := MsgStructToLocalChatLog(s)
conversation.LatestMsg = utils.StructToJsonString(s)
conversation.LatestMsgSendTime = s.SendTime
conversation.FaceURL = s.SenderFaceURL
conversation.ShowName = s.SenderNickname
err = c.insertMessageToLocalStorage(ctx, conversation.ConversationID, localMessage)
if err != nil {
return nil, err
}
_ = common.TriggerCmdUpdateConversation(ctx, common.UpdateConNode{ConID: conversation.ConversationID, Action: constant.AddConOrUpLatMsg, Args: conversation}, c.GetCh())
return s, nil
}
func (c *Conversation) SearchLocalMessages(ctx context.Context, searchParam *sdk_params_callback.SearchLocalMessagesParams) (*sdk_params_callback.SearchLocalMessagesCallback, error) {
searchParam.KeywordList = utils.TrimStringList(searchParam.KeywordList)
return c.searchLocalMessages(ctx, searchParam)
}
func (c *Conversation) SetMessageLocalEx(ctx context.Context, conversationID string, clientMsgID string, localEx string) error {
err := c.db.UpdateColumnsMessage(ctx, conversationID, clientMsgID, map[string]interface{}{"local_ex": localEx})
if err != nil {
return err
}
conversation, err := c.db.GetConversation(ctx, conversationID)
if err != nil {
return err
}
var latestMsg sdk_struct.MsgStruct
utils.JsonStringToStruct(conversation.LatestMsg, &latestMsg)
if latestMsg.ClientMsgID == clientMsgID {
log.ZDebug(ctx, "latestMsg local ex changed", "seq", latestMsg.Seq, "clientMsgID", latestMsg.ClientMsgID)
latestMsg.LocalEx = localEx
latestMsgStr := utils.StructToJsonString(latestMsg)
if err = c.db.UpdateColumnsConversation(ctx, conversationID, map[string]interface{}{"latest_msg": latestMsgStr, "latest_msg_send_time": latestMsg.SendTime}); err != nil {
return err
}
c.doUpdateConversation(common.Cmd2Value{Value: common.UpdateConNode{Action: constant.ConChange, Args: []string{conversationID}}})
}
return nil
}
func (c *Conversation) initBasicInfo(ctx context.Context, message *sdk_struct.MsgStruct, msgFrom, contentType int32) error {
message.CreateTime = utils.GetCurrentTimestampByMill()
message.SendTime = message.CreateTime
message.IsRead = false
message.Status = constant.MsgStatusSending
message.SendID = c.loginUserID
userInfo, err := c.user.GetUserInfoWithCache(ctx, c.loginUserID)
if err != nil {
return err
}
message.SenderFaceURL = userInfo.FaceURL
message.SenderNickname = userInfo.Nickname
ClientMsgID := utils.GetMsgID(message.SendID)
message.ClientMsgID = ClientMsgID
message.MsgFrom = msgFrom
message.ContentType = contentType
message.SenderPlatformID = c.platform
return nil
}
func (c *Conversation) getConversationTypeByGroupID(ctx context.Context, groupID string) (conversationID string, conversationType int32, err error) {
g, err := c.group.FetchGroupOrError(ctx, groupID)
if err != nil {
return "", 0, errs.WrapMsg(err, "get group info error")
}
switch g.GroupType {
case constant.NormalGroup:
return c.getConversationIDBySessionType(groupID, constant.WriteGroupChatType), constant.WriteGroupChatType, nil
case constant.SuperGroup, constant.WorkingGroup:
return c.getConversationIDBySessionType(groupID, constant.ReadGroupChatType), constant.ReadGroupChatType, nil
default:
return "", 0, sdkerrs.ErrGroupType
}
}
func (c *Conversation) SearchConversation(ctx context.Context, searchParam string) ([]*server_api_params.Conversation, error) {
// Check if search parameter is empty
if searchParam == "" {
return nil, sdkerrs.ErrArgs.WrapMsg("search parameter cannot be empty")
}
// Perform the search in your database or data source
// This is a placeholder for the actual database call
conversations, err := c.db.SearchConversations(ctx, searchParam)
if err != nil {
// Handle any errors that occurred during the search
return nil, err
}
apiConversations := make([]*server_api_params.Conversation, len(conversations))
for i, localConv := range conversations {
// Create new server_api_params.Conversation and map fields from localConv
apiConv := &server_api_params.Conversation{
ConversationID: localConv.ConversationID,
ConversationType: localConv.ConversationType,
UserID: localConv.UserID,
GroupID: localConv.GroupID,
RecvMsgOpt: localConv.RecvMsgOpt,
UnreadCount: localConv.UnreadCount,
DraftTextTime: localConv.DraftTextTime,
IsPinned: localConv.IsPinned,
IsPrivateChat: localConv.IsPrivateChat,
BurnDuration: localConv.BurnDuration,
GroupAtType: localConv.GroupAtType,
IsNotInGroup: localConv.IsNotInGroup,
UpdateUnreadCountTime: localConv.UpdateUnreadCountTime,
AttachedInfo: localConv.AttachedInfo,
Ex: localConv.Ex,
}
apiConversations[i] = apiConv
}
// Return the list of conversations
return apiConversations, nil
}
func (c *Conversation) GetInputStates(ctx context.Context, conversationID string, userID string) ([]int32, error) {
return c.typing.GetInputStates(conversationID, userID), nil
}
func (c *Conversation) ChangeInputStates(ctx context.Context, conversationID string, focus bool) error {
return c.typing.ChangeInputStates(ctx, conversationID, focus)
}