Support bot user
This commit is contained in:
@@ -77,7 +77,7 @@ func (run *Run) LoadAttributes(ctx context.Context) error {
|
||||
}
|
||||
|
||||
if run.TriggerUser == nil {
|
||||
u, err := user_model.GetUserByIDCtx(ctx, run.TriggerUserID)
|
||||
u, err := user_model.GetPossbileUserByID(ctx, run.TriggerUserID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -1,27 +0,0 @@
|
||||
// Copyright 2022 The Gitea Authors. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package bots
|
||||
|
||||
import (
|
||||
user_model "code.gitea.io/gitea/models/user"
|
||||
"code.gitea.io/gitea/modules/structs"
|
||||
)
|
||||
|
||||
// NewBotUser creates and returns a fake user for running the build.
|
||||
func NewBotUser() *user_model.User {
|
||||
return &user_model.User{
|
||||
ID: -2,
|
||||
Name: "gitea-bots",
|
||||
LowerName: "gitea-bots",
|
||||
IsActive: true,
|
||||
FullName: "Gitea Bots",
|
||||
Email: "teabot@gitea.io",
|
||||
KeepEmailPrivate: true,
|
||||
LoginName: "gitea-bots",
|
||||
Type: user_model.UserTypeIndividual,
|
||||
AllowCreateOrganization: true,
|
||||
Visibility: structs.VisibleTypePublic,
|
||||
}
|
||||
}
|
||||
@@ -351,7 +351,7 @@ func (c *Comment) LoadPoster(ctx context.Context) (err error) {
|
||||
return nil
|
||||
}
|
||||
|
||||
c.Poster, err = user_model.GetUserByIDCtx(ctx, c.PosterID)
|
||||
c.Poster, err = user_model.GetPossbileUserByID(ctx, c.PosterID)
|
||||
if err != nil {
|
||||
if user_model.IsErrUserNotExist(err) {
|
||||
c.PosterID = -1
|
||||
|
||||
@@ -49,12 +49,15 @@ func (comments CommentList) LoadPosters(ctx context.Context) error {
|
||||
}
|
||||
|
||||
for _, comment := range comments {
|
||||
if comment.PosterID <= 0 {
|
||||
if comment.PosterID == user_model.BotUserID {
|
||||
comment.Poster = user_model.NewBotUser()
|
||||
} else if comment.PosterID <= 0 {
|
||||
continue
|
||||
}
|
||||
var ok bool
|
||||
if comment.Poster, ok = posterMaps[comment.PosterID]; !ok {
|
||||
comment.Poster = user_model.NewGhostUser()
|
||||
} else {
|
||||
var ok bool
|
||||
if comment.Poster, ok = posterMaps[comment.PosterID]; !ok {
|
||||
comment.Poster = user_model.NewGhostUser()
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
||||
@@ -243,7 +243,7 @@ func (issue *Issue) LoadLabels(ctx context.Context) (err error) {
|
||||
// LoadPoster loads poster
|
||||
func (issue *Issue) LoadPoster(ctx context.Context) (err error) {
|
||||
if issue.Poster == nil {
|
||||
issue.Poster, err = user_model.GetUserByIDCtx(ctx, issue.PosterID)
|
||||
issue.Poster, err = user_model.GetPossbileUserByID(ctx, issue.PosterID)
|
||||
if err != nil {
|
||||
issue.PosterID = -1
|
||||
issue.Poster = user_model.NewGhostUser()
|
||||
|
||||
@@ -106,12 +106,15 @@ func (issues IssueList) loadPosters(ctx context.Context) error {
|
||||
}
|
||||
|
||||
for _, issue := range issues {
|
||||
if issue.PosterID <= 0 {
|
||||
if issue.PosterID == user_model.BotUserID {
|
||||
issue.Poster = user_model.NewBotUser()
|
||||
} else if issue.PosterID <= 0 {
|
||||
continue
|
||||
}
|
||||
var ok bool
|
||||
if issue.Poster, ok = posterMaps[issue.PosterID]; !ok {
|
||||
issue.Poster = user_model.NewGhostUser()
|
||||
} else {
|
||||
var ok bool
|
||||
if issue.Poster, ok = posterMaps[issue.PosterID]; !ok {
|
||||
issue.Poster = user_model.NewGhostUser()
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
||||
@@ -159,7 +159,7 @@ func (r *Review) LoadReviewer(ctx context.Context) (err error) {
|
||||
if r.ReviewerID == 0 || r.Reviewer != nil {
|
||||
return
|
||||
}
|
||||
r.Reviewer, err = user_model.GetUserByIDCtx(ctx, r.ReviewerID)
|
||||
r.Reviewer, err = user_model.GetPossbileUserByID(ctx, r.ReviewerID)
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
@@ -551,39 +551,6 @@ func GetUserSalt() (string, error) {
|
||||
return hex.EncodeToString(rBytes), nil
|
||||
}
|
||||
|
||||
// NewGhostUser creates and returns a fake user for someone has deleted their account.
|
||||
func NewGhostUser() *User {
|
||||
return &User{
|
||||
ID: -1,
|
||||
Name: "Ghost",
|
||||
LowerName: "ghost",
|
||||
}
|
||||
}
|
||||
|
||||
// NewReplaceUser creates and returns a fake user for external user
|
||||
func NewReplaceUser(name string) *User {
|
||||
return &User{
|
||||
ID: -1,
|
||||
Name: name,
|
||||
LowerName: strings.ToLower(name),
|
||||
}
|
||||
}
|
||||
|
||||
// IsGhost check if user is fake user for a deleted account
|
||||
func (u *User) IsGhost() bool {
|
||||
if u == nil {
|
||||
return false
|
||||
}
|
||||
return u.ID == -1 && u.Name == "Ghost"
|
||||
}
|
||||
|
||||
func (u *User) IsBots() bool {
|
||||
if u == nil {
|
||||
return false
|
||||
}
|
||||
return u.ID == -2 && u.Name == "gitea-bots"
|
||||
}
|
||||
|
||||
var (
|
||||
reservedUsernames = []string{
|
||||
".",
|
||||
@@ -1017,6 +984,20 @@ func GetUserByIDCtx(ctx context.Context, id int64) (*User, error) {
|
||||
return u, nil
|
||||
}
|
||||
|
||||
// GetPossbileUserByID returns the user if id > 0 or return system usrs if id < 0
|
||||
func GetPossbileUserByID(ctx context.Context, id int64) (*User, error) {
|
||||
switch id {
|
||||
case -1:
|
||||
return NewGhostUser(), nil
|
||||
case BotUserID:
|
||||
return NewBotUser(), nil
|
||||
case 0:
|
||||
return nil, ErrUserNotExist{}
|
||||
default:
|
||||
return GetUserByIDCtx(ctx, id)
|
||||
}
|
||||
}
|
||||
|
||||
// GetUserByNameCtx returns user by given name.
|
||||
func GetUserByName(ctx context.Context, name string) (*User, error) {
|
||||
if len(name) == 0 {
|
||||
|
||||
63
models/user/user_system.go
Normal file
63
models/user/user_system.go
Normal file
@@ -0,0 +1,63 @@
|
||||
// Copyright 2022 The Gitea Authors. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package user
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"code.gitea.io/gitea/modules/structs"
|
||||
)
|
||||
|
||||
// NewGhostUser creates and returns a fake user for someone has deleted their account.
|
||||
func NewGhostUser() *User {
|
||||
return &User{
|
||||
ID: -1,
|
||||
Name: "Ghost",
|
||||
LowerName: "ghost",
|
||||
}
|
||||
}
|
||||
|
||||
// IsGhost check if user is fake user for a deleted account
|
||||
func (u *User) IsGhost() bool {
|
||||
if u == nil {
|
||||
return false
|
||||
}
|
||||
return u.ID == -1 && u.Name == "Ghost"
|
||||
}
|
||||
|
||||
// NewReplaceUser creates and returns a fake user for external user
|
||||
func NewReplaceUser(name string) *User {
|
||||
return &User{
|
||||
ID: -1,
|
||||
Name: name,
|
||||
LowerName: strings.ToLower(name),
|
||||
}
|
||||
}
|
||||
|
||||
const BotUserID = -2
|
||||
|
||||
// NewBotUser creates and returns a fake user for running the build.
|
||||
func NewBotUser() *User {
|
||||
return &User{
|
||||
ID: BotUserID,
|
||||
Name: "gitea-bots",
|
||||
LowerName: "gitea-bots",
|
||||
IsActive: true,
|
||||
FullName: "Gitea Bots",
|
||||
Email: "teabot@gitea.io",
|
||||
KeepEmailPrivate: true,
|
||||
LoginName: "gitea-bots",
|
||||
Type: UserTypeIndividual,
|
||||
AllowCreateOrganization: true,
|
||||
Visibility: structs.VisibleTypePublic,
|
||||
}
|
||||
}
|
||||
|
||||
func (u *User) IsBots() bool {
|
||||
if u == nil {
|
||||
return false
|
||||
}
|
||||
return u.ID == BotUserID && u.Name == "gitea-bots"
|
||||
}
|
||||
Reference in New Issue
Block a user