refactor: add openim mysql to mongo refactor

Signed-off-by: Xinwei Xiong(cubxxw) <3293172751nss@gmail.com>
pull/1427/head
Xinwei Xiong(cubxxw) 2 years ago
parent d054de9c6d
commit ca30a76952

@ -37,8 +37,9 @@ import (
"github.com/openimsdk/open-im-server/v3/pkg/common/db/cache" "github.com/openimsdk/open-im-server/v3/pkg/common/db/cache"
"github.com/openimsdk/open-im-server/v3/pkg/common/db/controller" "github.com/openimsdk/open-im-server/v3/pkg/common/db/controller"
"github.com/openimsdk/open-im-server/v3/pkg/common/db/relation" "github.com/openimsdk/open-im-server/v3/pkg/common/db/newmgo"
tablerelation "github.com/openimsdk/open-im-server/v3/pkg/common/db/table/relation" tablerelation "github.com/openimsdk/open-im-server/v3/pkg/common/db/table/relation"
"github.com/openimsdk/open-im-server/v3/pkg/common/db/unrelation"
"github.com/openimsdk/open-im-server/v3/pkg/rpcclient/notification" "github.com/openimsdk/open-im-server/v3/pkg/rpcclient/notification"
) )
@ -52,41 +53,61 @@ type friendServer struct {
} }
func Start(client registry.SvcDiscoveryRegistry, server *grpc.Server) error { func Start(client registry.SvcDiscoveryRegistry, server *grpc.Server) error {
db, err := relation.NewGormDB() // Initialize MongoDB
mongo, err := unrelation.NewMongo()
if err != nil { if err != nil {
return err return err
} }
if err := db.AutoMigrate(&tablerelation.FriendModel{}, &tablerelation.FriendRequestModel{}, &tablerelation.BlackModel{}); err != nil {
// Initialize Redis
rdb, err := cache.NewRedis()
if err != nil {
return err return err
} }
rdb, err := cache.NewRedis()
friendMongoDB, err := newmgo.NewFriendMongo(mongo.GetDatabase())
if err != nil {
return err
}
friendRequestMongoDB, err := newmgo.NewFriendRequestMongo(mongo.GetDatabase())
if err != nil { if err != nil {
return err return err
} }
blackDB := relation.NewBlackGorm(db)
friendDB := relation.NewFriendGorm(db) blackMongoDB, err := newmgo.NewBlackMongo(mongo.GetDatabase())
if err != nil {
return err
}
// Initialize RPC clients
userRpcClient := rpcclient.NewUserRpcClient(client) userRpcClient := rpcclient.NewUserRpcClient(client)
msgRpcClient := rpcclient.NewMessageRpcClient(client) msgRpcClient := rpcclient.NewMessageRpcClient(client)
// Initialize notification sender
notificationSender := notification.NewFriendNotificationSender( notificationSender := notification.NewFriendNotificationSender(
&msgRpcClient, &msgRpcClient,
notification.WithRpcFunc(userRpcClient.GetUsersInfo), notification.WithRpcFunc(userRpcClient.GetUsersInfo),
) )
// Register Friend server with refactored MongoDB and Redis integrations
pbfriend.RegisterFriendServer(server, &friendServer{ pbfriend.RegisterFriendServer(server, &friendServer{
friendDatabase: controller.NewFriendDatabase( friendDatabase: controller.NewFriendDatabase(
friendDB, friendMongoDB,
relation.NewFriendRequestGorm(db), friendRequestMongoDB,
cache.NewFriendCacheRedis(rdb, friendDB, cache.GetDefaultOpt()), cache.NewFriendCacheRedis(rdb, friendMongoDB, cache.GetDefaultOpt()),
tx.NewGorm(db), tx.NewMongo(mongo.GetClient()),
), ),
blackDatabase: controller.NewBlackDatabase( blackDatabase: controller.NewBlackDatabase(
blackDB, blackMongoDB,
cache.NewBlackCacheRedis(rdb, blackDB, cache.GetDefaultOpt()), cache.NewBlackCacheRedis(rdb, blackMongoDB, cache.GetDefaultOpt()),
), ),
userRpcClient: &userRpcClient, userRpcClient: &userRpcClient,
notificationSender: notificationSender, notificationSender: notificationSender,
RegisterCenter: client, RegisterCenter: client,
conversationRpcClient: rpcclient.NewConversationRpcClient(client), conversationRpcClient: rpcclient.NewConversationRpcClient(client),
}) })
return nil return nil
} }

@ -16,6 +16,7 @@ package convert
import ( import (
"context" "context"
"fmt"
"github.com/OpenIMSDK/protocol/sdkws" "github.com/OpenIMSDK/protocol/sdkws"
"github.com/OpenIMSDK/tools/utils" "github.com/OpenIMSDK/tools/utils"
@ -31,23 +32,22 @@ func FriendPb2DB(friend *sdkws.FriendInfo) *relation.FriendModel {
return dbFriend return dbFriend
} }
func FriendDB2Pb( func FriendDB2Pb(ctx context.Context, friendDB *relation.FriendModel,
ctx context.Context,
friendDB *relation.FriendModel,
getUsers func(ctx context.Context, userIDs []string) (map[string]*sdkws.UserInfo, error), getUsers func(ctx context.Context, userIDs []string) (map[string]*sdkws.UserInfo, error),
) (*sdkws.FriendInfo, error) { ) (*sdkws.FriendInfo, error) {
pbfriend := &sdkws.FriendInfo{FriendUser: &sdkws.UserInfo{}}
utils.CopyStructFields(pbfriend, friendDB)
users, err := getUsers(ctx, []string{friendDB.FriendUserID}) users, err := getUsers(ctx, []string{friendDB.FriendUserID})
if err != nil { if err != nil {
return nil, err return nil, err
} }
pbfriend.FriendUser.UserID = users[friendDB.FriendUserID].UserID user, ok := users[friendDB.FriendUserID]
pbfriend.FriendUser.Nickname = users[friendDB.FriendUserID].Nickname if !ok {
pbfriend.FriendUser.FaceURL = users[friendDB.FriendUserID].FaceURL return nil, fmt.Errorf("user not found: %s", friendDB.FriendUserID)
pbfriend.FriendUser.Ex = users[friendDB.FriendUserID].Ex }
pbfriend.CreateTime = friendDB.CreateTime.Unix()
return pbfriend, nil return &sdkws.FriendInfo{
FriendUser: user,
CreateTime: friendDB.CreateTime.Unix(),
}, nil
} }
func FriendsDB2Pb( func FriendsDB2Pb(
@ -118,3 +118,37 @@ func FriendRequestDB2Pb(
} }
return res, nil return res, nil
} }
// FriendPb2DBMap converts a FriendInfo protobuf object to a map suitable for database operations.
// It only includes non-zero or non-empty fields in the map.
func FriendPb2DBMap(friend *sdkws.FriendInfo) map[string]any {
if friend == nil {
return nil
}
val := make(map[string]any)
// Assuming FriendInfo has similar fields to those in FriendModel.
// Add or remove fields based on your actual FriendInfo and FriendModel structures.
if friend.FriendUser != nil {
if friend.FriendUser.UserID != "" {
val["friend_user_id"] = friend.FriendUser.UserID
}
if friend.FriendUser.Nickname != "" {
val["nickname"] = friend.FriendUser.Nickname
}
if friend.FriendUser.FaceURL != "" {
val["face_url"] = friend.FriendUser.FaceURL
}
if friend.FriendUser.Ex != "" {
val["ex"] = friend.FriendUser.Ex
}
}
if friend.CreateTime != 0 {
val["create_time"] = friend.CreateTime // You might need to convert this to a proper time format.
}
// Include other fields from FriendInfo as needed, similar to the above pattern.
return val
}

