Skip to content

Commit

Permalink
Merge pull request #30 from ShanghaitechGeekPie/reward-fix
Browse files Browse the repository at this point in the history
Fix setting reward for comments and displaying reward in profile
  • Loading branch information
WAAutoMaton authored May 21, 2024
2 parents fef01e4 + ab3e905 commit ea1ae15
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
2 changes: 1 addition & 1 deletion internal/controllers/reward/setcomment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
33 changes: 23 additions & 10 deletions pkg/queries/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,29 +347,42 @@ 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
}

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 {
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
Expand Down

0 comments on commit ea1ae15

Please sign in to comment.