From 72ef3d1a7f20b3621f1bd8e0648ef509f7808576 Mon Sep 17 00:00:00 2001 From: AndrewZuo01 Date: Tue, 12 Dec 2023 12:21:26 +0800 Subject: [PATCH] update user command --- go.mod | 3 +++ pkg/common/db/controller/user.go | 4 ++-- pkg/common/db/mgo/user.go | 35 +++++++++------------------- pkg/common/db/table/relation/user.go | 3 ++- 4 files changed, 18 insertions(+), 27 deletions(-) diff --git a/go.mod b/go.mod index 19bbc2634..44103819b 100644 --- a/go.mod +++ b/go.mod @@ -154,5 +154,8 @@ require ( golang.org/x/crypto v0.14.0 // indirect gopkg.in/ini.v1 v1.67.0 // indirect ) +replace ( + github.com/OpenIMSDK/protocol v0.0.32 => github.com/AndrewZuo01/protocol v0.0.0-20231212040759-69dcf194366f +) diff --git a/pkg/common/db/controller/user.go b/pkg/common/db/controller/user.go index c4ffd47d5..1cd2ff741 100644 --- a/pkg/common/db/controller/user.go +++ b/pkg/common/db/controller/user.go @@ -73,7 +73,7 @@ type UserDatabase interface { AddUserCommand(ctx context.Context, userID string, Type int32, UUID string, value string) error DeleteUserCommand(ctx context.Context, userID string, Type int32, UUID string) error UpdateUserCommand(ctx context.Context, userID string, Type int32, UUID string, value string) error - GetUserCommands(ctx context.Context, userID string, Type int32) (map[string]string, error) + GetUserCommands(ctx context.Context, userID string, Type int32) (map[string]user.CommandInfo, error) } type userDatabase struct { @@ -242,7 +242,7 @@ func (u *userDatabase) DeleteUserCommand(ctx context.Context, userID string, Typ func (u *userDatabase) UpdateUserCommand(ctx context.Context, userID string, Type int32, UUID string, value string) error { return u.userDB.UpdateUserCommand(ctx, userID, Type, UUID, value) } -func (u *userDatabase) GetUserCommands(ctx context.Context, userID string, Type int32) (map[string]string, error) { +func (u *userDatabase) GetUserCommands(ctx context.Context, userID string, Type int32) (map[string]user.CommandInfo, error) { commands, err := u.userDB.GetUserCommands(ctx, userID, Type) return commands, err } diff --git a/pkg/common/db/mgo/user.go b/pkg/common/db/mgo/user.go index 570d64a81..e410a91d1 100644 --- a/pkg/common/db/mgo/user.go +++ b/pkg/common/db/mgo/user.go @@ -2,6 +2,7 @@ package mgo import ( "context" + "github.com/OpenIMSDK/protocol/user" "time" "github.com/OpenIMSDK/tools/mgoutil" @@ -74,24 +75,22 @@ func (u *UserMgo) CountTotal(ctx context.Context, before *time.Time) (count int6 } type UserCommand struct { - UserID string `bson:"userID"` - Type int32 `bson:"type"` - Commands map[string]string `bson:"commands"` + 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 { collection := u.coll.Database().Collection("userCommands") filter := bson.M{"userID": userID, "type": Type} - update := bson.M{"$set": bson.M{"commands." + UUID: value}} + update := bson.M{"$set": bson.M{"commands." + UUID: user.CommandInfo{Time: time.Now().Unix(), Value: value}}} options := options.Update().SetUpsert(true) _, err := collection.UpdateOne(ctx, filter, update, options) - if err != nil { - return err - } - return nil + return err } + func (u *UserMgo) DeleteUserCommand(ctx context.Context, userID string, Type int32, UUID string) error { collection := u.coll.Database().Collection("userCommands") @@ -99,30 +98,18 @@ func (u *UserMgo) DeleteUserCommand(ctx context.Context, userID string, Type int update := bson.M{"$unset": bson.M{"commands." + UUID: ""}} _, err := collection.UpdateOne(ctx, filter, update) - if err != nil { - return err - } - return nil + return err } func (u *UserMgo) UpdateUserCommand(ctx context.Context, userID string, Type int32, UUID string, value string) error { collection := u.coll.Database().Collection("userCommands") - // Create a filter to identify the document filter := bson.M{"userID": userID, "type": Type} + update := bson.M{"$set": bson.M{"commands." + UUID: user.CommandInfo{Time: time.Now().Unix(), Value: value}}} - // Create an update statement to set the new value for the command - update := bson.M{"$set": bson.M{"commands." + UUID: value}} - - // Perform the update operation _, err := collection.UpdateOne(ctx, filter, update) - if err != nil { - return err - } - - return nil + return err } - -func (u *UserMgo) GetUserCommands(ctx context.Context, userID string, Type int32) (map[string]string, error) { +func (u *UserMgo) GetUserCommands(ctx context.Context, userID string, Type int32) (map[string]user.CommandInfo, error) { collection := u.coll.Database().Collection("userCommands") filter := bson.M{"userID": userID, "type": Type} diff --git a/pkg/common/db/table/relation/user.go b/pkg/common/db/table/relation/user.go index e39cb442e..256b00f8d 100644 --- a/pkg/common/db/table/relation/user.go +++ b/pkg/common/db/table/relation/user.go @@ -16,6 +16,7 @@ package relation import ( "context" + "github.com/OpenIMSDK/protocol/user" "time" "github.com/OpenIMSDK/tools/pagination" @@ -64,5 +65,5 @@ type UserModelInterface interface { AddUserCommand(ctx context.Context, userID string, Type int32, UUID string, value string) error DeleteUserCommand(ctx context.Context, userID string, Type int32, UUID string) error UpdateUserCommand(ctx context.Context, userID string, Type int32, UUID string, value string) error - GetUserCommands(ctx context.Context, userID string, Type int32) (map[string]string, error) + GetUserCommands(ctx context.Context, userID string, Type int32) (map[string]user.CommandInfo, error) }