Skip to content

Commit 48ff4d5

Browse files
FGadvancermo3etwithchaoicey-yu
authored
refactor: friend and conversation update. (#708)
* fix: remove the conversation trigger to prevent waiting in a circular queue. Signed-off-by: Gordon <[email protected]> * refactor: remove files to pkg. Signed-off-by: Gordon <[email protected]> * optimizing the code * refactor: restruct friend module to relation. * refactor: conversation structure and code change. Signed-off-by: Gordon <[email protected]> * optimizing the code * optimizing the code * optimizing the code * new call * feat: new api call * db success log * new api invoke * fix: Bug fix for clearing unread messages. Signed-off-by: Gordon <[email protected]> * refactor: user package * refactor: full package * refactor: del license * test: fix statistics * refactor: user package * remove unused code * remove unused code * feat: api format * feat: api format * refactor: merge opensource code main into local branch. Signed-off-by: Gordon <[email protected]> * refactor: merge opensource code main into local branch. Signed-off-by: Gordon <[email protected]> * refactor: merge opensource code main into local branch. Signed-off-by: Gordon <[email protected]> * feat: api format * refactor: user * refactor: full module remove. Signed-off-by: Gordon <[email protected]> * feat: update setGroupInfo API to correct. * refactor: rename api function and add ExtractField function comment * refactor: update logging initialization to use constant from protocol package - Renamed the `log.InitLoggerFromConfig` function call in `init_login.go` to use the `pbConstant.PlatformID2Name` constant from the `protocol` package instead of the `constant.PlatformID2Name` constant from the `openim-sdk-core` package. This ensures consistency and avoids potential conflicts. feat: improve error handling in HTTP client - Updated the `get` and `postLogic` functions in `http_client.go` to use the `io.ReadAll` function instead of `ioutil.ReadAll` for reading the response body. This change improves error handling and ensures proper resource cleanup. refactor: remove unused import in new_http.go - Removed the unused import of `github.com/openimsdk/tools/errs` in `new_http.go` to clean up the code and improve maintainability. * refactor: full * refactor: update logging initialization to use constant from protocol package * feat: format code * Refactor: Update function name in conversation_msg package Rename the function `revokeMessageFromSvr` to `revokeMessageFromServer` in the `revoke.go` file of the `conversation_msg` package. This change ensures consistency with the naming convention used in the codebase. Refactor: Update function name in group package Rename the function `getGroupsInfoFromSvr` to `getGroupsInfoFromServer` in the `group.go` file of the `group` package. This change aligns with the naming convention used in the codebase. Refactor: Update function name in conversation_msg package Rename the function `getConversationsByIDsFromSvr` to `getConversationsByIDsFromServer` in the `sync.go` file of the `conversation_msg` package. This change ensures consistency with the naming convention used in the codebase. Refactor: Update function name in group package Rename the function `getGroupsInfoFromSvr` to `getGroupsInfoFromServer` in the `api.go` file of the `group` package. This change aligns with the naming convention used in the codebase. Refactor: Update function name in conversation_msg package Rename the function `clearConversationFromLocalAndSvr` to `clearConversationFromLocalAndServer` in the `delete.go` file of the `conversation_msg` package. This change ensures consistency with the naming convention used in the codebase. Refactor: Update function name in conversation_msg package Rename the function `deleteAllMsgFromLocalAndSvr` to `deleteAllMsgFromLocalAndServer` in the `delete.go` file of the `conversation_msg` package. This change ensures consistency with the naming convention used in the codebase. Refactor: Update function name in conversation_msg package Rename the function `deleteMessagesFromSvr` to `deleteMessagesFromServer` in the `delete.go` file of the `conversation_msg` package. This change ensures consistency with the naming convention used in the codebase. Refactor: Update function name in conversation_msg package Rename the function `getIncrementalConversationFromSvr` to `getIncrementalConversationFromServer` in the `incremental_sync.go` file of the `conversation_msg` package. This change ensures consistency with the naming convention used in the codebase. Refactor: Update function name in conversation_msg package Rename the function `getAllConversationListFromSvr` to `getAllConversationListFromServer` in the `incremental_sync.go` file of the `conversation_msg` package. This change ensures consistency with the naming convention used in the codebase. Refactor: Update function name in conversation_msg package Rename the function `getAllConversationIDsFromSvr` to `getAllConversationIDsFromServer` in the `incremental_sync.go` file of the `conversation_msg` package. This change ensures consistency with the naming convention used in the codebase. Refactor: Update function name in conversation_msg package Rename the function `syncMsgFromSvr` to `syncMsgFromServer` in the `msg_sync.go` file of the `interaction` package. This change ensures consistency with the naming convention used in the codebase. Refactor: Update function name in conversation_msg package Rename the function `syncMsgFromSvrSplit` to `syncMsgFromServerSplit` in the `msg_sync.go` file of the `interaction` package. This change ensures consistency with the naming convention used in the codebase. * Refactor function names in conversation_msg package * Refactor import paths in conversation_msg and indexdb packages * refactor: user * Refactor variable names and comments for clarity * refactor: full module remove. Signed-off-by: Gordon <[email protected]> * refactor: full module remove. Signed-off-by: Gordon <[email protected]> * Refactor import statements in api.go * feat: format code * feat: format code * feat: format code * Refactor database initialization and remove unused modules * remove unused import pkg. * feat: format code * Refactor conversation_msg package and remove unused imports * refactor: full module remove. Signed-off-by: Gordon <[email protected]> * Refactor file imports in conversation_msg package * Refactor file imports in conversation_msg package * Refactor file imports in event_listener package * Refactor file imports in conversation_msg package * refactor: cache * refactor: cache * refactor: cache * feat: add UpdateFriends interface in wasm. * move test to db folder. * replece olderst const. * refactor: del SetGlobalRecvMessageOpt * refactor: move user * refactor: http module remove. Signed-off-by: Gordon <[email protected]> * refactor: http module remove. Signed-off-by: Gordon <[email protected]> * refactor: http module remove. Signed-off-by: Gordon <[email protected]> * refactor: http module remove. Signed-off-by: Gordon <[email protected]> * remove main test. * refactor: http module remove. Signed-off-by: Gordon <[email protected]> * refactor: http module remove. Signed-off-by: Gordon <[email protected]> * refactor: http module remove. Signed-off-by: Gordon <[email protected]> * refactor: http module remove. Signed-off-by: Gordon <[email protected]> * refactor: remove chinese * refactor: remove chinese * Refactor group API to use simplified SetGroupInfo method * refactor: constant add. Signed-off-by: Gordon <[email protected]> * refactor: constant add. Signed-off-by: Gordon <[email protected]> * refactor: fix msgtest route * refactor: friend info update. Signed-off-by: Gordon <[email protected]> * feat: GetFriendListPage * fix: update router path in setGroupInfoEX. * refactor: friend info update. Signed-off-by: Gordon <[email protected]> * refactor: audio/mp4 update. Signed-off-by: Gordon <[email protected]> --------- Signed-off-by: Gordon <[email protected]> Co-authored-by: Monet Lee <[email protected]> Co-authored-by: withchao <[email protected]> Co-authored-by: icey-yu <[email protected]> Co-authored-by: icey-yu <[email protected]>
1 parent 2e37562 commit 48ff4d5

File tree

17 files changed

+115
-117
lines changed

17 files changed

+115
-117
lines changed

.DS_Store

-6 KB
Binary file not shown.

integration_test/internal/manager/msg_manager.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package manager
33
import (
44
"context"
55
"fmt"
6+
"time"
7+
68
"github.com/openimsdk/openim-sdk-core/v3/integration_test/internal/config"
79
"github.com/openimsdk/openim-sdk-core/v3/integration_test/internal/pkg/decorator"
810
"github.com/openimsdk/openim-sdk-core/v3/integration_test/internal/pkg/progress"
@@ -14,7 +16,6 @@ import (
1416
sdkUtils "github.com/openimsdk/openim-sdk-core/v3/pkg/utils"
1517
"github.com/openimsdk/tools/log"
1618
"github.com/openimsdk/tools/utils/datautil"
17-
"time"
1819
)
1920

2021
type TestMsgManager struct {
@@ -63,7 +64,7 @@ func (m *TestMsgManager) sendSingleMessages(ctx context.Context, gr *reerrgroup.
6364

6465
friends = datautil.ShuffleSlice(friends)
6566
for _, friend := range friends {
66-
if friend.FriendInfo != nil {
67+
if friend != nil {
6768
for i := 0; i < vars.SingleMessageNum; i++ {
6869
msg, err := testSDK.SDK.Conversation().CreateTextMessage(ctx,
6970
fmt.Sprintf("count %d:my userID is %s", i, testSDK.UserID))
@@ -73,7 +74,7 @@ func (m *TestMsgManager) sendSingleMessages(ctx context.Context, gr *reerrgroup.
7374
ctx = ccontext.WithOperationID(ctx, sdkUtils.OperationIDGenerator())
7475
t := time.Now()
7576
log.ZWarn(ctx, "sendSingleMessages begin", nil)
76-
_, err = testSDK.SendSingleMsg(ctx, msg, friend.FriendInfo.FriendUserID)
77+
_, err = testSDK.SendSingleMsg(ctx, msg, friend.FriendUserID)
7778
if err != nil {
7879
return err
7980
}

integration_test/internal/sdk/relation.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,15 @@ package sdk
33
import (
44
"context"
55

6-
"github.com/openimsdk/openim-sdk-core/v3/pkg/server_api_params"
6+
"github.com/openimsdk/openim-sdk-core/v3/pkg/db/model_struct"
77
)
88

9-
func (s *TestSDK) GetAllFriends(ctx context.Context) ([]*server_api_params.FullUserInfo, error) {
9+
func (s *TestSDK) GetAllFriends(ctx context.Context) ([]*model_struct.LocalFriend, error) {
1010
res, err := s.SDK.Relation().GetFriendList(ctx, false)
1111
if err != nil {
1212
return nil, err
1313
}
1414

15-
resp := []*server_api_params.FullUserInfo{}
15+
return res, nil
1616

17-
for _, v := range res {
18-
resp = append(resp, &server_api_params.FullUserInfo{FriendInfo: v})
19-
}
20-
21-
return resp, nil
2217
}

internal/conversation_msg/api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ func (c *Conversation) SetConversationDraft(ctx context.Context, conversationID,
126126
return nil
127127
}
128128

129-
func (c *Conversation) SetConversations(ctx context.Context, conversationID string, req *pbConversation.ConversationReq) error {
129+
func (c *Conversation) SetConversation(ctx context.Context, conversationID string, req *pbConversation.ConversationReq) error {
130130
c.conversationSyncMutex.Lock()
131131
defer c.conversationSyncMutex.Unlock()
132132

internal/group/api.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ func (g *Group) GetSpecifiedGroupMembersInfo(ctx context.Context, groupID string
305305

306306
_, err := g.db.GetVersionSync(ctx, g.groupAndMemberVersionTableName(), groupID)
307307
if err != nil {
308-
if errs.ErrRecordNotFound.Is(err) {
308+
if !errs.ErrRecordNotFound.Is(err) {
309309
return nil, err
310310
}
311311
err := g.IncrSyncGroupAndMember(ctx, groupID)
@@ -366,7 +366,7 @@ func (g *Group) GetGroupMemberList(ctx context.Context, groupID string, filter,
366366

367367
_, err := g.db.GetVersionSync(ctx, g.groupAndMemberVersionTableName(), groupID)
368368
if err != nil {
369-
if errs.ErrRecordNotFound.Is(err) {
369+
if !errs.ErrRecordNotFound.Is(err) {
370370
return nil, err
371371
}
372372
err := g.IncrSyncGroupAndMember(ctx, groupID)

internal/relation/api.go

Lines changed: 66 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ import (
1616
"github.com/openimsdk/tools/log"
1717
)
1818

19-
func (r *Relation) GetSpecifiedFriendsInfo(ctx context.Context, friendUserIDList []string) ([]*server_api_params.FullUserInfo, error) {
20-
datafetcher := datafetcher.NewDataFetcher(
19+
func (r *Relation) GetSpecifiedFriendsInfo(ctx context.Context, friendUserIDList []string, filterBlack bool) ([]*model_struct.LocalFriend, error) {
20+
dataFetcher := datafetcher.NewDataFetcher(
2121
r.db,
2222
r.friendListTableName(),
2323
r.loginUserID,
@@ -39,28 +39,31 @@ func (r *Relation) GetSpecifiedFriendsInfo(ctx context.Context, friendUserIDList
3939
return datautil.Batch(ServerFriendToLocalFriend, serverFriend), nil
4040
},
4141
)
42-
localFriendList, err := datafetcher.FetchMissingAndFillLocal(ctx, friendUserIDList)
42+
localFriendList, err := dataFetcher.FetchMissingAndFillLocal(ctx, friendUserIDList)
4343
if err != nil {
4444
return nil, err
4545
}
46-
46+
if !filterBlack {
47+
return localFriendList, nil
48+
}
4749
log.ZDebug(ctx, "GetDesignatedFriendsInfo", "localFriendList", localFriendList)
4850
blackList, err := r.db.GetBlackInfoList(ctx, friendUserIDList)
4951
if err != nil {
5052
return nil, err
5153
}
52-
log.ZDebug(ctx, "GetDesignatedFriendsInfo", "blackList", blackList)
53-
m := make(map[string]*model_struct.LocalBlack)
54-
for i, black := range blackList {
55-
m[black.BlockUserID] = blackList[i]
54+
if len(blackList) == 0 {
55+
return localFriendList, nil
5656
}
57-
res := make([]*server_api_params.FullUserInfo, 0, len(localFriendList))
57+
58+
log.ZDebug(ctx, "GetDesignatedFriendsInfo", "blackList", blackList)
59+
m := datautil.SliceSetAny(blackList, func(e *model_struct.LocalBlack) string {
60+
return e.BlockUserID
61+
})
62+
var res []*model_struct.LocalFriend
5863
for _, localFriend := range localFriendList {
59-
res = append(res, &server_api_params.FullUserInfo{
60-
PublicInfo: nil,
61-
FriendInfo: localFriend,
62-
BlackInfo: m[localFriend.FriendUserID],
63-
})
64+
if _, ok := m[localFriend.FriendUserID]; !ok {
65+
res = append(res, localFriend)
66+
}
6467
}
6568
return res, nil
6669
}
@@ -159,9 +162,6 @@ func (r *Relation) GetFriendList(ctx context.Context, filterBlack bool) ([]*mode
159162
if err != nil {
160163
return nil, err
161164
}
162-
if localFriendList == nil {
163-
localFriendList = []*model_struct.LocalFriend{}
164-
}
165165
if len(localFriendList) == 0 || !filterBlack {
166166
return localFriendList, nil
167167
}
@@ -172,33 +172,65 @@ func (r *Relation) GetFriendList(ctx context.Context, filterBlack bool) ([]*mode
172172
if len(localBlackList) == 0 {
173173
return localFriendList, nil
174174
}
175-
blackSet := make(map[string]struct{})
176-
for _, black := range localBlackList {
177-
blackSet[black.BlockUserID] = struct{}{}
178-
}
179-
res := localFriendList[:0]
180-
for i, friend := range localFriendList {
175+
blackSet := datautil.SliceSetAny(localBlackList, func(e *model_struct.LocalBlack) string {
176+
return e.BlockUserID
177+
})
178+
var res []*model_struct.LocalFriend
179+
for _, friend := range localFriendList {
181180
if _, ok := blackSet[friend.FriendUserID]; !ok {
182-
res = append(res, localFriendList[i])
181+
res = append(res, friend)
183182
}
184183
}
185184
return res, nil
186185
}
187186

188-
func (r *Relation) GetFriendListPage(ctx context.Context, offset, count int, filterBlack bool) ([]*model_struct.LocalFriend, error) {
189-
friends, err := r.GetFriendList(ctx, filterBlack)
187+
func (r *Relation) GetFriendListPage(ctx context.Context, offset, count int32, filterBlack bool) ([]*model_struct.LocalFriend, error) {
188+
dataFetcher := datafetcher.NewDataFetcher(
189+
r.db,
190+
r.friendListTableName(),
191+
r.loginUserID,
192+
func(localFriend *model_struct.LocalFriend) string {
193+
return localFriend.FriendUserID
194+
},
195+
func(ctx context.Context, values []*model_struct.LocalFriend) error {
196+
return r.db.BatchInsertFriend(ctx, values)
197+
},
198+
func(ctx context.Context, userIDs []string) ([]*model_struct.LocalFriend, bool, error) {
199+
localFriendList, err := r.db.GetFriendInfoList(ctx, userIDs)
200+
return localFriendList, true, err
201+
},
202+
func(ctx context.Context, userIDs []string) ([]*model_struct.LocalFriend, error) {
203+
serverFriend, err := r.GetDesignatedFriends(ctx, userIDs)
204+
if err != nil {
205+
return nil, err
206+
}
207+
return datautil.Batch(ServerFriendToLocalFriend, serverFriend), nil
208+
},
209+
)
210+
localBlackList, err := r.db.GetBlackListDB(ctx)
190211
if err != nil {
191212
return nil, err
192213
}
193-
if offset >= len(friends) {
194-
return friends[:0], nil
214+
if (!filterBlack) || len(localBlackList) == 0 {
215+
return dataFetcher.FetchWithPagination(ctx, int(offset), int(count))
216+
}
217+
localFriendList, err := dataFetcher.FetchWithPagination(ctx, int(offset), int(count*2))
218+
if err != nil {
219+
return nil, err
195220
}
196-
friends = friends[offset:]
197-
if len(friends) > count {
198-
return friends[:count], nil
199-
} else {
200-
return friends, nil
221+
blackUserIDs := datautil.SliceSetAny(localBlackList, func(e *model_struct.LocalBlack) string {
222+
return e.BlockUserID
223+
})
224+
res := localFriendList[:0]
225+
for _, friend := range localFriendList {
226+
if _, ok := blackUserIDs[friend.FriendUserID]; !ok {
227+
res = append(res, friend)
228+
}
229+
if len(res) == int(count) {
230+
break
231+
}
201232
}
233+
return res, nil
202234
}
203235

204236
func (r *Relation) SearchFriends(ctx context.Context, param *sdk.SearchFriendsParam) ([]*sdk.SearchFriendItem, error) {
@@ -256,6 +288,7 @@ func (r *Relation) GetBlackList(ctx context.Context) ([]*model_struct.LocalBlack
256288
}
257289

258290
func (r *Relation) UpdateFriends(ctx context.Context, req *relation.UpdateFriendsReq) error {
291+
req.OwnerUserID = r.loginUserID
259292
if err := r.updateFriends(ctx, req); err != nil {
260293
return err
261294
}

internal/user/api.go

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ func (u *User) GetSelfUserInfo(ctx context.Context) (*model_struct.LocalUser, er
8585

8686
func (u *User) SetSelfInfo(ctx context.Context, userInfo *sdkws.UserInfoWithEx) error {
8787
// updateSelfUserInfo updates the user's information with Ex field.
88+
userInfo.UserID = u.loginUserID
8889
if err := u.updateUserInfo(ctx, userInfo); err != nil {
8990
return err
9091
}
@@ -178,29 +179,3 @@ func (u *User) GetUsersInfoFromSvr(ctx context.Context, userIDs []string) ([]*mo
178179
}
179180
return datautil.Batch(ServerUserToLocalUser, users), nil
180181
}
181-
182-
func (u *User) GetUserInfoWithCache(ctx context.Context, cacheKey string) (*model_struct.LocalUser, error) {
183-
return u.UserCache.FetchGet(ctx, cacheKey)
184-
}
185-
186-
func (u *User) GetUserInfoWithCacheFunc(ctx context.Context, cacheKey string, fetchFunc func(ctx context.Context, key string) (*model_struct.LocalUser, error)) (*model_struct.LocalUser, error) {
187-
if userInfo, ok := u.UserCache.Load(cacheKey); ok {
188-
return userInfo, nil
189-
}
190-
191-
fetchedData, err := fetchFunc(ctx, cacheKey)
192-
if err != nil {
193-
return nil, err
194-
}
195-
196-
u.UserCache.Store(cacheKey, fetchedData)
197-
return fetchedData, nil
198-
}
199-
200-
func (u *User) GetUsersInfoWithCache(ctx context.Context, cacheKeys []string) ([]*model_struct.LocalUser, error) {
201-
m, err := u.UserCache.MultiFetchGet(ctx, cacheKeys)
202-
if err != nil {
203-
return nil, err
204-
}
205-
return datautil.MapToSlice(m), nil
206-
}

internal/user/user.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/openimsdk/openim-sdk-core/v3/pkg/db/model_struct"
2727
"github.com/openimsdk/openim-sdk-core/v3/pkg/syncer"
2828
"github.com/openimsdk/openim-sdk-core/v3/pkg/utils"
29+
"github.com/openimsdk/tools/utils/datautil"
2930
)
3031

3132
// NewUser creates a new User object.
@@ -132,3 +133,29 @@ func (u *User) initSyncer() {
132133
},
133134
)
134135
}
136+
137+
func (u *User) GetUserInfoWithCache(ctx context.Context, cacheKey string) (*model_struct.LocalUser, error) {
138+
return u.UserCache.FetchGet(ctx, cacheKey)
139+
}
140+
141+
func (u *User) GetUserInfoWithCacheFunc(ctx context.Context, cacheKey string, fetchFunc func(ctx context.Context, key string) (*model_struct.LocalUser, error)) (*model_struct.LocalUser, error) {
142+
if userInfo, ok := u.UserCache.Load(cacheKey); ok {
143+
return userInfo, nil
144+
}
145+
146+
fetchedData, err := fetchFunc(ctx, cacheKey)
147+
if err != nil {
148+
return nil, err
149+
}
150+
151+
u.UserCache.Store(cacheKey, fetchedData)
152+
return fetchedData, nil
153+
}
154+
155+
func (u *User) GetUsersInfoWithCache(ctx context.Context, cacheKeys []string) ([]*model_struct.LocalUser, error) {
156+
m, err := u.UserCache.MultiFetchGet(ctx, cacheKeys)
157+
if err != nil {
158+
return nil, err
159+
}
160+
return datautil.MapToSlice(m), nil
161+
}

open_im_sdk/conversation_msg.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ func GetMultipleConversation(callback open_im_sdk_callback.Base, operationID str
3434
call(callback, operationID, UserForSDK.Conversation().GetMultipleConversation, conversationIDList)
3535
}
3636

37-
func SetConversations(callback open_im_sdk_callback.Base, operationID string, conversationID string, req string) {
38-
call(callback, operationID, UserForSDK.Conversation().SetConversations, conversationID, req)
37+
func SetConversation(callback open_im_sdk_callback.Base, operationID string, conversationID string, req string) {
38+
call(callback, operationID, UserForSDK.Conversation().SetConversation, conversationID, req)
3939
}
4040

4141
func HideConversation(callback open_im_sdk_callback.Base, operationID string, conversationID string) {

open_im_sdk/relation.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ package open_im_sdk
1616

1717
import "github.com/openimsdk/openim-sdk-core/v3/open_im_sdk_callback"
1818

19-
func GetSpecifiedFriendsInfo(callback open_im_sdk_callback.Base, operationID string, userIDList string) {
20-
call(callback, operationID, UserForSDK.Relation().GetSpecifiedFriendsInfo, userIDList)
19+
func GetSpecifiedFriendsInfo(callback open_im_sdk_callback.Base, operationID string, userIDList string, filterBlack bool) {
20+
call(callback, operationID, UserForSDK.Relation().GetSpecifiedFriendsInfo, userIDList, filterBlack)
2121
}
2222

2323
func GetFriendList(callback open_im_sdk_callback.Base, operationID string, filterBlack bool) {

0 commit comments

Comments
 (0)