From 4f2800052f9cb91ab6e6322bfd3896ce9d183c70 Mon Sep 17 00:00:00 2001 From: AndrewZuo01 Date: Fri, 5 Jan 2024 10:25:35 +0800 Subject: [PATCH] update user command get all notification --- go.mod | 4 +++ internal/api/route.go | 1 + internal/api/user.go | 5 ++++ internal/rpc/user/user.go | 25 +++++++++++++++++- pkg/common/db/controller/user.go | 5 ++++ pkg/common/db/mgo/user.go | 38 ++++++++++++++++++++++++++++ pkg/common/db/table/relation/user.go | 1 + pkg/rpcclient/notification/user.go | 6 ++--- 8 files changed, 81 insertions(+), 4 deletions(-) diff --git a/go.mod b/go.mod index 393e742de..9bcfb2c70 100644 --- a/go.mod +++ b/go.mod @@ -155,4 +155,8 @@ require ( go.uber.org/zap v1.24.0 // indirect golang.org/x/crypto v0.14.0 // indirect gopkg.in/ini.v1 v1.67.0 // indirect +) +replace ( + github.com/OpenIMSDK/protocol v0.0.42 => github.com/AndrewZuo01/protocol v0.0.0-20240105021040-57f7d4a07674 + ) \ No newline at end of file diff --git a/internal/api/route.go b/internal/api/route.go index 1c91f4dde..8729bc1b9 100644 --- a/internal/api/route.go +++ b/internal/api/route.go @@ -83,6 +83,7 @@ func NewGinRouter(discov discoveryregistry.SvcDiscoveryRegistry, rdb redis.Unive userRouterGroup.POST("/process_user_command_delete", ParseToken, u.ProcessUserCommandDelete) userRouterGroup.POST("/process_user_command_update", ParseToken, u.ProcessUserCommandUpdate) userRouterGroup.POST("/process_user_command_get", ParseToken, u.ProcessUserCommandGet) + userRouterGroup.POST("/process_user_command_get_all", ParseToken, u.ProcessUserCommandGetAll) userRouterGroup.POST("/add_notification_account", ParseToken, u.AddNotificationAccount) userRouterGroup.POST("/update_notification_account", ParseToken, u.UpdateNotificationAccountInfo) diff --git a/internal/api/user.go b/internal/api/user.go index 5f0e23631..03d22c354 100644 --- a/internal/api/user.go +++ b/internal/api/user.go @@ -221,6 +221,11 @@ func (u *UserApi) ProcessUserCommandGet(c *gin.Context) { a2r.Call(user.UserClient.ProcessUserCommandGet, u.Client, c) } +// ProcessUserCommandGet user general function get all +func (u *UserApi) ProcessUserCommandGetAll(c *gin.Context) { + a2r.Call(user.UserClient.ProcessUserCommandGetAll, u.Client, c) +} + func (u *UserApi) AddNotificationAccount(c *gin.Context) { a2r.Call(user.UserClient.AddNotificationAccount, u.Client, c) } diff --git a/internal/rpc/user/user.go b/internal/rpc/user/user.go index d9d36f5d0..c34b9f0a1 100644 --- a/internal/rpc/user/user.go +++ b/internal/rpc/user/user.go @@ -442,7 +442,30 @@ func (s *userServer) ProcessUserCommandGet(ctx context.Context, req *pbuser.Proc } // Return the response with the slice - return &pbuser.ProcessUserCommandGetResp{KVArray: commandInfoSlice}, nil + return &pbuser.ProcessUserCommandGetResp{CommandResp: commandInfoSlice}, nil +} + +func (s *userServer) ProcessUserCommandGetAll(ctx context.Context, req *pbuser.ProcessUserCommandGetAllReq) (*pbuser.ProcessUserCommandGetAllResp, error) { + // Fetch user commands from the database + commands, err := s.UserDatabase.GetAllUserCommands(ctx, req.UserID) + if err != nil { + return nil, err + } + + // Initialize commandInfoSlice as an empty slice + commandInfoSlice := make([]*pbuser.AllCommandInfoResp, 0, len(commands)) + + for _, command := range commands { + // No need to use index since command is already a pointer + commandInfoSlice = append(commandInfoSlice, &pbuser.AllCommandInfoResp{ + Uuid: command.Uuid, + Value: command.Value, + CreateTime: command.CreateTime, + }) + } + + // Return the response with the slice + return &pbuser.ProcessUserCommandGetAllResp{CommandResp: commandInfoSlice}, nil } func (s *userServer) AddNotificationAccount(ctx context.Context, req *pbuser.AddNotificationAccountReq) (*pbuser.AddNotificationAccountResp, error) { diff --git a/pkg/common/db/controller/user.go b/pkg/common/db/controller/user.go index 72bdf6b06..a8955ca99 100644 --- a/pkg/common/db/controller/user.go +++ b/pkg/common/db/controller/user.go @@ -78,6 +78,7 @@ type UserDatabase interface { 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) ([]*user.CommandInfoResp, error) + GetAllUserCommands(ctx context.Context, userID string) ([]*user.AllCommandInfoResp, error) } type userDatabase struct { @@ -259,3 +260,7 @@ func (u *userDatabase) GetUserCommands(ctx context.Context, userID string, Type commands, err := u.userDB.GetUserCommand(ctx, userID, Type) return commands, err } +func (u *userDatabase) GetAllUserCommands(ctx context.Context, userID string) ([]*user.AllCommandInfoResp, error) { + commands, err := u.userDB.GetAllUserCommand(ctx, userID) + return commands, err +} diff --git a/pkg/common/db/mgo/user.go b/pkg/common/db/mgo/user.go index 0ca711ad8..c011980c1 100644 --- a/pkg/common/db/mgo/user.go +++ b/pkg/common/db/mgo/user.go @@ -163,7 +163,45 @@ func (u *UserMgo) GetUserCommand(ctx context.Context, userID string, Type int32) 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 { + UUID string `bson:"uuid"` + Value string `bson:"value"` + CreateTime int64 `bson:"createTime"` + } + + if err := cursor.Decode(&document); err != nil { + return nil, err + } + commandInfo := &user.AllCommandInfoResp{ // Change here: use a pointer to the struct + Uuid: document.UUID, + Value: document.Value, + CreateTime: document.CreateTime, + } + + 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{ diff --git a/pkg/common/db/table/relation/user.go b/pkg/common/db/table/relation/user.go index 8917ba55f..1c0a68c16 100644 --- a/pkg/common/db/table/relation/user.go +++ b/pkg/common/db/table/relation/user.go @@ -67,4 +67,5 @@ type UserModelInterface interface { DeleteUserCommand(ctx context.Context, userID string, Type int32, UUID string) error UpdateUserCommand(ctx context.Context, userID string, Type int32, UUID string, value string) error GetUserCommand(ctx context.Context, userID string, Type int32) ([]*user.CommandInfoResp, error) + GetAllUserCommand(ctx context.Context, userID string) ([]*user.AllCommandInfoResp, error) } diff --git a/pkg/rpcclient/notification/user.go b/pkg/rpcclient/notification/user.go index eb06566a4..4347faece 100644 --- a/pkg/rpcclient/notification/user.go +++ b/pkg/rpcclient/notification/user.go @@ -107,17 +107,17 @@ func (u *UserNotificationSender) UserCommandUpdateNotification( ctx context.Context, tips *sdkws.UserCommandUpdateTips, ) error { - return u.Notification(ctx, tips.FromUserID, tips.ToUserID, constant.UserStatusChangeNotification, tips) + return u.Notification(ctx, tips.FromUserID, tips.ToUserID, constant.UserCommandUpdateNotification, tips) } func (u *UserNotificationSender) UserCommandAddNotification( ctx context.Context, tips *sdkws.UserCommandAddTips, ) error { - return u.Notification(ctx, tips.FromUserID, tips.ToUserID, constant.UserStatusChangeNotification, tips) + return u.Notification(ctx, tips.FromUserID, tips.ToUserID, constant.UserCommandAddNotification, tips) } func (u *UserNotificationSender) UserCommandDeleteNotification( ctx context.Context, tips *sdkws.UserCommandDeleteTips, ) error { - return u.Notification(ctx, tips.FromUserID, tips.ToUserID, constant.UserStatusChangeNotification, tips) + return u.Notification(ctx, tips.FromUserID, tips.ToUserID, constant.UserCommandDeleteNotification, tips) }