@@ -528,6 +528,7 @@ type globalVarsStruct struct {
528528 replaceCharsHyphenRE * regexp.Regexp
529529 emailToReplacer * strings.Replacer
530530 emailRegexp * regexp.Regexp
531+ systemUserNewFuncs map [int64 ]func () * User
531532}
532533
533534var globalVars = sync .OnceValue (func () * globalVarsStruct {
@@ -550,6 +551,11 @@ var globalVars = sync.OnceValue(func() *globalVarsStruct {
550551 ";" , "" ,
551552 ),
552553 emailRegexp : regexp .MustCompile ("^[a-zA-Z0-9.!#$%&'*+-/=?^_`{|}~]*@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\ .[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$" ),
554+
555+ systemUserNewFuncs : map [int64 ]func () * User {
556+ GhostUserID : NewGhostUser ,
557+ ActionsUserID : NewActionsUser ,
558+ },
553559 }
554560})
555561
@@ -978,30 +984,28 @@ func GetUserByIDs(ctx context.Context, ids []int64) ([]*User, error) {
978984 return users , err
979985}
980986
981- // GetPossibleUserByID returns the user if id > 0 or return system usrs if id < 0
987+ // GetPossibleUserByID returns the user if id > 0 or returns system user if id < 0
982988func GetPossibleUserByID (ctx context.Context , id int64 ) (* User , error ) {
983- switch id {
984- case GhostUserID :
985- return NewGhostUser (), nil
986- case ActionsUserID :
987- return NewActionsUser (), nil
988- case 0 :
989+ if id < 0 {
990+ if newFunc , ok := globalVars (). systemUserNewFuncs [ id ]; ok {
991+ return newFunc (), nil
992+ }
993+ return nil , ErrUserNotExist { UID : id }
994+ } else if id == 0 {
989995 return nil , ErrUserNotExist {}
990- default :
991- return GetUserByID (ctx , id )
992996 }
997+ return GetUserByID (ctx , id )
993998}
994999
995- // GetPossibleUserByIDs returns the users if id > 0 or return system users if id < 0
1000+ // GetPossibleUserByIDs returns the users if id > 0 or returns system users if id < 0
9961001func GetPossibleUserByIDs (ctx context.Context , ids []int64 ) ([]* User , error ) {
9971002 uniqueIDs := container .SetOf (ids ... )
9981003 users := make ([]* User , 0 , len (ids ))
9991004 _ = uniqueIDs .Remove (0 )
1000- if uniqueIDs .Remove (GhostUserID ) {
1001- users = append (users , NewGhostUser ())
1002- }
1003- if uniqueIDs .Remove (ActionsUserID ) {
1004- users = append (users , NewActionsUser ())
1005+ for systemUID , newFunc := range globalVars ().systemUserNewFuncs {
1006+ if uniqueIDs .Remove (systemUID ) {
1007+ users = append (users , newFunc ())
1008+ }
10051009 }
10061010 res , err := GetUserByIDs (ctx , uniqueIDs .Values ())
10071011 if err != nil {
@@ -1011,7 +1015,7 @@ func GetPossibleUserByIDs(ctx context.Context, ids []int64) ([]*User, error) {
10111015 return users , nil
10121016}
10131017
1014- // GetUserByNameCtx returns user by given name.
1018+ // GetUserByName returns user by given name.
10151019func GetUserByName (ctx context.Context , name string ) (* User , error ) {
10161020 if len (name ) == 0 {
10171021 return nil , ErrUserNotExist {Name : name }
@@ -1042,8 +1046,8 @@ func GetUserEmailsByNames(ctx context.Context, names []string) []string {
10421046 return mails
10431047}
10441048
1045- // GetMaileableUsersByIDs gets users from ids, but only if they can receive mails
1046- func GetMaileableUsersByIDs (ctx context.Context , ids []int64 , isMention bool ) ([]* User , error ) {
1049+ // GetMailableUsersByIDs gets users from ids, but only if they can receive mails
1050+ func GetMailableUsersByIDs (ctx context.Context , ids []int64 , isMention bool ) ([]* User , error ) {
10471051 if len (ids ) == 0 {
10481052 return nil , nil
10491053 }
@@ -1068,17 +1072,6 @@ func GetMaileableUsersByIDs(ctx context.Context, ids []int64, isMention bool) ([
10681072 Find (& ous )
10691073}
10701074
1071- // GetUserNamesByIDs returns usernames for all resolved users from a list of Ids.
1072- func GetUserNamesByIDs (ctx context.Context , ids []int64 ) ([]string , error ) {
1073- unames := make ([]string , 0 , len (ids ))
1074- err := db .GetEngine (ctx ).In ("id" , ids ).
1075- Table ("user" ).
1076- Asc ("name" ).
1077- Cols ("name" ).
1078- Find (& unames )
1079- return unames , err
1080- }
1081-
10821075// GetUserNameByID returns username for the id
10831076func GetUserNameByID (ctx context.Context , id int64 ) (string , error ) {
10841077 var name string
0 commit comments