@ -22,31 +22,33 @@ import (
relationtb "github.com/openimsdk/open-im-server/v3/pkg/common/db/table/relation" relationtb "github.com/openimsdk/open-im-server/v3/pkg/common/db/table/relation"
) )
func UsersDB2Pb(users []*relationtb.UserModel) (result []*sdkws.UserInfo) { func UsersDB2Pb(users []*relationtb.UserModel) []*sdkws.UserInfo {
result := make([]*sdkws.UserInfo, 0, len(users))
for _, user := range users { for _, user := range users {
var userPb sdkws.UserInfo userPb := &sdkws.UserInfo{
userPb.UserID = user.UserID UserID: user.UserID,
userPb.Nickname = user.Nickname Nickname: user.Nickname,
userPb.FaceURL = user.FaceURL FaceURL: user.FaceURL,
userPb.Ex = user.Ex Ex: user.Ex,
userPb.CreateTime = user.CreateTime.UnixMilli() CreateTime: user.CreateTime.UnixMilli(),
userPb.AppMangerLevel = user.AppMangerLevel AppMangerLevel: user.AppMangerLevel,
userPb.GlobalRecvMsgOpt = user.GlobalRecvMsgOpt GlobalRecvMsgOpt: user.GlobalRecvMsgOpt,
result = append(result, &userPb) }
result = append(result, userPb)
} }
return result return result
} }
func UserPb2DB(user *sdkws.UserInfo) *relationtb.UserModel { func UserPb2DB(user *sdkws.UserInfo) *relationtb.UserModel {
var userDB relationtb.UserModel return &relationtb.UserModel{
userDB.UserID = user.UserID UserID: user.UserID,
userDB.Nickname = user.Nickname Nickname: user.Nickname,
userDB.FaceURL = user.FaceURL FaceURL: user.FaceURL,
userDB.Ex = user.Ex Ex: user.Ex,
userDB.CreateTime = time.UnixMilli(user.CreateTime) CreateTime: time.UnixMilli(user.CreateTime),
userDB.AppMangerLevel = user.AppMangerLevel AppMangerLevel: user.AppMangerLevel,
userDB.GlobalRecvMsgOpt = user.GlobalRecvMsgOpt GlobalRecvMsgOpt: user.GlobalRecvMsgOpt,
return &userDB }
} }
func UserPb2DBMap(user *sdkws.UserInfo) map[string]any { func UserPb2DBMap(user *sdkws.UserInfo) map[string]any {
@ -54,20 +56,19 @@ func UserPb2DBMap(user *sdkws.UserInfo) map[string]any {
return nil return nil
} }
val := make(map[string]any) val := make(map[string]any)
if user.Nickname != "" { fields := map[string]any{
val["nickname"] = user.Nickname "nickname": user.Nickname,
} "face_url": user.FaceURL,
if user.FaceURL != "" { "ex": user.Ex,
val["face_url"] = user.FaceURL "app_manager_level": user.AppMangerLevel,
} "global_recv_msg_opt": user.GlobalRecvMsgOpt,
if user.Ex != "" {
val["ex"] = user.FaceURL
}
if user.AppMangerLevel != 0 {
val["app_manger_level"] = user.AppMangerLevel
} }
if user.GlobalRecvMsgOpt != 0 { for key, value := range fields {
val["global_recv_msg_opt"] = user.GlobalRecvMsgOpt if v, ok := value.(string); ok && v != "" {
val[key] = v
} else if v, ok := value.(int32); ok && v != 0 {
val[key] = v
}
} }
return val return val
} }

@ -0,0 +1,86 @@
// Copyright © 2023 OpenIM. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package convert
import (
"reflect"
"testing"
"github.com/OpenIMSDK/protocol/sdkws"
relationtb "github.com/openimsdk/open-im-server/v3/pkg/common/db/table/relation"
)
func TestUsersDB2Pb(t *testing.T) {
type args struct {
users []*relationtb.UserModel
}
tests := []struct {
name string
args args
wantResult []*sdkws.UserInfo
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if gotResult := UsersDB2Pb(tt.args.users); !reflect.DeepEqual(gotResult, tt.wantResult) {
t.Errorf("UsersDB2Pb() = %v, want %v", gotResult, tt.wantResult)
}
})
}
}
func TestUserPb2DB(t *testing.T) {
type args struct {
user *sdkws.UserInfo
}
tests := []struct {
name string
args args
want *relationtb.UserModel
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := UserPb2DB(tt.args.user); !reflect.DeepEqual(got, tt.want) {
t.Errorf("UserPb2DB() = %v, want %v", got, tt.want)
}
})
}
}
func TestUserPb2DBMap(t *testing.T) {
user := &sdkws.UserInfo{
Nickname: "TestUser",
FaceURL: "http://openim.io/logo.jpg",
Ex: "Extra Data",
AppMangerLevel: 1,
GlobalRecvMsgOpt: 2,
}
expected := map[string]any{
"nickname": "TestUser",
"face_url": "http://openim.io/logo.jpg",
"ex": "Extra Data",
"app_manager_level": int32(1),
"global_recv_msg_opt": int32(2),
}
result := UserPb2DBMap(user)
if !reflect.DeepEqual(result, expected) {
t.Errorf("UserPb2DBMap returned unexpected map. Got %v, want %v", result, expected)
}
}

