mirror of
https://github.com/openimsdk/open-im-server.git
synced 2025-12-03 10:52:33 +08:00
update user command
This commit is contained in:
parent
cc4122dde4
commit
80b3ae875e
@ -3,7 +3,6 @@ package mgo
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"github.com/OpenIMSDK/protocol/user"
|
"github.com/OpenIMSDK/protocol/user"
|
||||||
log2 "github.com/OpenIMSDK/tools/log"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/OpenIMSDK/tools/mgoutil"
|
"github.com/OpenIMSDK/tools/mgoutil"
|
||||||
@ -75,37 +74,34 @@ func (u *UserMgo) CountTotal(ctx context.Context, before *time.Time) (count int6
|
|||||||
return mgoutil.Count(ctx, u.coll, bson.M{"create_time": bson.M{"$lt": before}})
|
return mgoutil.Count(ctx, u.coll, bson.M{"create_time": bson.M{"$lt": before}})
|
||||||
}
|
}
|
||||||
|
|
||||||
type UserCommand struct {
|
|
||||||
UserID string `bson:"userID"`
|
|
||||||
Type int32 `bson:"type"`
|
|
||||||
Commands map[string]user.CommandInfo `bson:"commands"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (u *UserMgo) AddUserCommand(ctx context.Context, userID string, Type int32, UUID string, value string) error {
|
func (u *UserMgo) AddUserCommand(ctx context.Context, userID string, Type int32, UUID string, value string) error {
|
||||||
collection := u.coll.Database().Collection("userCommands")
|
collection := u.coll.Database().Collection("userCommands")
|
||||||
|
|
||||||
filter := bson.M{"userID": userID, "type": Type}
|
// Create a new document instead of updating an existing one
|
||||||
update := bson.M{"$set": bson.M{"commands." + UUID: user.CommandInfo{Time: time.Now().Unix(), Value: value}}}
|
doc := bson.M{
|
||||||
options := options.Update().SetUpsert(true)
|
"userID": userID,
|
||||||
|
"type": Type,
|
||||||
|
"uuid": UUID,
|
||||||
|
"createTime": time.Now().Unix(), // assuming you want the creation time in Unix timestamp
|
||||||
|
"value": value,
|
||||||
|
}
|
||||||
|
|
||||||
_, err := collection.UpdateOne(ctx, filter, update, options)
|
_, err := collection.InsertOne(ctx, doc)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *UserMgo) DeleteUserCommand(ctx context.Context, userID string, Type int32, UUID string) error {
|
func (u *UserMgo) DeleteUserCommand(ctx context.Context, userID string, Type int32, UUID string) error {
|
||||||
collection := u.coll.Database().Collection("userCommands")
|
collection := u.coll.Database().Collection("userCommands")
|
||||||
|
|
||||||
filter := bson.M{"userID": userID, "type": Type}
|
filter := bson.M{"userID": userID, "type": Type, "uuid": UUID}
|
||||||
update := bson.M{"$unset": bson.M{"commands." + UUID: ""}}
|
|
||||||
|
|
||||||
_, err := collection.UpdateOne(ctx, filter, update)
|
_, err := collection.DeleteOne(ctx, filter)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
func (u *UserMgo) UpdateUserCommand(ctx context.Context, userID string, Type int32, UUID string, value string) error {
|
func (u *UserMgo) UpdateUserCommand(ctx context.Context, userID string, Type int32, UUID string, value string) error {
|
||||||
collection := u.coll.Database().Collection("userCommands")
|
collection := u.coll.Database().Collection("userCommands")
|
||||||
|
|
||||||
filter := bson.M{"userID": userID, "type": Type}
|
filter := bson.M{"userID": userID, "type": Type, "uuid": UUID}
|
||||||
update := bson.M{"$set": bson.M{"commands." + UUID: user.CommandInfo{Time: time.Now().Unix(), Value: value}}}
|
update := bson.M{"$set": bson.M{"value": value, "createTime": time.Now().Unix()}}
|
||||||
|
|
||||||
_, err := collection.UpdateOne(ctx, filter, update)
|
_, err := collection.UpdateOne(ctx, filter, update)
|
||||||
return err
|
return err
|
||||||
@ -123,24 +119,21 @@ func (u *UserMgo) GetUserCommands(ctx context.Context, userID string, Type int32
|
|||||||
var commands []user.CommandInfoResp
|
var commands []user.CommandInfoResp
|
||||||
|
|
||||||
for cursor.Next(ctx) {
|
for cursor.Next(ctx) {
|
||||||
|
|
||||||
var commandInfo user.CommandInfoResp
|
|
||||||
|
|
||||||
// Define a struct that represents your MongoDB document structure
|
|
||||||
var document struct {
|
var document struct {
|
||||||
UUID string `bson:"uuid"`
|
UUID string `bson:"uuid"`
|
||||||
Value string `bson:"value"`
|
Value string `bson:"value"`
|
||||||
CreateTime int64 `bson:"time"`
|
CreateTime int64 `bson:"createTime"`
|
||||||
}
|
}
|
||||||
log2.ZDebug(ctx, "cursor", cursor)
|
|
||||||
if err := cursor.Decode(&document); err != nil {
|
if err := cursor.Decode(&document); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
log2.ZDebug(ctx, "document", document)
|
|
||||||
// Populate the user.CommandInfoResp struct with the required fields
|
commandInfo := user.CommandInfoResp{
|
||||||
commandInfo.Uuid = document.UUID
|
Uuid: document.UUID,
|
||||||
commandInfo.Value = document.Value
|
Value: document.Value,
|
||||||
commandInfo.CreateTime = document.CreateTime
|
CreateTime: document.CreateTime,
|
||||||
|
}
|
||||||
|
|
||||||
commands = append(commands, commandInfo)
|
commands = append(commands, commandInfo)
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user