From 91abb180889db54020d27ea735dfb93676579a5f Mon Sep 17 00:00:00 2001 From: Hengyu Ai Date: Sun, 19 May 2024 20:38:22 +0800 Subject: [PATCH 1/6] fix(controllers/reward.SetComment): preload User before setting reward --- internal/controllers/reward/setcomment.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/controllers/reward/setcomment.go b/internal/controllers/reward/setcomment.go index 729f2aa..0616e6d 100644 --- a/internal/controllers/reward/setcomment.go +++ b/internal/controllers/reward/setcomment.go @@ -38,7 +38,7 @@ func SetComment(c *fiber.Ctx) error { } comment := &models.Comment{} - result := db.First(&comment, request.ID) + result := db.Preload("User").First(&comment, request.ID) if result.Error != nil { return errors.Wrap(result.Error, errors.DatabaseError) } From a04a3f701a601eb340b761f59141744f1d09f831 Mon Sep 17 00:00:00 2001 From: Hengyu Ai Date: Sun, 19 May 2024 21:11:46 +0800 Subject: [PATCH 2/6] fix(queries.GetProfile): display rewards for admins --- pkg/queries/user.go | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/pkg/queries/user.go b/pkg/queries/user.go index a4bdc3e..f941450 100644 --- a/pkg/queries/user.go +++ b/pkg/queries/user.go @@ -347,29 +347,43 @@ func GetUserByID(db *gorm.DB, id uint) (*models.User, error) { return user, nil } -// id: 被查询用户的id -// uid: 查询用户的id -// ip: 查询用户的ip -func GetProfile(db *gorm.DB, id uint, uid uint) (models.ProfileResponse, error) { +func GetProfile(db *gorm.DB, queriedUserID uint, queryingUserID uint) (models.ProfileResponse, error) { if db == nil { db = database.GetDB() } - user, err := GetUserByID(db, id) + + user, err := GetUserByID(db, queriedUserID) if err != nil { return models.ProfileResponse{}, err } + + // As written in the previous commit, administrators do not + // have access to the invitation code of users other than themselves. + var displayInvitationCode bool = queryingUserID == queriedUserID + var displayReward bool = queryingUserID == queriedUserID + + if queryingUserID != 0 && queryingUserID != queriedUserID { + queryingUser, err := GetUserByID(db, queryingUserID) + if err != nil { + return models.ProfileResponse{}, err + } + if queryingUser.IsAdmin || queryingUser.IsCommunityAdmin { + displayReward = true + } + } + avatar := "" if user.Avatar != "" { avatar = fmt.Sprintf("https://%s/%s/avatar/%s", database.GetEndpoint(), database.MinioConf.Bucket, user.Avatar) } - if user.IsAnonymous && id != uid { - return models.ProfileResponse{ID: id, NickName: user.NickName, Avatar: avatar, IsAnonymous: user.IsAnonymous, IsAdmin: user.IsAdmin, IsCommunityAdmin: user.IsCommunityAdmin}, nil + if user.IsAnonymous && queryingUserID != queriedUserID { + return models.ProfileResponse{ID: user.ID, NickName: user.NickName, Avatar: avatar, IsAnonymous: user.IsAnonymous, IsAdmin: user.IsAdmin, IsCommunityAdmin: user.IsCommunityAdmin}, nil } else { - r := models.ProfileResponse{ID: id, Email: user.Email, Year: user.Year, Grade: user.Grade, NickName: user.NickName, RealName: user.RealName, IsAnonymous: user.IsAnonymous, Avatar: avatar, IsAdmin: user.IsAdmin, IsCommunityAdmin: user.IsCommunityAdmin} - if id == uid { + r := models.ProfileResponse{ID: user.ID, Email: user.Email, Year: user.Year, Grade: user.Grade, NickName: user.NickName, RealName: user.RealName, IsAnonymous: user.IsAnonymous, Avatar: avatar, IsAdmin: user.IsAdmin, IsCommunityAdmin: user.IsCommunityAdmin} + if displayInvitationCode { r.InvitationCode = user.InvitationCode } - if id == uid || user.IsAdmin || user.IsCommunityAdmin { + if displayReward { r.Reward = user.Reward } else { r.Reward = -1 From a17223efcc315e15507d5c9baccf4194b5395382 Mon Sep 17 00:00:00 2001 From: Sizhe Zhao Date: Mon, 20 May 2024 11:06:00 +0800 Subject: [PATCH 3/6] style(queries.GetProfile): fix style --- pkg/queries/user.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/queries/user.go b/pkg/queries/user.go index f941450..deac4fe 100644 --- a/pkg/queries/user.go +++ b/pkg/queries/user.go @@ -359,7 +359,7 @@ func GetProfile(db *gorm.DB, queriedUserID uint, queryingUserID uint) (models.Pr // As written in the previous commit, administrators do not // have access to the invitation code of users other than themselves. - var displayInvitationCode bool = queryingUserID == queriedUserID + displayInvitationCode := queryingUserID == queriedUserID var displayReward bool = queryingUserID == queriedUserID if queryingUserID != 0 && queryingUserID != queriedUserID { From f9e4c250a4944e6115aa06e98ee9d93ac8d76005 Mon Sep 17 00:00:00 2001 From: Sizhe Zhao Date: Mon, 20 May 2024 11:06:12 +0800 Subject: [PATCH 4/6] style(queries.GetProfile): fix style --- pkg/queries/user.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/queries/user.go b/pkg/queries/user.go index deac4fe..d44b14c 100644 --- a/pkg/queries/user.go +++ b/pkg/queries/user.go @@ -360,7 +360,7 @@ func GetProfile(db *gorm.DB, queriedUserID uint, queryingUserID uint) (models.Pr // As written in the previous commit, administrators do not // have access to the invitation code of users other than themselves. displayInvitationCode := queryingUserID == queriedUserID - var displayReward bool = queryingUserID == queriedUserID + displayReward := queryingUserID == queriedUserID if queryingUserID != 0 && queryingUserID != queriedUserID { queryingUser, err := GetUserByID(db, queryingUserID) From 89c002350c937a0da9d1c764d889f9c89508c72a Mon Sep 17 00:00:00 2001 From: Sizhe Zhao Date: Mon, 20 May 2024 11:09:49 +0800 Subject: [PATCH 5/6] docs(queries.GetProfile): rm ref to old commits --- pkg/queries/user.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/pkg/queries/user.go b/pkg/queries/user.go index d44b14c..2358b19 100644 --- a/pkg/queries/user.go +++ b/pkg/queries/user.go @@ -357,8 +357,6 @@ func GetProfile(db *gorm.DB, queriedUserID uint, queryingUserID uint) (models.Pr return models.ProfileResponse{}, err } - // As written in the previous commit, administrators do not - // have access to the invitation code of users other than themselves. displayInvitationCode := queryingUserID == queriedUserID displayReward := queryingUserID == queriedUserID From ab3e9052ff8a30e5fa15264862d4eae5a41ab3fc Mon Sep 17 00:00:00 2001 From: Hengyu Ai <47201556+AstatineAi@users.noreply.github.com> Date: Mon, 20 May 2024 19:49:40 +0800 Subject: [PATCH 6/6] docs(queries.GetProfile): add comment on query condition Co-authored-by: Sizhe Zhao --- pkg/queries/user.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/queries/user.go b/pkg/queries/user.go index 2358b19..74c1672 100644 --- a/pkg/queries/user.go +++ b/pkg/queries/user.go @@ -360,6 +360,7 @@ func GetProfile(db *gorm.DB, queriedUserID uint, queryingUserID uint) (models.Pr displayInvitationCode := queryingUserID == queriedUserID displayReward := queryingUserID == queriedUserID + // don't query if not logged in if queryingUserID != 0 && queryingUserID != queriedUserID { queryingUser, err := GetUserByID(db, queryingUserID) if err != nil {