@ -33,19 +33,20 @@ const (
friendKey = "FRIEND_INFO:" friendKey = "FRIEND_INFO:"
) )
// args fn will exec when no data in msgCache. // FriendCache is an interface for caching friend-related data.
type FriendCache interface { type FriendCache interface {
metaCache metaCache
NewCache() FriendCache NewCache() FriendCache
GetFriendIDs(ctx context.Context, ownerUserID string) (friendIDs []string, err error) GetFriendIDs(ctx context.Context, ownerUserID string) (friendIDs []string, err error)
// call when friendID List changed // Called when friendID list changed
DelFriendIDs(ownerUserID ...string) FriendCache DelFriendIDs(ownerUserID ...string) FriendCache
// get single friendInfo from msgCache // Get single friendInfo from the cache
GetFriend(ctx context.Context, ownerUserID, friendUserID string) (friend *relationtb.FriendModel, err error) GetFriend(ctx context.Context, ownerUserID, friendUserID string) (friend *relationtb.FriendModel, err error)
// del friend when friend info changed // Delete friend when friend info changed
DelFriend(ownerUserID, friendUserID string) FriendCache DelFriend(ownerUserID, friendUserID string) FriendCache
} }
// FriendCacheRedis is an implementation of the FriendCache interface using Redis.
type FriendCacheRedis struct { type FriendCacheRedis struct {
metaCache metaCache
friendDB relationtb.FriendModelInterface friendDB relationtb.FriendModelInterface
@ -53,6 +54,7 @@ type FriendCacheRedis struct {
rcClient *rockscache.Client rcClient *rockscache.Client
} }
// NewFriendCacheRedis creates a new instance of FriendCacheRedis.
func NewFriendCacheRedis(rdb redis.UniversalClient, friendDB relationtb.FriendModelInterface, func NewFriendCacheRedis(rdb redis.UniversalClient, friendDB relationtb.FriendModelInterface,
options rockscache.Options) FriendCache { options rockscache.Options) FriendCache {
rcClient := rockscache.NewClient(rdb, options) rcClient := rockscache.NewClient(rdb, options)
@ -64,6 +66,7 @@ func NewFriendCacheRedis(rdb redis.UniversalClient, friendDB relationtb.FriendMo
} }
} }
// NewCache creates a new instance of FriendCacheRedis with the same configuration.
func (f *FriendCacheRedis) NewCache() FriendCache { func (f *FriendCacheRedis) NewCache() FriendCache {
return &FriendCacheRedis{ return &FriendCacheRedis{
rcClient: f.rcClient, rcClient: f.rcClient,
@ -73,24 +76,29 @@ func (f *FriendCacheRedis) NewCache() FriendCache {
} }
} }
// getFriendIDsKey returns the key for storing friend IDs in the cache.
func (f *FriendCacheRedis) getFriendIDsKey(ownerUserID string) string { func (f *FriendCacheRedis) getFriendIDsKey(ownerUserID string) string {
return friendIDsKey + ownerUserID return friendIDsKey + ownerUserID
} }
// getTwoWayFriendsIDsKey returns the key for storing two-way friend IDs in the cache.
func (f *FriendCacheRedis) getTwoWayFriendsIDsKey(ownerUserID string) string { func (f *FriendCacheRedis) getTwoWayFriendsIDsKey(ownerUserID string) string {
return TwoWayFriendsIDsKey + ownerUserID return TwoWayFriendsIDsKey + ownerUserID
} }
// getFriendKey returns the key for storing friend info in the cache.
func (f *FriendCacheRedis) getFriendKey(ownerUserID, friendUserID string) string { func (f *FriendCacheRedis) getFriendKey(ownerUserID, friendUserID string) string {
return friendKey + ownerUserID + "-" + friendUserID return friendKey + ownerUserID + "-" + friendUserID
} }
// GetFriendIDs retrieves friend IDs from the cache or the database if not found.
func (f *FriendCacheRedis) GetFriendIDs(ctx context.Context, ownerUserID string) (friendIDs []string, err error) { func (f *FriendCacheRedis) GetFriendIDs(ctx context.Context, ownerUserID string) (friendIDs []string, err error) {
return getCache(ctx, f.rcClient, f.getFriendIDsKey(ownerUserID), f.expireTime, func(ctx context.Context) ([]string, error) { return getCache(ctx, f.rcClient, f.getFriendIDsKey(ownerUserID), f.expireTime, func(ctx context.Context) ([]string, error) {
return f.friendDB.FindFriendUserIDs(ctx, ownerUserID) return f.friendDB.FindFriendUserIDs(ctx, ownerUserID)
}) })
} }
// DelFriendIDs deletes friend IDs from the cache.
func (f *FriendCacheRedis) DelFriendIDs(ownerUserIDs ...string) FriendCache { func (f *FriendCacheRedis) DelFriendIDs(ownerUserIDs ...string) FriendCache {
newGroupCache := f.NewCache() newGroupCache := f.NewCache()
keys := make([]string, 0, len(ownerUserIDs)) keys := make([]string, 0, len(ownerUserIDs))
@ -102,7 +110,7 @@ func (f *FriendCacheRedis) DelFriendIDs(ownerUserIDs ...string) FriendCache {
return newGroupCache return newGroupCache
} }
// todo. // GetTwoWayFriendIDs retrieves two-way friend IDs from the cache.
func (f *FriendCacheRedis) GetTwoWayFriendIDs(ctx context.Context, ownerUserID string) (twoWayFriendIDs []string, err error) { func (f *FriendCacheRedis) GetTwoWayFriendIDs(ctx context.Context, ownerUserID string) (twoWayFriendIDs []string, err error) {
friendIDs, err := f.GetFriendIDs(ctx, ownerUserID) friendIDs, err := f.GetFriendIDs(ctx, ownerUserID)
if err != nil { if err != nil {
@ -121,6 +129,7 @@ func (f *FriendCacheRedis) GetTwoWayFriendIDs(ctx context.Context, ownerUserID s
return twoWayFriendIDs, nil return twoWayFriendIDs, nil
} }
// DelTwoWayFriendIDs deletes two-way friend IDs from the cache.
func (f *FriendCacheRedis) DelTwoWayFriendIDs(ctx context.Context, ownerUserID string) FriendCache { func (f *FriendCacheRedis) DelTwoWayFriendIDs(ctx context.Context, ownerUserID string) FriendCache {
newFriendCache := f.NewCache() newFriendCache := f.NewCache()
newFriendCache.AddKeys(f.getTwoWayFriendsIDsKey(ownerUserID)) newFriendCache.AddKeys(f.getTwoWayFriendsIDsKey(ownerUserID))
@ -128,14 +137,15 @@ func (f *FriendCacheRedis) DelTwoWayFriendIDs(ctx context.Context, ownerUserID s
return newFriendCache return newFriendCache
} }
func (f *FriendCacheRedis) GetFriend(ctx context.Context, ownerUserID, // GetFriend retrieves friend info from the cache or the database if not found.
friendUserID string) (friend *relationtb.FriendModel, err error) { func (f *FriendCacheRedis) GetFriend(ctx context.Context, ownerUserID, friendUserID string) (friend *relationtb.FriendModel, err error) {
return getCache(ctx, f.rcClient, f.getFriendKey(ownerUserID, return getCache(ctx, f.rcClient, f.getFriendKey(ownerUserID,
friendUserID), f.expireTime, func(ctx context.Context) (*relationtb.FriendModel, error) { friendUserID), f.expireTime, func(ctx context.Context) (*relationtb.FriendModel, error) {
return f.friendDB.Take(ctx, ownerUserID, friendUserID) return f.friendDB.Take(ctx, ownerUserID, friendUserID)
}) })
} }
// DelFriend deletes friend info from the cache.
func (f *FriendCacheRedis) DelFriend(ownerUserID, friendUserID string) FriendCache { func (f *FriendCacheRedis) DelFriend(ownerUserID, friendUserID string) FriendCache {
newFriendCache := f.NewCache() newFriendCache := f.NewCache()
newFriendCache.AddKeys(f.getFriendKey(ownerUserID, friendUserID)) newFriendCache.AddKeys(f.getFriendKey(ownerUserID, friendUserID))

@ -79,21 +79,35 @@ func NewUserDatabase(userDB relation.UserModelInterface, cache cache.UserCache,
return &userDatabase{userDB: userDB, cache: cache, tx: tx, mongoDB: mongoDB} return &userDatabase{userDB: userDB, cache: cache, tx: tx, mongoDB: mongoDB}
} }
func (u *userDatabase) InitOnce(ctx context.Context, users []*relation.UserModel) (err error) {
userIDs := utils.Slice(users, func(e *relation.UserModel) string { func (u *userDatabase) InitOnce(ctx context.Context, users []*relation.UserModel) error {
return e.UserID // Extract user IDs from the given user models.
}) userIDs := utils.Slice(users, func(e *relation.UserModel) string {
result, err := u.userDB.Find(ctx, userIDs) return e.UserID
if err != nil { })
return err
} // Find existing users in the database.
miss := utils.SliceAnySub(users, result, func(e *relation.UserModel) string { return e.UserID }) existingUsers, err := u.userDB.Find(ctx, userIDs)
if len(miss) > 0 { if err != nil {
_ = u.userDB.Create(ctx, miss) return err
} }
return nil
// Determine which users are missing from the database.
missingUsers := utils.SliceAnySub(users, existingUsers, func(e *relation.UserModel) string {
return e.UserID
})
// Create records for missing users.
if len(missingUsers) > 0 {
if err := u.userDB.Create(ctx, missingUsers); err != nil {
return err
}
}
return nil
} }
// FindWithError Get the information of the specified user and return an error if the userID is not found. // FindWithError Get the information of the specified user and return an error if the userID is not found.
func (u *userDatabase) FindWithError(ctx context.Context, userIDs []string) (users []*relation.UserModel, err error) { func (u *userDatabase) FindWithError(ctx context.Context, userIDs []string) (users []*relation.UserModel, err error) {
users, err = u.cache.GetUsersInfo(ctx, userIDs) users, err = u.cache.GetUsersInfo(ctx, userIDs)

@ -0,0 +1,169 @@
package newmgo
import (
"context"
"github.com/openimsdk/open-im-server/v3/pkg/common/db/newmgo/mgotool"
"github.com/openimsdk/open-im-server/v3/pkg/common/db/table/relation"
"github.com/openimsdk/open-im-server/v3/pkg/common/pagination"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
)
// FriendMgo implements FriendModelInterface using MongoDB as the storage backend.
type FriendMgo struct {
coll *mongo.Collection
}
// NewFriendMongo creates a new instance of FriendMgo with the provided MongoDB database.
func NewFriendMongo(db *mongo.Database) (relation.FriendModelInterface, error) {
return &FriendMgo{
coll: db.Collection(relation.FriendModelCollectionName),
}, nil
}
// Create inserts multiple friend records.
func (f *FriendMgo) Create(ctx context.Context, friends []*relation.FriendModel) error {
return mgotool.InsertMany(ctx, f.coll, friends)
}
// Delete removes specified friends of the owner user.
func (f *FriendMgo) Delete(ctx context.Context, ownerUserID string, friendUserIDs []string) error {
filter := bson.M{
"owner_user_id": ownerUserID,
"friend_user_id": bson.M{"$in": friendUserIDs},
}
_, err := f.coll.DeleteMany(ctx, filter)
if err != nil {
return err
}
return nil
}
// UpdateByMap updates specific fields of a friend document using a map.
func (f *FriendMgo) UpdateByMap(ctx context.Context, ownerUserID string, friendUserID string, args map[string]interface{}) error {
if len(args) == 0 {
return nil
}
filter := bson.M{
"owner_user_id": ownerUserID,
"friend_user_id": friendUserID,
}
update := bson.M{"$set": args}
err := mgotool.UpdateOne(ctx, f.coll, filter, update, true)
if err != nil {
return err
}
return nil
}
// Update modifies multiple friend documents.
// func (f *FriendMgo) Update(ctx context.Context, friends []*relation.FriendModel) error {
// filter := bson.M{
// "owner_user_id": ownerUserID,
// "friend_user_id": friendUserID,
// }
// return mgotool.UpdateMany(ctx, f.coll, filter, friends)
// }
// UpdateRemark updates the remark for a specific friend.
func (f *FriendMgo) UpdateRemark(ctx context.Context, ownerUserID, friendUserID, remark string) error {
args := map[string]interface{}{"remark": remark}
return f.UpdateByMap(ctx, ownerUserID, friendUserID, args)
}
// Take retrieves a single friend document. Returns an error if not found.
func (f *FriendMgo) Take(ctx context.Context, ownerUserID, friendUserID string) (*relation.FriendModel, error) {
filter := bson.M{
"owner_user_id": ownerUserID,
"friend_user_id": friendUserID,
}
friend, err := mgotool.FindOne[*relation.FriendModel](ctx, f.coll, filter)
if err != nil {
return nil, err
}
return friend, nil
}
// FindUserState finds the friendship status between two users.
func (f *FriendMgo) FindUserState(ctx context.Context, userID1, userID2 string) ([]*relation.FriendModel, error) {
filter := bson.M{
"$or": []bson.M{
{"owner_user_id": userID1, "friend_user_id": userID2},
{"owner_user_id": userID2, "friend_user_id": userID1},
},
}
friends, err := mgotool.Find[*relation.FriendModel](ctx, f.coll, filter)
if err != nil {
return nil, err
}
return friends, nil
}
// FindFriends retrieves a list of friends for a given owner. Missing friends do not cause an error.
func (f *FriendMgo) FindFriends(ctx context.Context, ownerUserID string, friendUserIDs []string) ([]*relation.FriendModel, error) {
filter := bson.M{
"owner_user_id": ownerUserID,
"friend_user_id": bson.M{"$in": friendUserIDs},
}
friends, err := mgotool.Find[*relation.FriendModel](ctx, f.coll, filter)
if err != nil {
return nil, err
}
return friends, nil
}
// FindReversalFriends finds users who have added the specified user as a friend.
func (f *FriendMgo) FindReversalFriends(ctx context.Context, friendUserID string, ownerUserIDs []string) ([]*relation.FriendModel, error) {
filter := bson.M{
"owner_user_id": bson.M{"$in": ownerUserIDs},
"friend_user_id": friendUserID,
}
friends, err := mgotool.Find[*relation.FriendModel](ctx, f.coll, filter)
if err != nil {
return nil, err
}
return friends, nil
}
// FindOwnerFriends retrieves a paginated list of friends for a given owner.
func (f *FriendMgo) FindOwnerFriends(ctx context.Context, ownerUserID string, pagination pagination.Pagination, showNumber int32) ([]*relation.FriendModel, int64, error) {
filter := bson.M{"owner_user_id": ownerUserID}
count, friends, err := mgotool.FindPage[*relation.FriendModel](ctx, f.coll, filter, pagination)
if err != nil {
return nil, 0, err
}
return friends, count, nil
}
// FindInWhoseFriends finds users who have added the specified user as a friend, with pagination.
func (f *FriendMgo) FindInWhoseFriends(ctx context.Context, friendUserID string, pagination.Pagination, showNumber int32) ([]*relation.FriendModel, int64, error) {
filter := bson.M{"friend_user_id": friendUserID}
count, friends, err := mgotool.FindPage[*relation.FriendModel](ctx, f.coll, filter, pagination)
if err != nil {
return nil, 0, err
}
return friends, count, nil
}
// FindFriendUserIDs retrieves a list of friend user IDs for a given owner.
func (f *FriendMgo) FindFriendUserIDs(ctx context.Context, ownerUserID string) ([]string, error) {
filter := bson.M{"owner_user_id": ownerUserID}
friends := []*relation.FriendModel{}
friends, err := mgotool.Find[*relation.FriendModel](ctx, f.coll, filter)
if err != nil {
return nil, err
}
friendUserIDs := make([]string, len(friends))
for i, friend := range friends {
friendUserIDs[i] = friend.FriendUserID
}
return friendUserIDs, nil
}
// NewTx creates a new transaction.
func (f *FriendMgo) NewTx(tx any) relation.FriendModelInterface {
panic("not implemented")
return nil
}

@ -0,0 +1,48 @@
package newmgo
import (
"context"
"github.com/openimsdk/open-im-server/v3/pkg/common/db/newmgo/mgotool"
"github.com/openimsdk/open-im-server/v3/pkg/common/db/table/relation"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
)
type FriendRequestMgo struct {
coll *mongo.Collection
}
func NewFriendRequestMongo(db *mongo.Database) (relation.FriendRequestModelInterface, error) {
return &FriendRequestMgo{
coll: db.Collection(relation.FriendRequestModelCollectionName),
}, nil
}
func (f *FriendRequestMgo) Create(ctx context.Context, friendRequests []*relation.FriendRequestModel) error {
return mgotool.InsertMany(ctx, f.coll, friendRequests)
}
func (f *FriendRequestMgo) Delete(ctx context.Context, fromUserID, toUserID string) (err error) {
return mgotool.Delete[*relation.FriendRequestModel](ctx, f.coll, bson.M{"from_user_id": fromUserID, "to_user_id": toUserID})
}
func (f *FriendRequestMgo) UpdateByMap(ctx context.Context, formUserID, toUserID string, args map[string]any) (err error) {
if len(args) == 0 {
return nil
}
return mgotool.UpdateOne(ctx, f.coll, bson.M{"from_user_id": formUserID, "to_user_id": toUserID}, bson.M{"$set": args}, true)
}
func (f *FriendRequestMgo) Update(ctx context.Context, friendRequest *relation.FriendRequestModel) (err error) {
return mgotool.UpdateOne(ctx, f.coll, bson.M{"_id": friendRequest.ID}, bson.M{"$set": friendRequest}, true)
}
func (f *FriendRequestMgo) Find(ctx context.Context, fromUserID, toUserID string) (friendRequest *relation.FriendRequestModel, err error) {
return mgotool.FindOne[*relation.FriendRequestModel](ctx, f.coll, bson.M{"from_user_id": fromUserID, "to_user_id": toUserID})
}
func (f *FriendRequestMgo) Take(ctx context.Context, fromUserID, toUserID string) (friendRequest *relation.FriendRequestModel, err error) {
return f.Find(ctx, fromUserID, toUserID)
}

@ -2,12 +2,14 @@ package mgotool
import ( import (
"context" "context"
"github.com/OpenIMSDK/tools/errs" "github.com/OpenIMSDK/tools/errs"
"github.com/openimsdk/open-im-server/v3/pkg/common/pagination" "github.com/openimsdk/open-im-server/v3/pkg/common/pagination"
"go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options" "go.mongodb.org/mongo-driver/mongo/options"
) )
func basic[T any]() bool { func basic[T any]() bool {
var t T var t T
switch any(t).(type) { switch any(t).(type) {
@ -175,6 +177,14 @@ func DeleteMany(ctx context.Context, coll *mongo.Collection, filter any, opts ..
return nil return nil
} }
// TODO
func Delete[T any](ctx context.Context, coll *mongo.Collection, filter any, opts ...*options.DeleteOptions) error {
if _, err := coll.DeleteMany(ctx, filter, opts...); err != nil {
return errs.Wrap(err)
}
return nil
}
func Aggregate[T any](ctx context.Context, coll *mongo.Collection, pipeline any, opts ...*options.AggregateOptions) ([]T, error) { func Aggregate[T any](ctx context.Context, coll *mongo.Collection, pipeline any, opts ...*options.AggregateOptions) ([]T, error) {
cur, err := coll.Aggregate(ctx, pipeline, opts...) cur, err := coll.Aggregate(ctx, pipeline, opts...)
if err != nil { if err != nil {

@ -14,151 +14,141 @@
package relation package relation
import ( // type FriendRequestGorm struct {
"context" // *MetaDB
// }
"gorm.io/gorm"
// func NewFriendRequestGorm(db *gorm.DB) relation.FriendRequestModelInterface {
"github.com/OpenIMSDK/tools/utils" // return &FriendRequestGorm{NewMetaDB(db, &relation.FriendRequestModel{})}
// }
"github.com/openimsdk/open-im-server/v3/pkg/common/db/table/relation"
) // func (f *FriendRequestGorm) NewTx(tx any) relation.FriendRequestModelInterface {
// return &FriendRequestGorm{NewMetaDB(tx.(*gorm.DB), &relation.FriendRequestModel{})}
type FriendRequestGorm struct { // }
*MetaDB
} // // 插入多条记录.
// func (f *FriendRequestGorm) Create(ctx context.Context, friendRequests []*relation.FriendRequestModel) (err error) {
func NewFriendRequestGorm(db *gorm.DB) relation.FriendRequestModelInterface { // return utils.Wrap(f.db(ctx).Create(&friendRequests).Error, "")
return &FriendRequestGorm{NewMetaDB(db, &relation.FriendRequestModel{})} // }
}
// // 删除记录.
func (f *FriendRequestGorm) NewTx(tx any) relation.FriendRequestModelInterface { // func (f *FriendRequestGorm) Delete(ctx context.Context, fromUserID, toUserID string) (err error) {
return &FriendRequestGorm{NewMetaDB(tx.(*gorm.DB), &relation.FriendRequestModel{})} // return utils.Wrap(
} // f.db(ctx).
// Where("from_user_id = ? AND to_user_id = ?", fromUserID, toUserID).
// 插入多条记录. // Delete(&relation.FriendRequestModel{}).
func (f *FriendRequestGorm) Create(ctx context.Context, friendRequests []*relation.FriendRequestModel) (err error) { // Error,
return utils.Wrap(f.db(ctx).Create(&friendRequests).Error, "") // "",
} // )
// }
// 删除记录.
func (f *FriendRequestGorm) Delete(ctx context.Context, fromUserID, toUserID string) (err error) { // // 更新零值.
return utils.Wrap( // func (f *FriendRequestGorm) UpdateByMap(
f.db(ctx). // ctx context.Context,
Where("from_user_id = ? AND to_user_id = ?", fromUserID, toUserID). // fromUserID string,
Delete(&relation.FriendRequestModel{}). // toUserID string,
Error, // args map[string]any,
"", // ) (err error) {
) // return utils.Wrap(
} // f.db(ctx).
// Model(&relation.FriendRequestModel{}).
// 更新零值. // Where("from_user_id = ? AND to_user_id =?", fromUserID, toUserID).
func (f *FriendRequestGorm) UpdateByMap( // Updates(args).
ctx context.Context, // Error,
fromUserID string, // "",
toUserID string, // )
args map[string]any, // }
) (err error) {
return utils.Wrap( // // 更新记录 (非零值).
f.db(ctx). // func (f *FriendRequestGorm) Update(ctx context.Context, friendRequest *relation.FriendRequestModel) (err error) {
Model(&relation.FriendRequestModel{}). // fr2 := *friendRequest
Where("from_user_id = ? AND to_user_id =?", fromUserID, toUserID). // fr2.FromUserID = ""
Updates(args). // fr2.ToUserID = ""
Error, // return utils.Wrap(
"", // f.db(ctx).
) // Where("from_user_id = ? AND to_user_id =?", friendRequest.FromUserID, friendRequest.ToUserID).
} // Updates(fr2).
// Error,
// 更新记录 (非零值). // "",
func (f *FriendRequestGorm) Update(ctx context.Context, friendRequest *relation.FriendRequestModel) (err error) { // )
fr2 := *friendRequest // }
fr2.FromUserID = ""
fr2.ToUserID = "" // // 获取来指定用户的好友申请 未找到 不返回错误.
return utils.Wrap( // func (f *FriendRequestGorm) Find(
f.db(ctx). // ctx context.Context,
Where("from_user_id = ? AND to_user_id =?", friendRequest.FromUserID, friendRequest.ToUserID). // fromUserID, toUserID string,
Updates(fr2). // ) (friendRequest *relation.FriendRequestModel, err error) {
Error, // friendRequest = &relation.FriendRequestModel{}
"", // err = utils.Wrap(
) // f.db(ctx).Where("from_user_id = ? and to_user_id = ?", fromUserID, toUserID).Find(friendRequest).Error,
} // "",
// )
// 获取来指定用户的好友申请 未找到 不返回错误. // return friendRequest, err
func (f *FriendRequestGorm) Find( // }
ctx context.Context,
fromUserID, toUserID string, // func (f *FriendRequestGorm) Take(
) (friendRequest *relation.FriendRequestModel, err error) { // ctx context.Context,
friendRequest = &relation.FriendRequestModel{} // fromUserID, toUserID string,
err = utils.Wrap( // ) (friendRequest *relation.FriendRequestModel, err error) {
f.db(ctx).Where("from_user_id = ? and to_user_id = ?", fromUserID, toUserID).Find(friendRequest).Error, // friendRequest = &relation.FriendRequestModel{}
"", // err = utils.Wrap(
) // f.db(ctx).Where("from_user_id = ? and to_user_id = ?", fromUserID, toUserID).Take(friendRequest).Error,
return friendRequest, err // "",
} // )
// return friendRequest, err
func (f *FriendRequestGorm) Take( // }
ctx context.Context,
fromUserID, toUserID string, // // 获取toUserID收到的好友申请列表.
) (friendRequest *relation.FriendRequestModel, err error) { // func (f *FriendRequestGorm) FindToUserID(
friendRequest = &relation.FriendRequestModel{} // ctx context.Context,
err = utils.Wrap( // toUserID string,
f.db(ctx).Where("from_user_id = ? and to_user_id = ?", fromUserID, toUserID).Take(friendRequest).Error, // pageNumber, showNumber int32,
"", // ) (friendRequests []*relation.FriendRequestModel, total int64, err error) {
) // err = f.db(ctx).Model(&relation.FriendRequestModel{}).Where("to_user_id = ? ", toUserID).Count(&total).Error
return friendRequest, err // if err != nil {
} // return nil, 0, utils.Wrap(err, "")
// }
// 获取toUserID收到的好友申请列表. // err = utils.Wrap(
func (f *FriendRequestGorm) FindToUserID( // f.db(ctx).
ctx context.Context, // Where("to_user_id = ? ", toUserID).
toUserID string, // Limit(int(showNumber)).
pageNumber, showNumber int32, // Offset(int(pageNumber-1)*int(showNumber)).
) (friendRequests []*relation.FriendRequestModel, total int64, err error) { // Find(&friendRequests).
err = f.db(ctx).Model(&relation.FriendRequestModel{}).Where("to_user_id = ? ", toUserID).Count(&total).Error // Error,
if err != nil { // "",
return nil, 0, utils.Wrap(err, "") // )
} // return
err = utils.Wrap( // }
f.db(ctx).
Where("to_user_id = ? ", toUserID). // // 获取fromUserID发出去的好友申请列表.
Limit(int(showNumber)). // func (f *FriendRequestGorm) FindFromUserID(
Offset(int(pageNumber-1)*int(showNumber)). // ctx context.Context,
Find(&friendRequests). // fromUserID string,
Error, // pageNumber, showNumber int32,
"", // ) (friendRequests []*relation.FriendRequestModel, total int64, err error) {
) // err = f.db(ctx).Model(&relation.FriendRequestModel{}).Where("from_user_id = ? ", fromUserID).Count(&total).Error
return // if err != nil {
} // return nil, 0, utils.Wrap(err, "")
// }
// 获取fromUserID发出去的好友申请列表. // err = utils.Wrap(
func (f *FriendRequestGorm) FindFromUserID( // f.db(ctx).
ctx context.Context, // Where("from_user_id = ? ", fromUserID).
fromUserID string, // Limit(int(showNumber)).
pageNumber, showNumber int32, // Offset(int(pageNumber-1)*int(showNumber)).
) (friendRequests []*relation.FriendRequestModel, total int64, err error) { // Find(&friendRequests).
err = f.db(ctx).Model(&relation.FriendRequestModel{}).Where("from_user_id = ? ", fromUserID).Count(&total).Error // Error,
if err != nil { // "",
return nil, 0, utils.Wrap(err, "") // )
} // return
err = utils.Wrap( // }
f.db(ctx).
Where("from_user_id = ? ", fromUserID). // func (f *FriendRequestGorm) FindBothFriendRequests(ctx context.Context, fromUserID, toUserID string) (friends []*relation.FriendRequestModel, err error) {
Limit(int(showNumber)). // err = utils.Wrap(
Offset(int(pageNumber-1)*int(showNumber)). // f.db(ctx).
Find(&friendRequests). // Where("(from_user_id = ? AND to_user_id = ?) OR (from_user_id = ? AND to_user_id = ?)", fromUserID, toUserID, toUserID, fromUserID).
Error, // Find(&friends).
"", // Error,
) // "",
return // )
} // return
// }
func (f *FriendRequestGorm) FindBothFriendRequests(ctx context.Context, fromUserID, toUserID string) (friends []*relation.FriendRequestModel, err error) {
err = utils.Wrap(
f.db(ctx).
Where("(from_user_id = ? AND to_user_id = ?) OR (from_user_id = ? AND to_user_id = ?)", fromUserID, toUserID, toUserID, fromUserID).
Find(&friends).
Error,
"",
)
return
}

@ -17,62 +17,65 @@ package relation
import ( import (
"context" "context"
"time" "time"
"go.mongodb.org/mongo-driver/bson/primitive"
) )
const ( const (
FriendModelTableName = "friends" FriendModelCollectionName = "friends"
) )
// OwnerUserID string `gorm:"column:owner_user_id;primary_key;size:64"`
// FriendUserID string `gorm:"column:friend_user_id;primary_key;size:64"`
// Remark string `gorm:"column:remark;size:255"`
// CreateTime time.Time `gorm:"column:create_time;autoCreateTime"`
// AddSource int32 `gorm:"column:add_source"`
// OperatorUserID string `gorm:"column:operator_user_id;size:64"`
// Ex string `gorm:"column:ex;size:1024"`
// FriendModel represents the data structure for a friend relationship in MongoDB.
type FriendModel struct { type FriendModel struct {
OwnerUserID string `gorm:"column:owner_user_id;primary_key;size:64"` ID primitive.ObjectID `bson:"_id,omitempty"`
FriendUserID string `gorm:"column:friend_user_id;primary_key;size:64"` OwnerUserID string `bson:"owner_user_id"`
Remark string `gorm:"column:remark;size:255"` FriendUserID string `bson:"friend_user_id"`
CreateTime time.Time `gorm:"column:create_time;autoCreateTime"` Remark string `bson:"remark"`
AddSource int32 `gorm:"column:add_source"` CreateTime time.Time `bson:"create_time"`
OperatorUserID string `gorm:"column:operator_user_id;size:64"` AddSource int32 `bson:"add_source"`
Ex string `gorm:"column:ex;size:1024"` OperatorUserID string `bson:"operator_user_id"`
Ex string `bson:"ex"`
} }
func (FriendModel) TableName() string { // CollectionName returns the name of the MongoDB collection.
return FriendModelTableName func (FriendModel) CollectionName() string {
return FriendModelCollectionName
} }
// FriendModelInterface defines the operations for managing friends in MongoDB.
type FriendModelInterface interface { type FriendModelInterface interface {
// 插入多条记录 // Create inserts multiple friend records.
Create(ctx context.Context, friends []*FriendModel) (err error) Create(ctx context.Context, friends []*FriendModel) (err error)
// 删除ownerUserID指定的好友 // Delete removes specified friends of the owner user.
Delete(ctx context.Context, ownerUserID string, friendUserIDs []string) (err error) Delete(ctx context.Context, ownerUserID string, friendUserIDs []string) (err error)
// 更新ownerUserID单个好友信息 更新零值 // UpdateByMap updates specific fields of a friend document using a map.
UpdateByMap(ctx context.Context, ownerUserID string, friendUserID string, args map[string]any) (err error) UpdateByMap(ctx context.Context, ownerUserID string, friendUserID string, args map[string]any) (err error)
// 更新好友信息的非零值 // Update modifies multiple friend documents.
Update(ctx context.Context, friends []*FriendModel) (err error) // Update(ctx context.Context, friends []*FriendModel) (err error)
// 更新好友备注(也支持零值 // UpdateRemark updates the remark for a specific friend.
UpdateRemark(ctx context.Context, ownerUserID, friendUserID, remark string) (err error) UpdateRemark(ctx context.Context, ownerUserID, friendUserID, remark string) (err error)
// 获取单个好友信息,如没找到 返回错误 // Take retrieves a single friend document. Returns an error if not found.
Take(ctx context.Context, ownerUserID, friendUserID string) (friend *FriendModel, err error) Take(ctx context.Context, ownerUserID, friendUserID string) (friend *FriendModel, err error)
// 查找好友关系,如果是双向关系,则都返回 // FindUserState finds the friendship status between two users.
FindUserState(ctx context.Context, userID1, userID2 string) (friends []*FriendModel, err error) FindUserState(ctx context.Context, userID1, userID2 string) (friends []*FriendModel, err error)
// 获取 owner指定的好友列表 如果有friendUserIDs不存在也不返回错误 // FindFriends retrieves a list of friends for a given owner. Missing friends do not cause an error.
FindFriends(ctx context.Context, ownerUserID string, friendUserIDs []string) (friends []*FriendModel, err error) FindFriends(ctx context.Context, ownerUserID string, friendUserIDs []string) (friends []*FriendModel, err error)
// 获取哪些人添加了friendUserID 如果有ownerUserIDs不存在也不返回错误 // FindReversalFriends finds users who have added the specified user as a friend.
FindReversalFriends( FindReversalFriends(ctx context.Context, friendUserID string, ownerUserIDs []string) (friends []*FriendModel, err error)
ctx context.Context, // FindOwnerFriends retrieves a paginated list of friends for a given owner.
friendUserID string, FindOwnerFriends(ctx context.Context, ownerUserID string, pageNumber, showNumber int32) (friends []*FriendModel, total int64, err error)
ownerUserIDs []string, // FindInWhoseFriends finds users who have added the specified user as a friend, with pagination.
) (friends []*FriendModel, err error) FindInWhoseFriends(ctx context.Context, friendUserID string, pageNumber, showNumber int32) (friends []*FriendModel, total int64, err error)
// 获取ownerUserID好友列表 支持翻页 // FindFriendUserIDs retrieves a list of friend user IDs for a given owner.
FindOwnerFriends(
ctx context.Context,
ownerUserID string,
pageNumber, showNumber int32,
) (friends []*FriendModel, total int64, err error)
// 获取哪些人添加了friendUserID 支持翻页
FindInWhoseFriends(
ctx context.Context,
friendUserID string,
pageNumber, showNumber int32,
) (friends []*FriendModel, total int64, err error)
// 获取好友UserID列表
FindFriendUserIDs(ctx context.Context, ownerUserID string) (friendUserIDs []string, err error) FindFriendUserIDs(ctx context.Context, ownerUserID string) (friendUserIDs []string, err error)
// NewTx creates a new transaction.
NewTx(tx any) FriendModelInterface NewTx(tx any) FriendModelInterface
} }

@ -17,50 +17,47 @@ package relation
import ( import (
"context" "context"
"time" "time"
"go.mongodb.org/mongo-driver/bson/primitive"
) )
const FriendRequestModelTableName = "friend_requests" const FriendRequestModelCollectionName = "friend_requests"
type FriendRequestModel struct { type FriendRequestModel struct {
FromUserID string `gorm:"column:from_user_id;primary_key;size:64"` ID primitive.ObjectID `bson:"_id,omitempty"`
ToUserID string `gorm:"column:to_user_id;primary_key;size:64"` FromUserID string `bson:"from_user_id"`
HandleResult int32 `gorm:"column:handle_result"` ToUserID string `bson:"to_user_id"`
ReqMsg string `gorm:"column:req_msg;size:255"` HandleResult int32 `bson:"handle_result"`
CreateTime time.Time `gorm:"column:create_time; autoCreateTime"` ReqMsg string `bson:"req_msg"`
HandlerUserID string `gorm:"column:handler_user_id;size:64"` CreateTime time.Time `bson:"create_time"`
HandleMsg string `gorm:"column:handle_msg;size:255"` HandlerUserID string `bson:"handler_user_id"`
HandleTime time.Time `gorm:"column:handle_time"` HandleMsg string `bson:"handle_msg"`
Ex string `gorm:"column:ex;size:1024"` HandleTime time.Time `bson:"handle_time"`
Ex string `bson:"ex"`
} }
func (FriendRequestModel) TableName() string { func (FriendRequestModel) CollectionName() string {
return FriendRequestModelTableName return FriendRequestModelCollectionName
} }
type FriendRequestModelInterface interface { type FriendRequestModelInterface interface {
// 插入多条记录 // Insert multiple records
Create(ctx context.Context, friendRequests []*FriendRequestModel) (err error) Create(ctx context.Context, friendRequests []*FriendRequestModel) (err error)
// 删除记录 // Delete record
Delete(ctx context.Context, fromUserID, toUserID string) (err error) Delete(ctx context.Context, fromUserID, toUserID string) (err error)
// 更新零值 // Update with zero values
UpdateByMap(ctx context.Context, formUserID string, toUserID string, args map[string]any) (err error) UpdateByMap(ctx context.Context, formUserID string, toUserID string, args map[string]any) (err error)
// 更新多条记录 (非零值) // Update multiple records (non-zero values)
Update(ctx context.Context, friendRequest *FriendRequestModel) (err error) Update(ctx context.Context, friendRequest *FriendRequestModel) (err error)
// 获取来指定用户的好友申请 未找到 不返回错误 // Get friend requests sent to a specific user, no error returned if not found
Find(ctx context.Context, fromUserID, toUserID string) (friendRequest *FriendRequestModel, err error) Find(ctx context.Context, fromUserID, toUserID string) (friendRequest *FriendRequestModel, err error)
Take(ctx context.Context, fromUserID, toUserID string) (friendRequest *FriendRequestModel, err error) Take(ctx context.Context, fromUserID, toUserID string) (friendRequest *FriendRequestModel, err error)
// 获取toUserID收到的好友申请列表 // Get list of friend requests received by toUserID
FindToUserID( FindToUserID(ctx context.Context,toUserID string,pageNumber, showNumber int32,) (friendRequests []*FriendRequestModel, total int64, err error)
ctx context.Context, // Get list of friend requests sent by fromUserID
toUserID string, FindFromUserID(ctx context.Context,fromUserID string,pageNumber, showNumber int32,) (friendRequests []*FriendRequestModel, total int64, err error)
pageNumber, showNumber int32,
) (friendRequests []*FriendRequestModel, total int64, err error)
// 获取fromUserID发出去的好友申请列表
FindFromUserID(
ctx context.Context,
fromUserID string,
pageNumber, showNumber int32,
) (friendRequests []*FriendRequestModel, total int64, err error)
FindBothFriendRequests(ctx context.Context, fromUserID, toUserID string) (friends []*FriendRequestModel, err error) FindBothFriendRequests(ctx context.Context, fromUserID, toUserID string) (friends []*FriendRequestModel, err error)
NewTx(tx any) FriendRequestModelInterface NewTx(tx any) FriendRequestModelInterface
// Check if the record exists
Exist(ctx context.Context, userID string) (exist bool, err error)
} }

Loading…
Cancel
Save