mirror of
https://github.com/openimsdk/open-im-server.git
synced 2026-04-28 14:29:19 +08:00
19f5c94d25
* update set pin friends
* update set pin friends
* update set pin friends
* update set pin friends
* update set pin friends
* update set pin friends
* fix bugs
* fix bugs
* debug
* debug
* debug
* debug
* debug
* debug
* debug
* debug
* debug
* debug
* debug
* debug
* Update go.mod
* Update friend.go
* debug
* debug
* debug
* add pin friend test
* add pin friend test
* add pin friend test
* add pin friend test
* add pin friend test
* add pin friend test
* add pin friend test
* add pin friend test
* add pin friend test
* I cannot solve todo in test.sh
* update user command
* update user command
* update user command
* update user command
* update user command
* update user command
* update user command
* update user command
* update user command
* update user command
* update user command
* update user command
* update user command
* update user command
* Update go.mod
* fix group notification
* fix group notification
* update openimsdk tools
* update openim server remove duplicate code
* update openim server remove duplicate code
* update user command get
* update user command get
* update response of callback response error
* update black ex
* update join group ex
* update user pb2map
* update go sum
* update go sum
* update updateUserInfoEx
* update updateUserInfoEx
* update updateUserInfoEx add callback functions
* fix dismiss group function
* fix dismiss group function
* fix dismiss group function
* fix dismiss group function
* update pin friend to update friend
* fix go mod
* fix err golangci-lint
* fix UserPb2DBMap
* update comments, update go.mod check for register username
* update comments, update go.mod check for register username
* update comments, update go.mod check for register username
* update comments, update go.mod check for register username
* fix callback
* fix go.mod
* fix debug
* fix bugs
* update notification
* update notification
* update notification
* update notification
* update notification
* update notification
* update notification
* update notification
* fix updateUserInfoEx
* fix updateUserInfoEx
* modify go.mod
* fix updateUserInfoEx
* fix updateUserInfoEx
* fix: fix the bug
* fix: fix the imAdmin permission and searchNoficitaion resp
* fix updateUserInfoEx
* 2023 Annual Summary Reflections and Aspirations
Signed-off-by: Xinwei Xiong (cubxxw) <3293172751nss@gmail.com>
* fix: dissmissGroup and lack of keyword bug (#1678)
* Update docker-start-all.sh
* Update env-template.yaml
* update user command notification
* Update docker-start-all.sh
* fix openim config mongo passwd env
Signed-off-by: Xinwei Xiong (cubxxw) <3293172751nss@gmail.com>
* update user command get all notification
* update user command get all notification
* fix type = 0
* fix type = 0
* fix type = 0
* fix type = 0
* fix type = 0
* fix type = 0
* fix typing cause callback
* add ex to usercommand
* add ex to usercommand
* update updatefriends
* fix updatefriend map
* fix updatefriend FriendsInfoUpdateNotification
* fix push online and offline user, but why typing trigger callback push?
* fix push online and offline user, but why typing trigger callback push?
* update user command record not found and user access check
* update user command get all user access check
* update go.mod
* fix callback name and place
* fix: fix some bug
* fix: group messages sync failed.
* upadtae callback test
* fix callback typing
* fix callback typing
* fix callback typing
* fix callback typing
* fix lint on processusercommand
* fix: fix the valiable name
* fix: fix the getSortedConversation api
* fix: fix the mongo search error
* fix: GroupApplicationAcceptedNotification
(cherry picked from commit 4c3c4555a3)
* fix: GroupApplicationAcceptedNotification
* fix update friends
* fix pageFindUser
* fix pageFindUser
* fix pageFindUser
* fix pageFindUser
* Delete .devcontainer directory
* revert
* more powerful PageFindUserWithKeyword
* more powerful PageFindUserWithKeyword
* more powerful PageFindUserWithKeyword
* bug: update nickname
* bug: update nickname
---------
Signed-off-by: Xinwei Xiong (cubxxw) <3293172751nss@gmail.com>
Co-authored-by: Xinwei Xiong <3293172751@qq.com>
Co-authored-by: luhaoling <2198702716@qq.com>
Co-authored-by: Xinwei Xiong (cubxxw) <3293172751nss@gmail.com>
Co-authored-by: Brabem <69128477+luhaoling@users.noreply.github.com>
Co-authored-by: OpenIM Bot <124379614+kubbot@users.noreply.github.com>
Co-authored-by: OpenIM Robot <139873238+openimbot@users.noreply.github.com>
Co-authored-by: Gordon <46924906+FGadvancer@users.noreply.github.com>
Co-authored-by: withchao <993506633@qq.com>
314 lines
9.4 KiB
Go
314 lines
9.4 KiB
Go
// 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 mgo
|
|
|
|
import (
|
|
"context"
|
|
"github.com/OpenIMSDK/protocol/user"
|
|
"github.com/OpenIMSDK/tools/errs"
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
"time"
|
|
|
|
"github.com/OpenIMSDK/tools/mgoutil"
|
|
"github.com/OpenIMSDK/tools/pagination"
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
|
|
"github.com/openimsdk/open-im-server/v3/pkg/common/db/table/relation"
|
|
)
|
|
|
|
func NewUserMongo(db *mongo.Database) (relation.UserModelInterface, error) {
|
|
coll := db.Collection("user")
|
|
_, err := coll.Indexes().CreateOne(context.Background(), mongo.IndexModel{
|
|
Keys: bson.D{
|
|
{Key: "user_id", Value: 1},
|
|
},
|
|
Options: options.Index().SetUnique(true),
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &UserMgo{coll: coll}, nil
|
|
}
|
|
|
|
type UserMgo struct {
|
|
coll *mongo.Collection
|
|
}
|
|
|
|
func (u *UserMgo) Create(ctx context.Context, users []*relation.UserModel) error {
|
|
return mgoutil.InsertMany(ctx, u.coll, users)
|
|
}
|
|
|
|
func (u *UserMgo) UpdateByMap(ctx context.Context, userID string, args map[string]any) (err error) {
|
|
if len(args) == 0 {
|
|
return nil
|
|
}
|
|
return mgoutil.UpdateOne(ctx, u.coll, bson.M{"user_id": userID}, bson.M{"$set": args}, true)
|
|
}
|
|
|
|
func (u *UserMgo) Find(ctx context.Context, userIDs []string) (users []*relation.UserModel, err error) {
|
|
return mgoutil.Find[*relation.UserModel](ctx, u.coll, bson.M{"user_id": bson.M{"$in": userIDs}})
|
|
}
|
|
|
|
func (u *UserMgo) Take(ctx context.Context, userID string) (user *relation.UserModel, err error) {
|
|
return mgoutil.FindOne[*relation.UserModel](ctx, u.coll, bson.M{"user_id": userID})
|
|
}
|
|
|
|
func (u *UserMgo) TakeNotification(ctx context.Context, level int64) (user []*relation.UserModel, err error) {
|
|
return mgoutil.Find[*relation.UserModel](ctx, u.coll, bson.M{"app_manger_level": level})
|
|
}
|
|
|
|
func (u *UserMgo) TakeByNickname(ctx context.Context, nickname string) (user []*relation.UserModel, err error) {
|
|
return mgoutil.Find[*relation.UserModel](ctx, u.coll, bson.M{"nickname": nickname})
|
|
}
|
|
|
|
func (u *UserMgo) Page(ctx context.Context, pagination pagination.Pagination) (count int64, users []*relation.UserModel, err error) {
|
|
return mgoutil.FindPage[*relation.UserModel](ctx, u.coll, bson.M{}, pagination)
|
|
}
|
|
|
|
func (u *UserMgo) PageFindUser(ctx context.Context, level1 int64, level2 int64, pagination pagination.Pagination) (count int64, users []*relation.UserModel, err error) {
|
|
query := bson.M{
|
|
"$or": []bson.M{
|
|
{"app_manger_level": level1},
|
|
{"app_manger_level": level2},
|
|
},
|
|
}
|
|
|
|
return mgoutil.FindPage[*relation.UserModel](ctx, u.coll, query, pagination)
|
|
}
|
|
func (u *UserMgo) PageFindUserWithKeyword(ctx context.Context, level1 int64, level2 int64, userID string, nickName string, pagination pagination.Pagination) (count int64, users []*relation.UserModel, err error) {
|
|
// Initialize the base query with level conditions
|
|
query := bson.M{
|
|
"$and": []bson.M{
|
|
{"app_manger_level": bson.M{"$in": []int64{level1, level2}}},
|
|
},
|
|
}
|
|
|
|
// Add userID and userName conditions to the query if they are provided
|
|
if userID != "" || nickName != "" {
|
|
userConditions := []bson.M{}
|
|
if userID != "" {
|
|
// Use regex for userID
|
|
regexPattern := primitive.Regex{Pattern: userID, Options: "i"} // 'i' for case-insensitive matching
|
|
userConditions = append(userConditions, bson.M{"user_id": regexPattern})
|
|
}
|
|
if nickName != "" {
|
|
// Use regex for userName
|
|
regexPattern := primitive.Regex{Pattern: nickName, Options: "i"} // 'i' for case-insensitive matching
|
|
userConditions = append(userConditions, bson.M{"nickname": regexPattern})
|
|
}
|
|
query["$and"] = append(query["$and"].([]bson.M), bson.M{"$or": userConditions})
|
|
}
|
|
|
|
// Perform the paginated search
|
|
return mgoutil.FindPage[*relation.UserModel](ctx, u.coll, query, pagination)
|
|
}
|
|
|
|
func (u *UserMgo) GetAllUserID(ctx context.Context, pagination pagination.Pagination) (int64, []string, error) {
|
|
return mgoutil.FindPage[string](ctx, u.coll, bson.M{}, pagination, options.Find().SetProjection(bson.M{"_id": 0, "user_id": 1}))
|
|
}
|
|
|
|
func (u *UserMgo) Exist(ctx context.Context, userID string) (exist bool, err error) {
|
|
return mgoutil.Exist(ctx, u.coll, bson.M{"user_id": userID})
|
|
}
|
|
|
|
func (u *UserMgo) GetUserGlobalRecvMsgOpt(ctx context.Context, userID string) (opt int, err error) {
|
|
return mgoutil.FindOne[int](ctx, u.coll, bson.M{"user_id": userID}, options.FindOne().SetProjection(bson.M{"_id": 0, "global_recv_msg_opt": 1}))
|
|
}
|
|
|
|
func (u *UserMgo) CountTotal(ctx context.Context, before *time.Time) (count int64, err error) {
|
|
if before == nil {
|
|
return mgoutil.Count(ctx, u.coll, bson.M{})
|
|
}
|
|
return mgoutil.Count(ctx, u.coll, bson.M{"create_time": bson.M{"$lt": before}})
|
|
}
|
|
|
|
func (u *UserMgo) AddUserCommand(ctx context.Context, userID string, Type int32, UUID string, value string, ex string) error {
|
|
collection := u.coll.Database().Collection("userCommands")
|
|
|
|
// Create a new document instead of updating an existing one
|
|
doc := bson.M{
|
|
"userID": userID,
|
|
"type": Type,
|
|
"uuid": UUID,
|
|
"createTime": time.Now().Unix(), // assuming you want the creation time in Unix timestamp
|
|
"value": value,
|
|
"ex": ex,
|
|
}
|
|
|
|
_, err := collection.InsertOne(ctx, doc)
|
|
return err
|
|
}
|
|
|
|
func (u *UserMgo) DeleteUserCommand(ctx context.Context, userID string, Type int32, UUID string) error {
|
|
collection := u.coll.Database().Collection("userCommands")
|
|
|
|
filter := bson.M{"userID": userID, "type": Type, "uuid": UUID}
|
|
|
|
result, err := collection.DeleteOne(ctx, filter)
|
|
if result.DeletedCount == 0 {
|
|
// No records found to update
|
|
return errs.Wrap(errs.ErrRecordNotFound)
|
|
}
|
|
return err
|
|
}
|
|
func (u *UserMgo) UpdateUserCommand(ctx context.Context, userID string, Type int32, UUID string, val map[string]any) error {
|
|
if len(val) == 0 {
|
|
return nil
|
|
}
|
|
|
|
collection := u.coll.Database().Collection("userCommands")
|
|
|
|
filter := bson.M{"userID": userID, "type": Type, "uuid": UUID}
|
|
update := bson.M{"$set": val}
|
|
|
|
result, err := collection.UpdateOne(ctx, filter, update)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if result.MatchedCount == 0 {
|
|
// No records found to update
|
|
return errs.Wrap(errs.ErrRecordNotFound)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (u *UserMgo) GetUserCommand(ctx context.Context, userID string, Type int32) ([]*user.CommandInfoResp, error) {
|
|
collection := u.coll.Database().Collection("userCommands")
|
|
filter := bson.M{"userID": userID, "type": Type}
|
|
|
|
cursor, err := collection.Find(ctx, filter)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer cursor.Close(ctx)
|
|
|
|
// Initialize commands as a slice of pointers
|
|
commands := []*user.CommandInfoResp{}
|
|
|
|
for cursor.Next(ctx) {
|
|
var document struct {
|
|
Type int32 `bson:"type"`
|
|
UUID string `bson:"uuid"`
|
|
Value string `bson:"value"`
|
|
CreateTime int64 `bson:"createTime"`
|
|
Ex string `bson:"ex"`
|
|
}
|
|
|
|
if err := cursor.Decode(&document); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
commandInfo := &user.CommandInfoResp{
|
|
Type: document.Type,
|
|
Uuid: document.UUID,
|
|
Value: document.Value,
|
|
CreateTime: document.CreateTime,
|
|
Ex: document.Ex,
|
|
}
|
|
|
|
commands = append(commands, commandInfo)
|
|
}
|
|
|
|
if err := cursor.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return commands, nil
|
|
}
|
|
func (u *UserMgo) GetAllUserCommand(ctx context.Context, userID string) ([]*user.AllCommandInfoResp, error) {
|
|
collection := u.coll.Database().Collection("userCommands")
|
|
filter := bson.M{"userID": userID}
|
|
|
|
cursor, err := collection.Find(ctx, filter)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer cursor.Close(ctx)
|
|
|
|
// Initialize commands as a slice of pointers
|
|
commands := []*user.AllCommandInfoResp{}
|
|
|
|
for cursor.Next(ctx) {
|
|
var document struct {
|
|
Type int32 `bson:"type"`
|
|
UUID string `bson:"uuid"`
|
|
Value string `bson:"value"`
|
|
CreateTime int64 `bson:"createTime"`
|
|
Ex string `bson:"ex"`
|
|
}
|
|
|
|
if err := cursor.Decode(&document); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
commandInfo := &user.AllCommandInfoResp{
|
|
Type: document.Type,
|
|
Uuid: document.UUID,
|
|
Value: document.Value,
|
|
CreateTime: document.CreateTime,
|
|
Ex: document.Ex,
|
|
}
|
|
|
|
commands = append(commands, commandInfo)
|
|
}
|
|
|
|
if err := cursor.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return commands, nil
|
|
}
|
|
func (u *UserMgo) CountRangeEverydayTotal(ctx context.Context, start time.Time, end time.Time) (map[string]int64, error) {
|
|
pipeline := bson.A{
|
|
bson.M{
|
|
"$match": bson.M{
|
|
"create_time": bson.M{
|
|
"$gte": start,
|
|
"$lt": end,
|
|
},
|
|
},
|
|
},
|
|
bson.M{
|
|
"$group": bson.M{
|
|
"_id": bson.M{
|
|
"$dateToString": bson.M{
|
|
"format": "%Y-%m-%d",
|
|
"date": "$create_time",
|
|
},
|
|
},
|
|
"count": bson.M{
|
|
"$sum": 1,
|
|
},
|
|
},
|
|
},
|
|
}
|
|
type Item struct {
|
|
Date string `bson:"_id"`
|
|
Count int64 `bson:"count"`
|
|
}
|
|
items, err := mgoutil.Aggregate[Item](ctx, u.coll, pipeline)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
res := make(map[string]int64, len(items))
|
|
for _, item := range items {
|
|
res[item.Date] = item.Count
|
|
}
|
|
return res, nil
|
|
}
|