2018-02-21 10:55:34 +00: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 11:49:59 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models/db"
|
|
|
|
)
|
2018-02-21 10:55:34 +00: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)
|
|
|
|
}
|
|
|
|
|
2021-09-19 11:49:59 +00:00
|
|
|
func (actions ActionList) loadUsers(e db.Engine) ([]*User, error) {
|
2018-02-21 10:55:34 +00:00
|
|
|
if len(actions) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
userIDs := actions.getUserIDs()
|
|
|
|
userMaps := make(map[int64]*User, len(userIDs))
|
|
|
|
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]
|
|
|
|
}
|
|
|
|
return valuesUser(userMaps), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadUsers loads actions' all users
|
|
|
|
func (actions ActionList) LoadUsers() ([]*User, error) {
|
2021-09-19 11:49:59 +00:00
|
|
|
return actions.loadUsers(db.DefaultContext().Engine())
|
2018-02-21 10:55:34 +00: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)
|
|
|
|
}
|
|
|
|
|
2021-09-19 11:49:59 +00:00
|
|
|
func (actions ActionList) loadRepositories(e db.Engine) ([]*Repository, error) {
|
2018-02-21 10:55:34 +00:00
|
|
|
if len(actions) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
repoIDs := actions.getRepoIDs()
|
|
|
|
repoMaps := make(map[int64]*Repository, len(repoIDs))
|
|
|
|
err := e.
|
|
|
|
In("id", repoIDs).
|
|
|
|
Find(&repoMaps)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("find repository: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, action := range actions {
|
|
|
|
action.Repo = repoMaps[action.RepoID]
|
|
|
|
}
|
|
|
|
return valuesRepository(repoMaps), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadRepositories loads actions' all repositories
|
|
|
|
func (actions ActionList) LoadRepositories() ([]*Repository, error) {
|
2021-09-19 11:49:59 +00:00
|
|
|
return actions.loadRepositories(db.DefaultContext().Engine())
|
2018-02-21 10:55:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// loadAttributes loads all attributes
|
2021-09-19 11:49:59 +00:00
|
|
|
func (actions ActionList) loadAttributes(e db.Engine) (err error) {
|
2018-02-21 10:55:34 +00:00
|
|
|
if _, err = actions.loadUsers(e); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err = actions.loadRepositories(e); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadAttributes loads attributes of the actions
|
|
|
|
func (actions ActionList) LoadAttributes() error {
|
2021-09-19 11:49:59 +00:00
|
|
|
return actions.loadAttributes(db.DefaultContext().Engine())
|
2018-02-21 10:55:34 +00:00
|
|
|
}
|