diff --git a/internal/api/friend.go b/internal/api/friend.go index 3af162a53..5b2dfa0ca 100644 --- a/internal/api/friend.go +++ b/internal/api/friend.go @@ -98,3 +98,20 @@ func (o *FriendApi) UpdateFriends(c *gin.Context) { func (o *FriendApi) GetIncrementalFriends(c *gin.Context) { a2r.Call(friend.FriendClient.GetIncrementalFriends, o.Client, c) } + +//func BatchIncremental[A, B, C any,D comparable](c *gin.Context, rpc func(client C, ctx context.Context, req *A, options ...grpc.CallOption) (*B, error), getID func(req *A)D, setID func(req *A, id D)) { +// req, err := a2r.ParseRequestNotCheck[BatchIncrementalReq[A]](c) +// if err != nil { +// apiresp.GinError(c, err) +// return +// } +// if len(req.List) == 0 { +// apiresp.GinError(c, errs.ErrArgs.WrapMsg("empty versions list")) +// return +// } +//} +// +//type BatchIncrementalReq[A any] struct { +// UserID string `json:"user_id"` +// List []*A `json:"list"` +//} diff --git a/internal/rpc/friend/sync.go b/internal/rpc/friend/sync.go index 9d2a53b58..a2f49b283 100644 --- a/internal/rpc/friend/sync.go +++ b/internal/rpc/friend/sync.go @@ -6,7 +6,6 @@ import ( "encoding/binary" "encoding/json" "github.com/openimsdk/open-im-server/v3/pkg/authverify" - "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" ) @@ -34,38 +33,31 @@ func (s *friendServer) GetIncrementalFriends(ctx context.Context, req *pbfriend. 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.GetIncrementalFriendsResp{ - Version: uint64(incrVer.Version), - VersionID: dataver.VersionIDStr(incrVer.ID), - Full: true, - SyncCount: uint32(s.config.RpcConfig.FriendSyncCount), - }, nil - } - var changes []*relation.FriendModel - res := dataver.NewSyncResult(incrVer, sortUserIDs, req.VersionID) - if len(res.Changes) > 0 { - changes, err = s.friendDatabase.FindFriendsWithError(ctx, req.UserID, res.Changes) + var ( + deleteUserIDs []string + changeUserIDs []string + ) + if incrVer.Full() { + changeUserIDs, err = s.friendDatabase.FindSortFriendUserIDs(ctx, req.UserID) if err != nil { return nil, err } + } else { + deleteUserIDs, changeUserIDs = incrVer.DeleteAndChangeIDs() } - calcHash := s.sortFriendUserIDsHash(sortUserIDs) - if calcHash == req.IdHash { - sortUserIDs = nil + var friends []*relation.FriendModel + if len(changeUserIDs) > 0 { + friends, err = s.friendDatabase.FindFriendsWithError(ctx, req.UserID, changeUserIDs) + if err != nil { + return nil, err + } } return &pbfriend.GetIncrementalFriendsResp{ - Version: uint64(res.Version), - VersionID: res.VersionID, - Full: res.Full, - SyncCount: uint32(s.config.RpcConfig.FriendSyncCount), - SortUserIdHash: calcHash, - SortUserIds: sortUserIDs, - DeleteUserIds: res.DeleteEID, - Changes: friendsDB2PB(changes), + Version: uint64(incrVer.Version), + VersionID: incrVer.ID.String(), + Full: incrVer.Full(), + SyncCount: uint32(s.config.RpcConfig.FriendSyncCount), + DeleteUserIds: deleteUserIDs, + Changes: friendsDB2PB(friends), }, nil } diff --git a/pkg/common/db/dataver/common.go b/pkg/common/db/dataver/common.go index 3ff9906e5..e29ab2b96 100644 --- a/pkg/common/db/dataver/common.go +++ b/pkg/common/db/dataver/common.go @@ -35,14 +35,15 @@ func (w *WriteLog) Full() bool { return len(w.Logs) != w.LogLen } -func (w *WriteLog) DeleteEId() []string { - var eIds []string +func (w *WriteLog) DeleteAndChangeIDs() (delIds []string, changeIds []string) { for _, l := range w.Logs { if l.Deleted { - eIds = append(eIds, l.EID) + delIds = append(delIds, l.EID) + } else { + changeIds = append(changeIds, l.EID) } } - return eIds + return } type Elem struct { diff --git a/pkg/common/db/mgo/mongo_test.go b/pkg/common/db/mgo/mongo_test.go new file mode 100644 index 000000000..294dd1317 --- /dev/null +++ b/pkg/common/db/mgo/mongo_test.go @@ -0,0 +1,34 @@ +package mgo + +import ( + "context" + "go.mongodb.org/mongo-driver/mongo" + "go.mongodb.org/mongo-driver/mongo/options" + "testing" + "time" +) + +func Result[V any](val V, err error) V { + if err != nil { + panic(err) + } + return val +} + +func Check(err error) { + if err != nil { + panic(err) + } +} + +func TestName(t *testing.T) { + cli := Result(mongo.Connect(context.Background(), options.Client().ApplyURI("mongodb://openIM:openIM123@172.16.8.48:37017/openim_v3?maxPoolSize=100").SetConnectTimeout(5*time.Second))) + conv := Result(NewConversationMongo(cli.Database("openim_v3"))) + num, err := conv.GetAllConversationIDsNumber(context.Background()) + if err != nil { + panic(err) + } + t.Log(num) + ids := Result(conv.GetAllConversationIDs(context.Background())) + t.Log(ids) +}