Skip to content

Replace -1 with GhostUserID #27703

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion models/issues/comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ func (c *Comment) LoadPoster(ctx context.Context) (err error) {
c.Poster, err = user_model.GetPossibleUserByID(ctx, c.PosterID)
if err != nil {
if user_model.IsErrUserNotExist(err) {
c.PosterID = -1
c.PosterID = user_model.GhostUserID
c.Poster = user_model.NewGhostUser()
} else {
log.Error("getUserByID[%d]: %v", c.ID, err)
Expand Down
2 changes: 1 addition & 1 deletion models/issues/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ func (issue *Issue) LoadPoster(ctx context.Context) (err error) {
if issue.Poster == nil && issue.PosterID != 0 {
issue.Poster, err = user_model.GetPossibleUserByID(ctx, issue.PosterID)
if err != nil {
issue.PosterID = -1
issue.PosterID = user_model.GhostUserID
issue.Poster = user_model.NewGhostUser()
if !user_model.IsErrUserNotExist(err) {
return fmt.Errorf("getUserByID.(poster) [%d]: %w", issue.PosterID, err)
Expand Down
2 changes: 1 addition & 1 deletion models/issues/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ func (pr *PullRequest) LoadAttributes(ctx context.Context) (err error) {
if pr.HasMerged && pr.Merger == nil {
pr.Merger, err = user_model.GetUserByID(ctx, pr.MergerID)
if user_model.IsErrUserNotExist(err) {
pr.MergerID = -1
pr.MergerID = user_model.GhostUserID
pr.Merger = user_model.NewGhostUser()
} else if err != nil {
return fmt.Errorf("getUserByID [%d]: %w", pr.MergerID, err)
Expand Down
3 changes: 1 addition & 2 deletions models/user/avatar.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down
4 changes: 2 additions & 2 deletions models/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down
16 changes: 11 additions & 5 deletions models/user/user_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand All @@ -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),
}
Expand Down
2 changes: 1 addition & 1 deletion routers/web/user/avatar.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func AvatarByUserName(ctx *context.Context) {
size := int(ctx.ParamsInt64(":size"))

var user *user_model.User
if strings.ToLower(userName) != "ghost" {
if strings.ToLower(userName) != user_model.GhostUserLowerName {
var err error
if user, err = user_model.GetUserByName(ctx, userName); err != nil {
if user_model.IsErrUserNotExist(err) {
Expand Down