2018-02-21 13:55:34 +03:00
// Copyright 2018 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 models
2021-09-19 14:49:59 +03:00
import (
"fmt"
"code.gitea.io/gitea/models/db"
2021-12-10 04:27:50 +03:00
repo_model "code.gitea.io/gitea/models/repo"
2021-11-24 12:49:20 +03:00
user_model "code.gitea.io/gitea/models/user"
2021-09-19 14:49:59 +03:00
)
2018-02-21 13:55:34 +03:00
// ActionList defines a list of actions
type ActionList [ ] * Action
func ( actions ActionList ) getUserIDs ( ) [ ] int64 {
userIDs := make ( map [ int64 ] struct { } , len ( actions ) )
for _ , action := range actions {
if _ , ok := userIDs [ action . ActUserID ] ; ! ok {
userIDs [ action . ActUserID ] = struct { } { }
}
}
return keysInt64 ( userIDs )
}
2022-03-13 19:40:47 +03:00
func ( actions ActionList ) loadUsers ( e db . Engine ) ( map [ int64 ] * user_model . User , error ) {
2018-02-21 13:55:34 +03:00
if len ( actions ) == 0 {
return nil , nil
}
userIDs := actions . getUserIDs ( )
2021-11-24 12:49:20 +03:00
userMaps := make ( map [ int64 ] * user_model . User , len ( userIDs ) )
2018-02-21 13:55:34 +03:00
err := e .
In ( "id" , userIDs ) .
Find ( & userMaps )
if err != nil {
return nil , fmt . Errorf ( "find user: %v" , err )
}
for _ , action := range actions {
action . ActUser = userMaps [ action . ActUserID ]
}
2022-03-13 19:40:47 +03:00
return userMaps , nil
2018-02-21 13:55:34 +03:00
}
func ( actions ActionList ) getRepoIDs ( ) [ ] int64 {
repoIDs := make ( map [ int64 ] struct { } , len ( actions ) )
for _ , action := range actions {
if _ , ok := repoIDs [ action . RepoID ] ; ! ok {
repoIDs [ action . RepoID ] = struct { } { }
}
}
return keysInt64 ( repoIDs )
}
2022-03-13 19:40:47 +03:00
func ( actions ActionList ) loadRepositories ( e db . Engine ) error {
2018-02-21 13:55:34 +03:00
if len ( actions ) == 0 {
2022-03-13 19:40:47 +03:00
return nil
2018-02-21 13:55:34 +03:00
}
repoIDs := actions . getRepoIDs ( )
2021-12-10 04:27:50 +03:00
repoMaps := make ( map [ int64 ] * repo_model . Repository , len ( repoIDs ) )
2022-03-13 19:40:47 +03:00
err := e . In ( "id" , repoIDs ) . Find ( & repoMaps )
2018-02-21 13:55:34 +03:00
if err != nil {
2022-03-13 19:40:47 +03:00
return fmt . Errorf ( "find repository: %v" , err )
2018-02-21 13:55:34 +03:00
}
for _ , action := range actions {
action . Repo = repoMaps [ action . RepoID ]
}
2022-03-13 19:40:47 +03:00
return nil
2018-02-21 13:55:34 +03:00
}
2022-03-13 19:40:47 +03:00
func ( actions ActionList ) loadRepoOwner ( e db . Engine , userMap map [ int64 ] * user_model . User ) ( err error ) {
if userMap == nil {
userMap = make ( map [ int64 ] * user_model . User )
2018-02-21 13:55:34 +03:00
}
2022-03-13 19:40:47 +03:00
for _ , action := range actions {
repoOwner , ok := userMap [ action . Repo . OwnerID ]
if ! ok {
repoOwner , err = user_model . GetUserByID ( action . Repo . OwnerID )
if err != nil {
if user_model . IsErrUserNotExist ( err ) {
continue
}
return err
}
userMap [ repoOwner . ID ] = repoOwner
}
action . Repo . Owner = repoOwner
2018-02-21 13:55:34 +03:00
}
return nil
}
2022-03-13 19:40:47 +03:00
// loadAttributes loads all attributes
func ( actions ActionList ) loadAttributes ( e db . Engine ) error {
userMap , err := actions . loadUsers ( e )
if err != nil {
return err
}
if err := actions . loadRepositories ( e ) ; err != nil {
return err
}
return actions . loadRepoOwner ( e , userMap )
2018-02-21 13:55:34 +03:00
}