Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: rewrite SDK data fetch logic. #4

Merged
merged 1 commit into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion internal/friend/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,18 @@ package friend

import (
"context"

"github.com/openimsdk/openim-sdk-core/v3/internal/util"
"github.com/openimsdk/openim-sdk-core/v3/pkg/constant"
"github.com/openimsdk/openim-sdk-core/v3/pkg/datafetcher"
"github.com/openimsdk/openim-sdk-core/v3/pkg/db/model_struct"
sdk "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"
friend "github.com/openimsdk/protocol/relation"
"github.com/openimsdk/protocol/wrapperspb"
"github.com/openimsdk/tools/errs"
"github.com/openimsdk/tools/utils/datautil"

"github.com/openimsdk/tools/log"
)
Expand Down Expand Up @@ -166,10 +169,34 @@ func (f *Friend) GetFriendList(ctx context.Context) ([]*server_api_params.FullUs
}

func (f *Friend) GetFriendListPage(ctx context.Context, offset, count int32) ([]*server_api_params.FullUserInfo, error) {
localFriendList, err := f.db.GetPageFriendList(ctx, int(offset), int(count))
datafetcher := datafetcher.NewDataFetcher(
f.db,
f.friendListTableName(),
f.loginUserID,
func(localFriend *model_struct.LocalFriend) string {
return localFriend.FriendUserID
},
func(ctx context.Context, values []*model_struct.LocalFriend) error {
return f.db.BatchInsertFriend(ctx, values)
},
func(ctx context.Context, userIDs []string) ([]*model_struct.LocalFriend, error) {
return f.db.GetFriendInfoList(ctx, userIDs)
},
func(ctx context.Context, userIDs []string) ([]*model_struct.LocalFriend, error) {
serverFriend, err := f.GetDesignatedFriends(ctx, userIDs)
if err != nil {
return nil, err
}
return datautil.Batch(ServerFriendToLocalFriend, serverFriend), nil
},
)

localFriendList, err := datafetcher.FetchWithPagination(ctx, int(offset), int(count))
if err != nil {
return nil, err
}

// don't need extra handle. only full pull.
localBlackList, err := f.db.GetBlackListDB(ctx)
if err != nil {
return nil, err
Expand Down
9 changes: 9 additions & 0 deletions internal/friend/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package friend

import (
"context"

"github.com/openimsdk/openim-sdk-core/v3/internal/util"
"github.com/openimsdk/openim-sdk-core/v3/pkg/constant"
friend "github.com/openimsdk/protocol/relation"
Expand Down Expand Up @@ -159,3 +160,11 @@ func (f *Friend) SyncAllBlackList(ctx context.Context) error {
log.ZDebug(ctx, "black from local", "data", localData)
return f.blockSyncer.Sync(ctx, util.Batch(ServerBlackToLocalBlack, serverData), localData, nil)
}

func (f *Friend) GetDesignatedFriends(ctx context.Context, friendIDs []string) ([]*sdkws.FriendInfo, error) {
resp := &friend.GetDesignatedFriendsResp{}
if err := util.ApiPost(ctx, constant.GetDesignatedFriendsRouter, &friend.GetDesignatedFriendsReq{OwnerUserID: f.loginUserID, FriendUserIDs: friendIDs}, &resp); err != nil {
return nil, err
}
return resp.FriendsInfo, nil
}
1 change: 1 addition & 0 deletions internal/group/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package group

import (
"context"

"github.com/openimsdk/openim-sdk-core/v3/internal/util"
"github.com/openimsdk/openim-sdk-core/v3/open_im_sdk_callback"
"github.com/openimsdk/openim-sdk-core/v3/pkg/common"
Expand Down
33 changes: 27 additions & 6 deletions internal/group/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ package group

import (
"context"
"time"

"github.com/openimsdk/openim-sdk-core/v3/pkg/datafetcher"
"github.com/openimsdk/tools/utils/datautil"
"time"

"github.com/openimsdk/openim-sdk-core/v3/internal/util"
"github.com/openimsdk/openim-sdk-core/v3/pkg/constant"
Expand Down Expand Up @@ -176,8 +177,29 @@ func (g *Group) SetGroupMemberInfo(ctx context.Context, groupMemberInfo *group.S
return g.SyncGroupMembers(ctx, groupMemberInfo.GroupID, groupMemberInfo.UserID)
}

func (g *Group) GetJoinedGroupList(ctx context.Context) ([]*model_struct.LocalGroup, error) {
return g.db.GetJoinedGroupListDB(ctx)
func (g *Group) GetJoinedGroupList(ctx context.Context, offset, count int32) ([]*model_struct.LocalGroup, error) {
dataFetcher := datafetcher.NewDataFetcher(
g.db,
g.groupTableName(),
g.loginUserID,
func(localGroup *model_struct.LocalGroup) string {
return localGroup.GroupID
},
func(ctx context.Context, values []*model_struct.LocalGroup) error {
return g.db.BatchInsertGroup(ctx, values)
},
func(ctx context.Context, groupIDs []string) ([]*model_struct.LocalGroup, error) {
return g.db.GetGroups(ctx, groupIDs)
},
func(ctx context.Context, groupIDs []string) ([]*model_struct.LocalGroup, error) {
serverGroupInfo, err := g.getGroupsInfoFromSvr(ctx, groupIDs)
if err != nil {
return nil, err
}
return datautil.Batch(ServerGroupToLocalGroup, serverGroupInfo), nil
},
)
return dataFetcher.FetchWithPagination(ctx, int(offset), int(count))
}

func (g *Group) GetSpecifiedGroupsInfo(ctx context.Context, groupIDs []string) ([]*model_struct.LocalGroup, error) {
Expand Down Expand Up @@ -261,9 +283,8 @@ func (g *Group) GetGroupMemberList(ctx context.Context, groupID string, filter,
func(ctx context.Context, values []*model_struct.LocalGroupMember) error {
return g.db.BatchInsertGroupMember(ctx, values)
},
func(ctx context.Context, uids []string) ([]*model_struct.LocalGroupMember, error) {
//todo GetGroupMemberListSplit change to
return g.db.GetGroupMemberListSplit(ctx, groupID, filter, int(offset), int(count))
func(ctx context.Context, userIDs []string) ([]*model_struct.LocalGroupMember, error) {
return g.db.GetGroupMemberListByUserIDs(ctx, groupID, filter, userIDs)
},
func(ctx context.Context, userIDs []string) ([]*model_struct.LocalGroupMember, error) {
serverGroupMember, err := g.GetDesignatedGroupMembers(ctx, groupID, userIDs)
Expand Down
2 changes: 2 additions & 0 deletions pkg/db/db_interface/databse.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,8 @@ type FriendModel interface {
UpdateFriend(ctx context.Context, friend *model_struct.LocalFriend) error
GetAllFriendList(ctx context.Context) ([]*model_struct.LocalFriend, error)
GetPageFriendList(ctx context.Context, offset, count int) ([]*model_struct.LocalFriend, error)
BatchInsertFriend(ctx context.Context, friendList []*model_struct.LocalFriend) error

SearchFriendList(ctx context.Context, keyword string, isSearchUserID, isSearchNickname, isSearchRemark bool) ([]*model_struct.LocalFriend, error)
GetFriendInfoByFriendUserID(ctx context.Context, FriendUserID string) (*model_struct.LocalFriend, error)
GetFriendInfoList(ctx context.Context, friendUserIDList []string) ([]*model_struct.LocalFriend, error)
Expand Down
11 changes: 11 additions & 0 deletions pkg/db/friend_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ import (
"context"
"errors"
"fmt"

"github.com/openimsdk/openim-sdk-core/v3/pkg/db/model_struct"
"github.com/openimsdk/openim-sdk-core/v3/pkg/utils"
"github.com/openimsdk/tools/errs"
)

func (d *DataBase) InsertFriend(ctx context.Context, friend *model_struct.LocalFriend) error {
Expand Down Expand Up @@ -79,6 +81,15 @@ func (d *DataBase) GetPageFriendList(ctx context.Context, offset, count int) ([]
return friendList, err
}

func (d *DataBase) BatchInsertFriend(ctx context.Context, friendList []*model_struct.LocalFriend) error {
d.friendMtx.Lock()
defer d.friendMtx.Unlock()
if friendList == nil {
return errs.New("nil").Wrap()
}
return errs.WrapMsg(d.conn.WithContext(ctx).Create(friendList).Error, "BatchInsertFriendList failed")
}

func (d *DataBase) SearchFriendList(ctx context.Context, keyword string, isSearchUserID, isSearchNickname, isSearchRemark bool) ([]*model_struct.LocalFriend, error) {
d.friendMtx.Lock()
defer d.friendMtx.Unlock()
Expand Down
20 changes: 8 additions & 12 deletions pkg/db/group_member_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,30 +92,26 @@ func (d *DataBase) GetGroupMemberListByGroupID(ctx context.Context, groupID stri
func (d *DataBase) GetGroupMemberListByUserIDs(ctx context.Context, groupID string, filter int32, userIDs []string) ([]*model_struct.LocalGroupMember, error) {
d.groupMtx.Lock()
defer d.groupMtx.Unlock()
var groupMemberList []model_struct.LocalGroupMember
var groupMemberList []*model_struct.LocalGroupMember
var err error
switch filter {
case constant.GroupFilterAll:
err = d.conn.WithContext(ctx).Where("group_id = ? AND user_id IN ?", groupID, userIDs).Order("role_level DESC, join_time ASC").Find(&groupMemberList).Error
case constant.GroupFilterOwner:
err = d.conn.WithContext(ctx).Where("group_id = ? AND role_level = ? AND user_id IN ?", groupID, constant.GroupOwner, userIDs).Find(&groupMemberList).Error
case constant.GroupFilterAdmin:
err = d.conn.WithContext(ctx).Where("group_id = ? AND role_level = ? AND user_id IN ?", groupID, constant.GroupAdmin, userIDs).Order("join_time ASC").Find(&groupMemberList).Error
err = d.conn.WithContext(ctx).Where("group_id = ? AND role_level = ? AND user_id IN ?", groupID, constant.GroupAdmin, userIDs).Find(&groupMemberList).Error
case constant.GroupFilterOrdinaryUsers:
err = d.conn.WithContext(ctx).Where("group_id = ? AND role_level = ? AND user_id IN ?", groupID, constant.GroupOrdinaryUsers, userIDs).Order("join_time ASC").Find(&groupMemberList).Error
err = d.conn.WithContext(ctx).Where("group_id = ? AND role_level = ? AND user_id IN ?", groupID, constant.GroupOrdinaryUsers, userIDs).Find(&groupMemberList).Error
case constant.GroupFilterAdminAndOrdinaryUsers:
err = d.conn.WithContext(ctx).Where("group_id = ? AND (role_level = ? OR role_level = ?) AND user_id IN ?", groupID, constant.GroupAdmin, constant.GroupOrdinaryUsers, userIDs).Order("role_level DESC, join_time ASC").Find(&groupMemberList).Error
err = d.conn.WithContext(ctx).Where("group_id = ? AND (role_level = ? OR role_level = ?) AND user_id IN ?", groupID, constant.GroupAdmin, constant.GroupOrdinaryUsers, userIDs).Find(&groupMemberList).Error
case constant.GroupFilterOwnerAndAdmin:
err = d.conn.WithContext(ctx).Where("group_id = ? AND (role_level = ? OR role_level = ?) AND user_id IN ?", groupID, constant.GroupOwner, constant.GroupAdmin, userIDs).Order("role_level DESC, join_time ASC").Find(&groupMemberList).Error
err = d.conn.WithContext(ctx).Where("group_id = ? AND (role_level = ? OR role_level = ?) AND user_id IN ?", groupID, constant.GroupOwner, constant.GroupAdmin, userIDs).Find(&groupMemberList).Error
default:
return nil, errs.New("filter args failed.", "filter", filter).Wrap()
}
var transfer []*model_struct.LocalGroupMember
for _, member := range groupMemberList {
memberCopy := member
transfer = append(transfer, &memberCopy)
}
return transfer, errs.Wrap(err)

return groupMemberList, errs.Wrap(err)

}

Expand Down Expand Up @@ -230,7 +226,7 @@ func (d *DataBase) BatchInsertGroupMember(ctx context.Context, groupMemberList [
if groupMemberList == nil {
return errors.New("nil")
}
return utils.Wrap(d.conn.WithContext(ctx).Create(groupMemberList).Error, "BatchInsertMessageList failed")
return errs.WrapMsg(d.conn.WithContext(ctx).Create(groupMemberList).Error, "BatchInsertMessageList failed")
}

func (d *DataBase) DeleteGroupMember(ctx context.Context, groupID, userID string) error {
Expand Down