Replace -1 with GhostUserID (#27703)
This commit is contained in:
parent
eb1478791f
commit
881806a50b
7 changed files with 18 additions and 13 deletions
|
@ -349,7 +349,7 @@ func (c *Comment) LoadPoster(ctx context.Context) (err error) {
|
||||||
c.Poster, err = user_model.GetPossibleUserByID(ctx, c.PosterID)
|
c.Poster, err = user_model.GetPossibleUserByID(ctx, c.PosterID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if user_model.IsErrUserNotExist(err) {
|
if user_model.IsErrUserNotExist(err) {
|
||||||
c.PosterID = -1
|
c.PosterID = user_model.GhostUserID
|
||||||
c.Poster = user_model.NewGhostUser()
|
c.Poster = user_model.NewGhostUser()
|
||||||
} else {
|
} else {
|
||||||
log.Error("getUserByID[%d]: %v", c.ID, err)
|
log.Error("getUserByID[%d]: %v", c.ID, err)
|
||||||
|
|
|
@ -219,7 +219,7 @@ func (issue *Issue) LoadPoster(ctx context.Context) (err error) {
|
||||||
if issue.Poster == nil && issue.PosterID != 0 {
|
if issue.Poster == nil && issue.PosterID != 0 {
|
||||||
issue.Poster, err = user_model.GetPossibleUserByID(ctx, issue.PosterID)
|
issue.Poster, err = user_model.GetPossibleUserByID(ctx, issue.PosterID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
issue.PosterID = -1
|
issue.PosterID = user_model.GhostUserID
|
||||||
issue.Poster = user_model.NewGhostUser()
|
issue.Poster = user_model.NewGhostUser()
|
||||||
if !user_model.IsErrUserNotExist(err) {
|
if !user_model.IsErrUserNotExist(err) {
|
||||||
return fmt.Errorf("getUserByID.(poster) [%d]: %w", issue.PosterID, err)
|
return fmt.Errorf("getUserByID.(poster) [%d]: %w", issue.PosterID, err)
|
||||||
|
|
|
@ -272,7 +272,7 @@ func (pr *PullRequest) LoadAttributes(ctx context.Context) (err error) {
|
||||||
if pr.HasMerged && pr.Merger == nil {
|
if pr.HasMerged && pr.Merger == nil {
|
||||||
pr.Merger, err = user_model.GetUserByID(ctx, pr.MergerID)
|
pr.Merger, err = user_model.GetUserByID(ctx, pr.MergerID)
|
||||||
if user_model.IsErrUserNotExist(err) {
|
if user_model.IsErrUserNotExist(err) {
|
||||||
pr.MergerID = -1
|
pr.MergerID = user_model.GhostUserID
|
||||||
pr.Merger = user_model.NewGhostUser()
|
pr.Merger = user_model.NewGhostUser()
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
return fmt.Errorf("getUserByID [%d]: %w", pr.MergerID, err)
|
return fmt.Errorf("getUserByID [%d]: %w", pr.MergerID, err)
|
||||||
|
|
|
@ -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
|
// 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 {
|
func (u *User) AvatarLinkWithSize(ctx context.Context, size int) string {
|
||||||
if u.ID == -1 {
|
if u.IsGhost() {
|
||||||
// ghost user
|
|
||||||
return avatars.DefaultAvatarLink()
|
return avatars.DefaultAvatarLink()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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
|
// GetPossibleUserByID returns the user if id > 0 or return system usrs if id < 0
|
||||||
func GetPossibleUserByID(ctx context.Context, id int64) (*User, error) {
|
func GetPossibleUserByID(ctx context.Context, id int64) (*User, error) {
|
||||||
switch id {
|
switch id {
|
||||||
case -1:
|
case GhostUserID:
|
||||||
return NewGhostUser(), nil
|
return NewGhostUser(), nil
|
||||||
case ActionsUserID:
|
case ActionsUserID:
|
||||||
return NewActionsUser(), nil
|
return NewActionsUser(), nil
|
||||||
|
@ -949,7 +949,7 @@ func GetPossibleUserByIDs(ctx context.Context, ids []int64) ([]*User, error) {
|
||||||
uniqueIDs := container.SetOf(ids...)
|
uniqueIDs := container.SetOf(ids...)
|
||||||
users := make([]*User, 0, len(ids))
|
users := make([]*User, 0, len(ids))
|
||||||
_ = uniqueIDs.Remove(0)
|
_ = uniqueIDs.Remove(0)
|
||||||
if uniqueIDs.Remove(-1) {
|
if uniqueIDs.Remove(GhostUserID) {
|
||||||
users = append(users, NewGhostUser())
|
users = append(users, NewGhostUser())
|
||||||
}
|
}
|
||||||
if uniqueIDs.Remove(ActionsUserID) {
|
if uniqueIDs.Remove(ActionsUserID) {
|
||||||
|
|
|
@ -9,12 +9,18 @@ import (
|
||||||
"code.gitea.io/gitea/modules/structs"
|
"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.
|
// NewGhostUser creates and returns a fake user for someone has deleted their account.
|
||||||
func NewGhostUser() *User {
|
func NewGhostUser() *User {
|
||||||
return &User{
|
return &User{
|
||||||
ID: -1,
|
ID: GhostUserID,
|
||||||
Name: "Ghost",
|
Name: GhostUserName,
|
||||||
LowerName: "ghost",
|
LowerName: GhostUserLowerName,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -23,13 +29,13 @@ func (u *User) IsGhost() bool {
|
||||||
if u == nil {
|
if u == nil {
|
||||||
return false
|
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
|
// NewReplaceUser creates and returns a fake user for external user
|
||||||
func NewReplaceUser(name string) *User {
|
func NewReplaceUser(name string) *User {
|
||||||
return &User{
|
return &User{
|
||||||
ID: -1,
|
ID: 0,
|
||||||
Name: name,
|
Name: name,
|
||||||
LowerName: strings.ToLower(name),
|
LowerName: strings.ToLower(name),
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,7 +27,7 @@ func AvatarByUserName(ctx *context.Context) {
|
||||||
size := int(ctx.ParamsInt64(":size"))
|
size := int(ctx.ParamsInt64(":size"))
|
||||||
|
|
||||||
var user *user_model.User
|
var user *user_model.User
|
||||||
if strings.ToLower(userName) != "ghost" {
|
if strings.ToLower(userName) != user_model.GhostUserLowerName {
|
||||||
var err error
|
var err error
|
||||||
if user, err = user_model.GetUserByName(ctx, userName); err != nil {
|
if user, err = user_model.GetUserByName(ctx, userName); err != nil {
|
||||||
if user_model.IsErrUserNotExist(err) {
|
if user_model.IsErrUserNotExist(err) {
|
||||||
|
|
Loading…
Reference in a new issue