aboutsummaryrefslogtreecommitdiffstats
path: root/models/user
diff options
context:
space:
mode:
authorNanguan Lin <70063547+lng2020@users.noreply.github.com>2023-10-20 22:43:08 +0800
committerGitHub <noreply@github.com>2023-10-20 14:43:08 +0000
commit881806a50ba4e3bb761245b2b590046eb1423b95 (patch)
treeef427c4ee00438259c75b42e347fd8729be49a8a /models/user
parenteb1478791f2bdf77f54853e25878e3c5371a80d3 (diff)
downloadgitea-881806a50ba4e3bb761245b2b590046eb1423b95.tar.gz
gitea-881806a50ba4e3bb761245b2b590046eb1423b95.zip
Replace -1 with GhostUserID (#27703)
Diffstat (limited to 'models/user')
-rw-r--r--models/user/avatar.go3
-rw-r--r--models/user/user.go4
-rw-r--r--models/user/user_system.go16
3 files changed, 14 insertions, 9 deletions
diff --git a/models/user/avatar.go b/models/user/avatar.go
index 8c9abecbea..c6937d7b51 100644
--- a/models/user/avatar.go
+++ b/models/user/avatar.go
@@ -58,8 +58,7 @@ func GenerateRandomAvatar(ctx context.Context, u *User) error {
// AvatarLinkWithSize returns a link to the user's avatar with size. size <= 0 means default size
func (u *User) AvatarLinkWithSize(ctx context.Context, size int) string {
- if u.ID == -1 {
- // ghost user
+ if u.IsGhost() {
return avatars.DefaultAvatarLink()
}
diff --git a/models/user/user.go b/models/user/user.go
index 60aa6b9a6f..ce0e055b15 100644
--- a/models/user/user.go
+++ b/models/user/user.go
@@ -933,7 +933,7 @@ func GetUserByIDs(ctx context.Context, ids []int64) ([]*User, error) {
// GetPossibleUserByID returns the user if id > 0 or return system usrs if id < 0
func GetPossibleUserByID(ctx context.Context, id int64) (*User, error) {
switch id {
- case -1:
+ case GhostUserID:
return NewGhostUser(), nil
case ActionsUserID:
return NewActionsUser(), nil
@@ -949,7 +949,7 @@ func GetPossibleUserByIDs(ctx context.Context, ids []int64) ([]*User, error) {
uniqueIDs := container.SetOf(ids...)
users := make([]*User, 0, len(ids))
_ = uniqueIDs.Remove(0)
- if uniqueIDs.Remove(-1) {
+ if uniqueIDs.Remove(GhostUserID) {
users = append(users, NewGhostUser())
}
if uniqueIDs.Remove(ActionsUserID) {
diff --git a/models/user/user_system.go b/models/user/user_system.go
index f54f4e3ffb..612cdb2cae 100644
--- a/models/user/user_system.go
+++ b/models/user/user_system.go
@@ -9,12 +9,18 @@ import (
"code.gitea.io/gitea/modules/structs"
)
+const (
+ GhostUserID = -1
+ GhostUserName = "Ghost"
+ GhostUserLowerName = "ghost"
+)
+
// NewGhostUser creates and returns a fake user for someone has deleted their account.
func NewGhostUser() *User {
return &User{
- ID: -1,
- Name: "Ghost",
- LowerName: "ghost",
+ ID: GhostUserID,
+ Name: GhostUserName,
+ LowerName: GhostUserLowerName,
}
}
@@ -23,13 +29,13 @@ func (u *User) IsGhost() bool {
if u == nil {
return false
}
- return u.ID == -1 && u.Name == "Ghost"
+ return u.ID == GhostUserID && u.Name == GhostUserName
}
// NewReplaceUser creates and returns a fake user for external user
func NewReplaceUser(name string) *User {
return &User{
- ID: -1,
+ ID: 0,
Name: name,
LowerName: strings.ToLower(name),
}