Skip to content

Commit 4823740

Browse files
committed
refactor: change the login and initialization logic to prevent the SDK from crashing due to being uninitialized or not logged in.
Signed-off-by: Gordon <[email protected]>
1 parent 514c760 commit 4823740

35 files changed

+531
-489
lines changed

internal/conversation_msg/api.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ func (c *Conversation) checkID(ctx context.Context, s *sdk_struct.MsgStruct,
181181
return nil, sdkerrs.ErrArgs
182182
}
183183
s.SendID = c.loginUserID
184-
s.SenderPlatformID = c.platformID
184+
s.SenderPlatformID = c.platform
185185
lc := &model_struct.LocalConversation{LatestMsgSendTime: s.CreateTime}
186186
//assemble messages and conversations based on single or group chat types
187187
if recvID == "" {
@@ -950,7 +950,7 @@ func (c *Conversation) initBasicInfo(ctx context.Context, message *sdk_struct.Ms
950950
message.ClientMsgID = ClientMsgID
951951
message.MsgFrom = msgFrom
952952
message.ContentType = contentType
953-
message.SenderPlatformID = c.platformID
953+
message.SenderPlatformID = c.platform
954954
return nil
955955
}
956956

internal/conversation_msg/conversation_msg.go

+19-9
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
"github.com/openimsdk/openim-sdk-core/v3/internal/third/file"
1919
"github.com/openimsdk/openim-sdk-core/v3/internal/user"
2020
"github.com/openimsdk/openim-sdk-core/v3/open_im_sdk_callback"
21-
"github.com/openimsdk/openim-sdk-core/v3/pkg/ccontext"
2221
"github.com/openimsdk/openim-sdk-core/v3/pkg/common"
2322
"github.com/openimsdk/openim-sdk-core/v3/pkg/constant"
2423
"github.com/openimsdk/openim-sdk-core/v3/pkg/db/db_interface"
@@ -58,7 +57,7 @@ type Conversation struct {
5857
recvCh chan common.Cmd2Value
5958
msgSyncerCh chan common.Cmd2Value
6059
loginUserID string
61-
platformID int32
60+
platform int32
6261
DataDir string
6362
relation *relation.Relation
6463
group *group.Group
@@ -79,6 +78,22 @@ type Conversation struct {
7978
typing *typing
8079
}
8180

81+
func (c *Conversation) SetDataBase(db db_interface.DataBase) {
82+
c.db = db
83+
}
84+
85+
func (c *Conversation) SetLoginUserID(loginUserID string) {
86+
c.loginUserID = loginUserID
87+
}
88+
89+
func (c *Conversation) SetPlatform(platform int32) {
90+
c.platform = platform
91+
}
92+
93+
func (c *Conversation) SetDataDir(DataDir string) {
94+
c.DataDir = DataDir
95+
}
96+
8297
func (c *Conversation) SetMsgListener(msgListener func() open_im_sdk_callback.OnAdvancedMsgListener) {
8398
c.msgListener = msgListener
8499
}
@@ -91,22 +106,17 @@ func (c *Conversation) SetBusinessListener(businessListener func() open_im_sdk_c
91106
c.businessListener = businessListener
92107
}
93108

94-
func NewConversation(ctx context.Context, longConnMgr *interaction.LongConnMgr, db db_interface.DataBase,
109+
func NewConversation(longConnMgr *interaction.LongConnMgr,
95110
recvCh, msgSyncerCh chan common.Cmd2Value, relation *relation.Relation, group *group.Group, user *user.User,
96111
file *file.File) *Conversation {
97-
info := ccontext.Info(ctx)
98-
n := &Conversation{db: db,
112+
n := &Conversation{
99113
LongConnMgr: longConnMgr,
100114
recvCh: recvCh,
101115
msgSyncerCh: msgSyncerCh,
102-
loginUserID: info.UserID(),
103-
platformID: info.PlatformID(),
104-
DataDir: info.DataDir(),
105116
relation: relation,
106117
group: group,
107118
user: user,
108119
file: file,
109-
IsExternalExtensions: info.IsExternalExtensions(),
110120
maxSeqRecorder: NewMaxSeqRecorder(),
111121
messagePullForwardEndSeqMap: cache.NewConversationSeqContextCache(),
112122
messagePullReverseEndSeqMap: cache.NewConversationSeqContextCache(),

internal/group/api.go

-39
Original file line numberDiff line numberDiff line change
@@ -95,18 +95,6 @@ func (g *Group) DismissGroup(ctx context.Context, groupID string) error {
9595
return nil
9696
}
9797

98-
//func (g *Group) SetGroupApplyMemberFriend(ctx context.Context, groupID string, rule int32) error {
99-
// return g.SetGroupInfo(ctx, &sdkws.GroupInfoForSet{GroupID: groupID, ApplyMemberFriend: wrapperspb.Int32(rule)})
100-
//}
101-
//
102-
//func (g *Group) SetGroupLookMemberInfo(ctx context.Context, groupID string, rule int32) error {
103-
// return g.SetGroupInfo(ctx, &sdkws.GroupInfoForSet{GroupID: groupID, LookMemberInfo: wrapperspb.Int32(rule)})
104-
//}
105-
//
106-
//func (g *Group) SetGroupVerification(ctx context.Context, groupID string, verification int32) error {
107-
// return g.SetGroupInfo(ctx, &sdkws.GroupInfoForSet{GroupID: groupID, NeedVerification: wrapperspb.Int32(verification)})
108-
//}
109-
11098
func (g *Group) ChangeGroupMute(ctx context.Context, groupID string, isMute bool) (err error) {
11199
if isMute {
112100
err = g.muteGroup(ctx, groupID)
@@ -183,14 +171,6 @@ func (g *Group) SetGroupMemberInfo(ctx context.Context, groupMemberInfo *group.S
183171
return g.IncrSyncGroupAndMember(ctx, groupMemberInfo.GroupID)
184172
}
185173

186-
//func (g *Group) SetGroupMemberRoleLevel(ctx context.Context, groupID, userID string, roleLevel int) error {
187-
// return g.SetGroupMemberInfo(ctx, &group.SetGroupMemberInfo{GroupID: groupID, UserID: userID, RoleLevel: wrapperspb.Int32(int32(roleLevel))})
188-
//}
189-
//
190-
//func (g *Group) SetGroupMemberNickname(ctx context.Context, groupID, userID string, groupMemberNickname string) error {
191-
// return g.SetGroupMemberInfo(ctx, &group.SetGroupMemberInfo{GroupID: groupID, UserID: userID, Nickname: wrapperspb.String(groupMemberNickname)})
192-
//}
193-
194174
func (g *Group) GetJoinedGroupList(ctx context.Context) ([]*model_struct.LocalGroup, error) {
195175
return g.db.GetJoinedGroupListDB(ctx)
196176
}
@@ -524,22 +504,3 @@ func (g *Group) HandlerGroupApplication(ctx context.Context, req *group.GroupApp
524504
func (g *Group) GetGroupMemberNameAndFaceURL(ctx context.Context, groupID string, userIDs []string) (map[string]*model_struct.LocalGroupMember, error) {
525505
return g.GetGroupMembersInfo(ctx, groupID, userIDs)
526506
}
527-
528-
//func (g *Group) SearchGroupMembersV2(ctx context.Context, req *group.SearchGroupMemberReq) ([]*model_struct.LocalGroupMember, error) {
529-
// if err := req.Check(); err != nil {
530-
// return nil, err
531-
// }
532-
// info, err := g.db.GetGroupInfoByGroupID(ctx, req.GroupID)
533-
// if err != nil {
534-
// return nil, err
535-
// }
536-
// if info.MemberCount <= pconstant.MaxSyncPullNumber {
537-
// return g.db.SearchGroupMembersDB(ctx, req.Keyword, req.GroupID, true, false,
538-
// int((req.Pagination.PageNumber-1)*req.Pagination.ShowNumber), int(req.Pagination.ShowNumber))
539-
// }
540-
// resp, err := util.CallApi[group.SearchGroupMemberResp](ctx, constant.SearchGroupMember, req)
541-
// if err != nil {
542-
// return nil, err
543-
// }
544-
// return datautil.Slice(resp.Members, g.pbGroupMemberToLocal), nil
545-
//}

internal/group/group.go

+11-3
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,9 @@ const (
4141
groupMemberSyncLimit = 1000
4242
)
4343

44-
func NewGroup(loginUserID string, db db_interface.DataBase,
44+
func NewGroup(
4545
conversationCh chan common.Cmd2Value) *Group {
4646
g := &Group{
47-
loginUserID: loginUserID,
48-
db: db,
4947
conversationCh: conversationCh,
5048
}
5149
g.initSyncer()
@@ -311,3 +309,13 @@ func (g *Group) FetchGroupOrError(ctx context.Context, groupID string) (*model_s
311309
func (g *Group) delLocalGroupRequest(ctx context.Context, groupID, userID string) error {
312310
return g.db.DeleteGroupRequest(ctx, groupID, userID)
313311
}
312+
313+
// SetDataBase sets the DataBase field in Group struct
314+
func (g *Group) SetDataBase(db db_interface.DataBase) {
315+
g.db = db
316+
}
317+
318+
// SetLoginUserID sets the loginUserID field in Group struct
319+
func (g *Group) SetLoginUserID(loginUserID string) {
320+
g.loginUserID = loginUserID
321+
}

internal/interaction/msg_sync.go

+13-14
Original file line numberDiff line numberDiff line change
@@ -63,28 +63,27 @@ type MsgSyncer struct {
6363

6464
}
6565

66+
func (m *MsgSyncer) SetLoginUserID(loginUserID string) {
67+
m.loginUserID = loginUserID
68+
}
69+
70+
func (m *MsgSyncer) SetDataBase(db db_interface.DataBase) {
71+
m.db = db
72+
}
73+
6674
// NewMsgSyncer creates a new instance of the message synchronizer.
67-
func NewMsgSyncer(ctx context.Context, conversationCh, recvCh chan common.Cmd2Value,
68-
loginUserID string, longConnMgr *LongConnMgr, db db_interface.DataBase, syncTimes int) (*MsgSyncer, error) {
69-
m := &MsgSyncer{
70-
loginUserID: loginUserID,
75+
func NewMsgSyncer(conversationCh, recvCh chan common.Cmd2Value,
76+
longConnMgr *LongConnMgr) *MsgSyncer {
77+
return &MsgSyncer{
7178
longConnMgr: longConnMgr,
7279
recvCh: recvCh,
7380
conversationCh: conversationCh,
74-
ctx: ctx,
7581
syncedMaxSeqs: make(map[string]int64),
76-
db: db,
77-
syncTimes: syncTimes,
78-
}
79-
if err := m.loadSeq(ctx); err != nil {
80-
log.ZError(ctx, "loadSeq err", err)
81-
return nil, err
8282
}
83-
return m, nil
8483
}
8584

86-
// seq The db reads the data to the memory,set syncedMaxSeqs
87-
func (m *MsgSyncer) loadSeq(ctx context.Context) error {
85+
// LoadSeq seq The db reads the data to the memory,set syncedMaxSeqs
86+
func (m *MsgSyncer) LoadSeq(ctx context.Context) error {
8887
conversationIDList, err := m.db.GetAllConversationIDList(ctx)
8988
if err != nil {
9089
log.ZError(ctx, "get conversation id list failed", err)

internal/relation/relation.go

+13-3
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ const (
2424
friendSyncLimit int64 = 10000
2525
)
2626

27-
func NewFriend(loginUserID string, db db_interface.DataBase, user *user.User, conversationCh chan common.Cmd2Value) *Relation {
28-
r := &Relation{loginUserID: loginUserID, db: db, user: user, conversationCh: conversationCh}
27+
func NewRelation(conversationCh chan common.Cmd2Value, user *user.User) *Relation {
28+
r := &Relation{conversationCh: conversationCh, user: user}
2929
r.initSyncer()
3030
return r
3131
}
@@ -56,7 +56,7 @@ func (r *Relation) initSyncer() {
5656
return r.db.DeleteFriendDB(ctx, value.FriendUserID)
5757
}),
5858
syncer.WithUpdate[*model_struct.LocalFriend, relation.GetPaginationFriendsResp, [2]string](func(ctx context.Context, server, local *model_struct.LocalFriend) error {
59-
r.user.UserCache.Delete(server.FriendUserID)
59+
r.user.UserCache().Delete(server.FriendUserID)
6060
return r.db.UpdateFriend(ctx, server)
6161
}),
6262
syncer.WithUUID[*model_struct.LocalFriend, relation.GetPaginationFriendsResp, [2]string](func(value *model_struct.LocalFriend) [2]string {
@@ -216,3 +216,13 @@ func (r *Relation) SetListener(listener func() open_im_sdk_callback.OnFriendship
216216
func (r *Relation) SetListenerForService(listener open_im_sdk_callback.OnListenerForService) {
217217
r.listenerForService = listener
218218
}
219+
220+
// SetDataBase sets the DataBase field in Relation struct
221+
func (r *Relation) SetDataBase(db db_interface.DataBase) {
222+
r.db = db
223+
}
224+
225+
// SetLoginUserID sets the loginUserID field in Relation struct
226+
func (r *Relation) SetLoginUserID(loginUserID string) {
227+
r.loginUserID = loginUserID
228+
}

internal/third/api.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010

1111
func (c *Third) UpdateFcmToken(ctx context.Context, fcmToken string, expireTime int64) error {
1212
return api.FcmUpdateToken.Execute(ctx, &third.FcmUpdateTokenReq{
13-
PlatformID: c.platformID,
13+
PlatformID: c.platform,
1414
FcmToken: fcmToken,
1515
Account: c.loginUserID,
1616
ExpireTime: expireTime,

internal/third/file/upload.go

+17-6
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,6 @@ import (
2121
"encoding/hex"
2222
"errors"
2323
"fmt"
24-
"github.com/openimsdk/openim-sdk-core/v3/pkg/api"
25-
"github.com/openimsdk/openim-sdk-core/v3/pkg/db/db_interface"
26-
"github.com/openimsdk/openim-sdk-core/v3/pkg/db/model_struct"
27-
"github.com/openimsdk/tools/errs"
2824
"io"
2925
"net/http"
3026
"net/url"
@@ -33,6 +29,11 @@ import (
3329
"sync/atomic"
3430
"time"
3531

32+
"github.com/openimsdk/openim-sdk-core/v3/pkg/api"
33+
"github.com/openimsdk/openim-sdk-core/v3/pkg/db/db_interface"
34+
"github.com/openimsdk/openim-sdk-core/v3/pkg/db/model_struct"
35+
"github.com/openimsdk/tools/errs"
36+
3637
"github.com/openimsdk/protocol/third"
3738
"github.com/openimsdk/tools/log"
3839
)
@@ -59,8 +60,8 @@ type partInfo struct {
5960
PartMd5s []string
6061
}
6162

62-
func NewFile(database db_interface.DataBase, loginUserID string) *File {
63-
return &File{database: database, loginUserID: loginUserID, confLock: &sync.Mutex{}, mapLocker: &sync.Mutex{}, uploading: make(map[string]*lockInfo)}
63+
func NewFile() *File {
64+
return &File{confLock: &sync.Mutex{}, mapLocker: &sync.Mutex{}, uploading: make(map[string]*lockInfo)}
6465
}
6566

6667
type File struct {
@@ -72,6 +73,16 @@ type File struct {
7273
uploading map[string]*lockInfo
7374
}
7475

76+
// SetDataBase sets the DataBase field in File struct
77+
func (f *File) SetDataBase(db db_interface.DataBase) {
78+
f.database = db
79+
}
80+
81+
// SetLoginUserID sets the loginUserID field in File struct
82+
func (f *File) SetLoginUserID(loginUserID string) {
83+
f.loginUserID = loginUserID
84+
}
85+
7586
type lockInfo struct {
7687
count int32
7788
locker sync.Locker

internal/third/log.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -107,15 +107,15 @@ func (c *Third) uploadLogs(ctx context.Context, line int, ex string, progress Pr
107107
return err
108108
}
109109
reqUpload := &file.UploadFileReq{Filepath: zippath, Name: fmt.Sprintf("sdk_log_%s_%s_%s_%s_%s",
110-
c.loginUserID, c.systemType, constant.PlatformID2Name[int(c.platformID)], version.Version, filepath.Base(zippath)), Cause: "sdklog", ContentType: "application/zip"}
110+
c.loginUserID, c.appFramework, constant.PlatformID2Name[int(c.platform)], version.Version, filepath.Base(zippath)), Cause: "sdklog", ContentType: "application/zip"}
111111
resp, err := c.fileUploader.UploadFile(ctx, reqUpload, &progressConvert{ctx: ctx, p: progress})
112112
if err != nil {
113113
return err
114114
}
115115
ccontext.Info(ctx)
116116
reqLog := &third.UploadLogsReq{
117-
Platform: c.platformID,
118-
AppFramework: c.systemType,
117+
Platform: c.platform,
118+
AppFramework: c.appFramework,
119119
Version: version.Version,
120120
FileURLs: []*third.FileURL{{Filename: zippath, URL: resp.URL}},
121121
Ex: ex,

internal/third/third.go

+22-5
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,36 @@
1515
package third
1616

1717
import (
18-
"github.com/openimsdk/openim-sdk-core/v3/internal/third/file"
1918
"sync"
19+
20+
"github.com/openimsdk/openim-sdk-core/v3/internal/third/file"
2021
)
2122

2223
type Third struct {
23-
platformID int32
24+
platform int32
2425
loginUserID string
25-
systemType string
26+
appFramework string
2627
LogFilePath string
2728
fileUploader *file.File
2829
logUploadLock sync.Mutex
2930
}
3031

31-
func NewThird(platformID int32, loginUserID, systemType, LogFilePath string, fileUploader *file.File) *Third {
32-
return &Third{platformID: platformID, loginUserID: loginUserID, systemType: systemType, LogFilePath: LogFilePath, fileUploader: fileUploader}
32+
func (t *Third) SetPlatform(platform int32) {
33+
t.platform = platform
34+
}
35+
36+
func (t *Third) SetLoginUserID(loginUserID string) {
37+
t.loginUserID = loginUserID
38+
}
39+
40+
func (t *Third) SetAppFramework(appFramework string) {
41+
t.appFramework = appFramework
42+
}
43+
44+
func (t *Third) SetLogFilePath(LogFilePath string) {
45+
t.LogFilePath = LogFilePath
46+
}
47+
48+
func NewThird(fileUploader *file.File) *Third {
49+
return &Third{fileUploader: fileUploader}
3350
}

0 commit comments

Comments
 (0)