mirror of
https://github.com/openimsdk/open-im-server.git
synced 2025-11-04 19:32:17 +08:00
friend incr sync
This commit is contained in:
parent
8e37a417db
commit
f6131c4ce5
@ -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"`
|
||||
//}
|
||||
|
||||
@ -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
|
||||
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()
|
||||
}
|
||||
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 friends []*relation.FriendModel
|
||||
if len(changeUserIDs) > 0 {
|
||||
friends, err = s.friendDatabase.FindFriendsWithError(ctx, req.UserID, changeUserIDs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
calcHash := s.sortFriendUserIDsHash(sortUserIDs)
|
||||
if calcHash == req.IdHash {
|
||||
sortUserIDs = nil
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
@ -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 {
|
||||
|
||||
34
pkg/common/db/mgo/mongo_test.go
Normal file
34
pkg/common/db/mgo/mongo_test.go
Normal file
@ -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)
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user