You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
165 lines
6.1 KiB
165 lines
6.1 KiB
11 months ago
|
// 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.
|
||
|
|
||
12 months ago
|
package mgo
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
6 months ago
|
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/database"
|
||
|
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/model"
|
||
10 months ago
|
|
||
7 months ago
|
"github.com/openimsdk/tools/db/mongoutil"
|
||
|
"github.com/openimsdk/tools/db/pagination"
|
||
12 months ago
|
"go.mongodb.org/mongo-driver/bson"
|
||
|
"go.mongodb.org/mongo-driver/mongo"
|
||
9 months ago
|
"go.mongodb.org/mongo-driver/mongo/options"
|
||
12 months ago
|
)
|
||
|
|
||
6 months ago
|
// FriendMgo implements Friend using MongoDB as the storage backend.
|
||
12 months ago
|
type FriendMgo struct {
|
||
|
coll *mongo.Collection
|
||
|
}
|
||
|
|
||
|
// NewFriendMongo creates a new instance of FriendMgo with the provided MongoDB database.
|
||
6 months ago
|
func NewFriendMongo(db *mongo.Database) (database.Friend, error) {
|
||
12 months ago
|
coll := db.Collection("friend")
|
||
|
_, err := coll.Indexes().CreateOne(context.Background(), mongo.IndexModel{
|
||
|
Keys: bson.D{
|
||
|
{Key: "owner_user_id", Value: 1},
|
||
|
{Key: "friend_user_id", Value: 1},
|
||
|
},
|
||
|
Options: options.Index().SetUnique(true),
|
||
|
})
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
return &FriendMgo{coll: coll}, nil
|
||
|
}
|
||
|
|
||
|
// Create inserts multiple friend records.
|
||
6 months ago
|
func (f *FriendMgo) Create(ctx context.Context, friends []*model.Friend) error {
|
||
7 months ago
|
return mongoutil.InsertMany(ctx, f.coll, friends)
|
||
12 months ago
|
}
|
||
|
|
||
|
// 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},
|
||
|
}
|
||
7 months ago
|
return mongoutil.DeleteOne(ctx, f.coll, filter)
|
||
12 months ago
|
}
|
||
|
|
||
|
// 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,
|
||
|
}
|
||
7 months ago
|
return mongoutil.UpdateOne(ctx, f.coll, filter, bson.M{"$set": args}, true)
|
||
12 months ago
|
}
|
||
|
|
||
|
// Update modifies multiple friend documents.
|
||
6 months ago
|
// func (f *FriendMgo) Update(ctx context.Context, friends []*relation.Friend) error {
|
||
12 months ago
|
// 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 {
|
||
|
return f.UpdateByMap(ctx, ownerUserID, friendUserID, map[string]any{"remark": remark})
|
||
|
}
|
||
|
|
||
|
// Take retrieves a single friend document. Returns an error if not found.
|
||
6 months ago
|
func (f *FriendMgo) Take(ctx context.Context, ownerUserID, friendUserID string) (*model.Friend, error) {
|
||
12 months ago
|
filter := bson.M{
|
||
|
"owner_user_id": ownerUserID,
|
||
|
"friend_user_id": friendUserID,
|
||
|
}
|
||
6 months ago
|
return mongoutil.FindOne[*model.Friend](ctx, f.coll, filter)
|
||
12 months ago
|
}
|
||
|
|
||
|
// FindUserState finds the friendship status between two users.
|
||
6 months ago
|
func (f *FriendMgo) FindUserState(ctx context.Context, userID1, userID2 string) ([]*model.Friend, error) {
|
||
12 months ago
|
filter := bson.M{
|
||
|
"$or": []bson.M{
|
||
|
{"owner_user_id": userID1, "friend_user_id": userID2},
|
||
|
{"owner_user_id": userID2, "friend_user_id": userID1},
|
||
|
},
|
||
|
}
|
||
6 months ago
|
return mongoutil.Find[*model.Friend](ctx, f.coll, filter)
|
||
12 months ago
|
}
|
||
|
|
||
|
// FindFriends retrieves a list of friends for a given owner. Missing friends do not cause an error.
|
||
6 months ago
|
func (f *FriendMgo) FindFriends(ctx context.Context, ownerUserID string, friendUserIDs []string) ([]*model.Friend, error) {
|
||
12 months ago
|
filter := bson.M{
|
||
|
"owner_user_id": ownerUserID,
|
||
|
"friend_user_id": bson.M{"$in": friendUserIDs},
|
||
|
}
|
||
6 months ago
|
return mongoutil.Find[*model.Friend](ctx, f.coll, filter)
|
||
12 months ago
|
}
|
||
|
|
||
|
// FindReversalFriends finds users who have added the specified user as a friend.
|
||
6 months ago
|
func (f *FriendMgo) FindReversalFriends(ctx context.Context, friendUserID string, ownerUserIDs []string) ([]*model.Friend, error) {
|
||
12 months ago
|
filter := bson.M{
|
||
|
"owner_user_id": bson.M{"$in": ownerUserIDs},
|
||
|
"friend_user_id": friendUserID,
|
||
|
}
|
||
6 months ago
|
return mongoutil.Find[*model.Friend](ctx, f.coll, filter)
|
||
12 months ago
|
}
|
||
|
|
||
|
// FindOwnerFriends retrieves a paginated list of friends for a given owner.
|
||
6 months ago
|
func (f *FriendMgo) FindOwnerFriends(ctx context.Context, ownerUserID string, pagination pagination.Pagination) (int64, []*model.Friend, error) {
|
||
12 months ago
|
filter := bson.M{"owner_user_id": ownerUserID}
|
||
6 months ago
|
return mongoutil.FindPage[*model.Friend](ctx, f.coll, filter, pagination)
|
||
12 months ago
|
}
|
||
|
|
||
|
// FindInWhoseFriends finds users who have added the specified user as a friend, with pagination.
|
||
6 months ago
|
func (f *FriendMgo) FindInWhoseFriends(ctx context.Context, friendUserID string, pagination pagination.Pagination) (int64, []*model.Friend, error) {
|
||
12 months ago
|
filter := bson.M{"friend_user_id": friendUserID}
|
||
6 months ago
|
return mongoutil.FindPage[*model.Friend](ctx, f.coll, filter, pagination)
|
||
12 months ago
|
}
|
||
|
|
||
|
// 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}
|
||
7 months ago
|
return mongoutil.Find[string](ctx, f.coll, filter, options.Find().SetProjection(bson.M{"_id": 0, "friend_user_id": 1}))
|
||
12 months ago
|
}
|
||
11 months ago
|
|
||
10 months ago
|
func (f *FriendMgo) UpdateFriends(ctx context.Context, ownerUserID string, friendUserIDs []string, val map[string]any) error {
|
||
|
// Ensure there are IDs to update
|
||
|
if len(friendUserIDs) == 0 {
|
||
|
return nil // Or return an error if you expect there to always be IDs
|
||
11 months ago
|
}
|
||
|
|
||
10 months ago
|
// Create a filter to match documents with the specified ownerUserID and any of the friendUserIDs
|
||
|
filter := bson.M{
|
||
|
"owner_user_id": ownerUserID,
|
||
|
"friend_user_id": bson.M{"$in": friendUserIDs},
|
||
11 months ago
|
}
|
||
|
|
||
10 months ago
|
// Create an update document
|
||
|
update := bson.M{"$set": val}
|
||
11 months ago
|
|
||
10 months ago
|
// Perform the update operation for all matching documents
|
||
7 months ago
|
_, err := mongoutil.UpdateMany(ctx, f.coll, filter, update)
|
||
10 months ago
|
return err
|
||
11 months ago
|
}
|