parent
2f97933f6c
commit
1f02bdc267
@ -0,0 +1,26 @@
|
|||||||
|
package friend
|
||||||
|
|
||||||
|
import (
|
||||||
|
relationtb "github.com/openimsdk/open-im-server/v3/pkg/common/db/table/relation"
|
||||||
|
"github.com/openimsdk/protocol/friend"
|
||||||
|
"github.com/openimsdk/tools/utils/datautil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func friendDB2PB(db *relationtb.FriendModel) *friend.FriendInfo {
|
||||||
|
return &friend.FriendInfo{
|
||||||
|
OwnerUserID: db.OwnerUserID,
|
||||||
|
FriendUserID: db.FriendUserID,
|
||||||
|
FriendNickname: db.FriendNickname,
|
||||||
|
FriendFaceURL: db.FriendFaceURL,
|
||||||
|
Remark: db.Remark,
|
||||||
|
CreateTime: db.CreateTime.UnixMilli(),
|
||||||
|
AddSource: db.AddSource,
|
||||||
|
OperatorUserID: db.OperatorUserID,
|
||||||
|
Ex: db.Ex,
|
||||||
|
IsPinned: db.IsPinned,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func friendsDB2PB(db []*relationtb.FriendModel) []*friend.FriendInfo {
|
||||||
|
return datautil.Slice(db, friendDB2PB)
|
||||||
|
}
|
@ -0,0 +1,65 @@
|
|||||||
|
package friend
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"crypto/md5"
|
||||||
|
"encoding/binary"
|
||||||
|
"encoding/json"
|
||||||
|
"github.com/openimsdk/open-im-server/v3/pkg/common/db/dataver"
|
||||||
|
"github.com/openimsdk/open-im-server/v3/pkg/common/db/table/relation"
|
||||||
|
pbfriend "github.com/openimsdk/protocol/friend"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (s *friendServer) SearchFriends(ctx context.Context, req *pbfriend.SearchFriendsReq) (*pbfriend.SearchFriendsResp, error) {
|
||||||
|
//TODO implement me
|
||||||
|
panic("implement me")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *friendServer) sortFriendUserIDsHash(userIDs []string) uint64 {
|
||||||
|
data, _ := json.Marshal(userIDs)
|
||||||
|
sum := md5.Sum(data)
|
||||||
|
return binary.BigEndian.Uint64(sum[:])
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *friendServer) IncrSyncFriends(ctx context.Context, req *pbfriend.IncrSyncFriendsReq) (*pbfriend.IncrSyncFriendsResp, error) {
|
||||||
|
var limit int
|
||||||
|
if req.Version > 0 {
|
||||||
|
limit = s.config.RpcConfig.FriendSyncCount
|
||||||
|
}
|
||||||
|
incrVer, err := s.friendDatabase.FindFriendIncrVersion(ctx, req.UserID, uint(req.Version), limit)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
sortUserIDs, err := s.friendDatabase.FindSortFriendUserIDs(ctx, req.UserID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if len(sortUserIDs) == 0 {
|
||||||
|
return &pbfriend.IncrSyncFriendsResp{
|
||||||
|
Version: uint64(incrVer.Version),
|
||||||
|
Full: true,
|
||||||
|
SyncCount: uint32(s.config.RpcConfig.FriendSyncCount),
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
var changes []*relation.FriendModel
|
||||||
|
res := dataver.NewSyncResult(incrVer, sortUserIDs)
|
||||||
|
if len(res.Changes) > 0 {
|
||||||
|
changes, err = s.friendDatabase.FindFriendsWithError(ctx, req.UserID, res.Changes)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
calcHash := s.sortFriendUserIDsHash(sortUserIDs)
|
||||||
|
if calcHash == req.IdHash {
|
||||||
|
sortUserIDs = nil
|
||||||
|
}
|
||||||
|
return &pbfriend.IncrSyncFriendsResp{
|
||||||
|
Version: uint64(res.Version),
|
||||||
|
Full: res.Full,
|
||||||
|
SyncCount: uint32(s.config.RpcConfig.FriendSyncCount),
|
||||||
|
SortUserIdHash: calcHash,
|
||||||
|
SortUserIds: sortUserIDs,
|
||||||
|
DeleteUserIds: res.DeleteEID,
|
||||||
|
Changes: friendsDB2PB(changes),
|
||||||
|
}, nil
|
||||||
|
}
|
@ -1,32 +1,31 @@
|
|||||||
package dataver
|
package dataver
|
||||||
|
|
||||||
type SyncResult[T any] struct {
|
import "github.com/openimsdk/tools/utils/datautil"
|
||||||
|
|
||||||
|
type SyncResult struct {
|
||||||
Version uint
|
Version uint
|
||||||
DeleteEID []string
|
DeleteEID []string
|
||||||
Changes []T
|
Changes []string
|
||||||
Full bool
|
Full bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSyncResult[T any](wl *WriteLog, find func(eIds []string) ([]T, error)) (*SyncResult[T], error) {
|
func NewSyncResult(wl *WriteLog, fullIDs []string) *SyncResult {
|
||||||
var findEIDs []string
|
var findEIDs []string
|
||||||
var res SyncResult[T]
|
var res SyncResult
|
||||||
if wl.Full() {
|
if wl.Full() {
|
||||||
|
res.Changes = fullIDs
|
||||||
res.Full = true
|
res.Full = true
|
||||||
} else {
|
} else {
|
||||||
|
idSet := datautil.SliceSet(fullIDs)
|
||||||
for _, l := range wl.Logs {
|
for _, l := range wl.Logs {
|
||||||
if l.Deleted {
|
if l.Deleted {
|
||||||
res.DeleteEID = append(res.DeleteEID, l.EID)
|
res.DeleteEID = append(res.DeleteEID, l.EID)
|
||||||
} else {
|
} else {
|
||||||
|
if _, ok := idSet[l.EID]; ok {
|
||||||
findEIDs = append(findEIDs, l.EID)
|
findEIDs = append(findEIDs, l.EID)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if res.Full || len(findEIDs) > 0 {
|
|
||||||
var err error
|
|
||||||
res.Changes, err = find(findEIDs)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return &res, nil
|
return &res
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in new issue