Skip to content

Commit 881806a

Browse files
authored
Replace -1 with GhostUserID (#27703)
1 parent eb14787 commit 881806a

File tree

7 files changed

+18
-13
lines changed

7 files changed

+18
-13
lines changed

models/issues/comment.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ func (c *Comment) LoadPoster(ctx context.Context) (err error) {
349349
c.Poster, err = user_model.GetPossibleUserByID(ctx, c.PosterID)
350350
if err != nil {
351351
if user_model.IsErrUserNotExist(err) {
352-
c.PosterID = -1
352+
c.PosterID = user_model.GhostUserID
353353
c.Poster = user_model.NewGhostUser()
354354
} else {
355355
log.Error("getUserByID[%d]: %v", c.ID, err)

models/issues/issue.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ func (issue *Issue) LoadPoster(ctx context.Context) (err error) {
219219
if issue.Poster == nil && issue.PosterID != 0 {
220220
issue.Poster, err = user_model.GetPossibleUserByID(ctx, issue.PosterID)
221221
if err != nil {
222-
issue.PosterID = -1
222+
issue.PosterID = user_model.GhostUserID
223223
issue.Poster = user_model.NewGhostUser()
224224
if !user_model.IsErrUserNotExist(err) {
225225
return fmt.Errorf("getUserByID.(poster) [%d]: %w", issue.PosterID, err)

models/issues/pull.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ func (pr *PullRequest) LoadAttributes(ctx context.Context) (err error) {
272272
if pr.HasMerged && pr.Merger == nil {
273273
pr.Merger, err = user_model.GetUserByID(ctx, pr.MergerID)
274274
if user_model.IsErrUserNotExist(err) {
275-
pr.MergerID = -1
275+
pr.MergerID = user_model.GhostUserID
276276
pr.Merger = user_model.NewGhostUser()
277277
} else if err != nil {
278278
return fmt.Errorf("getUserByID [%d]: %w", pr.MergerID, err)

models/user/avatar.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ func GenerateRandomAvatar(ctx context.Context, u *User) error {
5858

5959
// AvatarLinkWithSize returns a link to the user's avatar with size. size <= 0 means default size
6060
func (u *User) AvatarLinkWithSize(ctx context.Context, size int) string {
61-
if u.ID == -1 {
62-
// ghost user
61+
if u.IsGhost() {
6362
return avatars.DefaultAvatarLink()
6463
}
6564

models/user/user.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -933,7 +933,7 @@ func GetUserByIDs(ctx context.Context, ids []int64) ([]*User, error) {
933933
// GetPossibleUserByID returns the user if id > 0 or return system usrs if id < 0
934934
func GetPossibleUserByID(ctx context.Context, id int64) (*User, error) {
935935
switch id {
936-
case -1:
936+
case GhostUserID:
937937
return NewGhostUser(), nil
938938
case ActionsUserID:
939939
return NewActionsUser(), nil
@@ -949,7 +949,7 @@ func GetPossibleUserByIDs(ctx context.Context, ids []int64) ([]*User, error) {
949949
uniqueIDs := container.SetOf(ids...)
950950
users := make([]*User, 0, len(ids))
951951
_ = uniqueIDs.Remove(0)
952-
if uniqueIDs.Remove(-1) {
952+
if uniqueIDs.Remove(GhostUserID) {
953953
users = append(users, NewGhostUser())
954954
}
955955
if uniqueIDs.Remove(ActionsUserID) {

models/user/user_system.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,18 @@ import (
99
"code.gitea.io/gitea/modules/structs"
1010
)
1111

12+
const (
13+
GhostUserID = -1
14+
GhostUserName = "Ghost"
15+
GhostUserLowerName = "ghost"
16+
)
17+
1218
// NewGhostUser creates and returns a fake user for someone has deleted their account.
1319
func NewGhostUser() *User {
1420
return &User{
15-
ID: -1,
16-
Name: "Ghost",
17-
LowerName: "ghost",
21+
ID: GhostUserID,
22+
Name: GhostUserName,
23+
LowerName: GhostUserLowerName,
1824
}
1925
}
2026

@@ -23,13 +29,13 @@ func (u *User) IsGhost() bool {
2329
if u == nil {
2430
return false
2531
}
26-
return u.ID == -1 && u.Name == "Ghost"
32+
return u.ID == GhostUserID && u.Name == GhostUserName
2733
}
2834

2935
// NewReplaceUser creates and returns a fake user for external user
3036
func NewReplaceUser(name string) *User {
3137
return &User{
32-
ID: -1,
38+
ID: 0,
3339
Name: name,
3440
LowerName: strings.ToLower(name),
3541
}

routers/web/user/avatar.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func AvatarByUserName(ctx *context.Context) {
2727
size := int(ctx.ParamsInt64(":size"))
2828

2929
var user *user_model.User
30-
if strings.ToLower(userName) != "ghost" {
30+
if strings.ToLower(userName) != user_model.GhostUserLowerName {
3131
var err error
3232
if user, err = user_model.GetUserByName(ctx, userName); err != nil {
3333
if user_model.IsErrUserNotExist(err) {

0 commit comments

Comments
 (0)