friend incr sync

pull/2336/head
withchao 1 year ago
parent 8e37a417db
commit f6131c4ce5

@ -98,3 +98,20 @@ func (o *FriendApi) UpdateFriends(c *gin.Context) {
func (o *FriendApi) GetIncrementalFriends(c *gin.Context) { func (o *FriendApi) GetIncrementalFriends(c *gin.Context) {
a2r.Call(friend.FriendClient.GetIncrementalFriends, o.Client, c) 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/binary"
"encoding/json" "encoding/json"
"github.com/openimsdk/open-im-server/v3/pkg/authverify" "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" "github.com/openimsdk/open-im-server/v3/pkg/common/db/table/relation"
pbfriend "github.com/openimsdk/protocol/friend" pbfriend "github.com/openimsdk/protocol/friend"
) )
@ -34,38 +33,31 @@ func (s *friendServer) GetIncrementalFriends(ctx context.Context, req *pbfriend.
if err != nil { if err != nil {
return nil, err return nil, err
} }
sortUserIDs, err := s.friendDatabase.FindSortFriendUserIDs(ctx, req.UserID) var (
if err != nil { deleteUserIDs []string
return nil, err changeUserIDs []string
} )
if len(sortUserIDs) == 0 { if incrVer.Full() {
return &pbfriend.GetIncrementalFriendsResp{ changeUserIDs, err = s.friendDatabase.FindSortFriendUserIDs(ctx, req.UserID)
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)
if err != nil { if err != nil {
return nil, err return nil, err
} }
} else {
deleteUserIDs, changeUserIDs = incrVer.DeleteAndChangeIDs()
} }
calcHash := s.sortFriendUserIDsHash(sortUserIDs) var friends []*relation.FriendModel
if calcHash == req.IdHash { if len(changeUserIDs) > 0 {
sortUserIDs = nil friends, err = s.friendDatabase.FindFriendsWithError(ctx, req.UserID, changeUserIDs)
if err != nil {
return nil, err
}
} }
return &pbfriend.GetIncrementalFriendsResp{ return &pbfriend.GetIncrementalFriendsResp{
Version: uint64(res.Version), Version: uint64(incrVer.Version),
VersionID: res.VersionID, VersionID: incrVer.ID.String(),
Full: res.Full, Full: incrVer.Full(),
SyncCount: uint32(s.config.RpcConfig.FriendSyncCount), SyncCount: uint32(s.config.RpcConfig.FriendSyncCount),
SortUserIdHash: calcHash, DeleteUserIds: deleteUserIDs,
SortUserIds: sortUserIDs, Changes: friendsDB2PB(friends),
DeleteUserIds: res.DeleteEID,
Changes: friendsDB2PB(changes),
}, nil }, nil
} }

@ -35,14 +35,15 @@ func (w *WriteLog) Full() bool {
return len(w.Logs) != w.LogLen return len(w.Logs) != w.LogLen
} }
func (w *WriteLog) DeleteEId() []string { func (w *WriteLog) DeleteAndChangeIDs() (delIds []string, changeIds []string) {
var eIds []string
for _, l := range w.Logs { for _, l := range w.Logs {
if l.Deleted { 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 { type Elem struct {

@ -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…
Cancel
Save