2018-02-21 05:55:34 -05: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.
|
|
|
|
|
2022-08-24 22:31:57 -04:00
|
|
|
package activities
|
2018-02-21 05:55:34 -05:00
|
|
|
|
2021-09-19 07:49:59 -04:00
|
|
|
import (
|
2022-05-20 10:08:52 -04:00
|
|
|
"context"
|
2021-09-19 07:49:59 -04:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-12-09 20:27:50 -05:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-24 04:49:20 -05:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2022-03-31 05:20:39 -04:00
|
|
|
"code.gitea.io/gitea/modules/container"
|
2021-09-19 07:49:59 -04:00
|
|
|
)
|
2018-02-21 05:55:34 -05: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{}{}
|
|
|
|
}
|
|
|
|
}
|
2022-03-31 05:20:39 -04:00
|
|
|
return container.KeysInt64(userIDs)
|
2018-02-21 05:55:34 -05:00
|
|
|
}
|
|
|
|
|
2022-05-20 10:08:52 -04:00
|
|
|
func (actions ActionList) loadUsers(ctx context.Context) (map[int64]*user_model.User, error) {
|
2018-02-21 05:55:34 -05:00
|
|
|
if len(actions) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
userIDs := actions.getUserIDs()
|
2021-11-24 04:49:20 -05:00
|
|
|
userMaps := make(map[int64]*user_model.User, len(userIDs))
|
2022-05-20 10:08:52 -04:00
|
|
|
err := db.GetEngine(ctx).
|
2018-02-21 05:55:34 -05:00
|
|
|
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 12:40:47 -04:00
|
|
|
return userMaps, nil
|
2018-02-21 05:55:34 -05: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{}{}
|
|
|
|
}
|
|
|
|
}
|
2022-03-31 05:20:39 -04:00
|
|
|
return container.KeysInt64(repoIDs)
|
2018-02-21 05:55:34 -05:00
|
|
|
}
|
|
|
|
|
2022-05-20 10:08:52 -04:00
|
|
|
func (actions ActionList) loadRepositories(ctx context.Context) error {
|
2018-02-21 05:55:34 -05:00
|
|
|
if len(actions) == 0 {
|
2022-03-13 12:40:47 -04:00
|
|
|
return nil
|
2018-02-21 05:55:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
repoIDs := actions.getRepoIDs()
|
2021-12-09 20:27:50 -05:00
|
|
|
repoMaps := make(map[int64]*repo_model.Repository, len(repoIDs))
|
2022-05-20 10:08:52 -04:00
|
|
|
err := db.GetEngine(ctx).In("id", repoIDs).Find(&repoMaps)
|
2018-02-21 05:55:34 -05:00
|
|
|
if err != nil {
|
2022-03-13 12:40:47 -04:00
|
|
|
return fmt.Errorf("find repository: %v", err)
|
2018-02-21 05:55:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, action := range actions {
|
|
|
|
action.Repo = repoMaps[action.RepoID]
|
|
|
|
}
|
2022-03-13 12:40:47 -04:00
|
|
|
return nil
|
2018-02-21 05:55:34 -05:00
|
|
|
}
|
|
|
|
|
2022-05-20 10:08:52 -04:00
|
|
|
func (actions ActionList) loadRepoOwner(ctx context.Context, userMap map[int64]*user_model.User) (err error) {
|
2022-03-13 12:40:47 -04:00
|
|
|
if userMap == nil {
|
|
|
|
userMap = make(map[int64]*user_model.User)
|
2018-02-21 05:55:34 -05:00
|
|
|
}
|
|
|
|
|
2022-03-13 12:40:47 -04:00
|
|
|
for _, action := range actions {
|
2022-05-05 11:39:26 -04:00
|
|
|
if action.Repo == nil {
|
|
|
|
continue
|
|
|
|
}
|
2022-03-13 12:40:47 -04:00
|
|
|
repoOwner, ok := userMap[action.Repo.OwnerID]
|
|
|
|
if !ok {
|
2022-05-20 10:08:52 -04:00
|
|
|
repoOwner, err = user_model.GetUserByIDCtx(ctx, action.Repo.OwnerID)
|
2022-03-13 12:40:47 -04:00
|
|
|
if err != nil {
|
|
|
|
if user_model.IsErrUserNotExist(err) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
userMap[repoOwner.ID] = repoOwner
|
|
|
|
}
|
|
|
|
action.Repo.Owner = repoOwner
|
2018-02-21 05:55:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-13 12:40:47 -04:00
|
|
|
// loadAttributes loads all attributes
|
2022-05-20 10:08:52 -04:00
|
|
|
func (actions ActionList) loadAttributes(ctx context.Context) error {
|
|
|
|
userMap, err := actions.loadUsers(ctx)
|
2022-03-13 12:40:47 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-05-20 10:08:52 -04:00
|
|
|
if err := actions.loadRepositories(ctx); err != nil {
|
2022-03-13 12:40:47 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-05-20 10:08:52 -04:00
|
|
|
return actions.loadRepoOwner(ctx, userMap)
|
2018-02-21 05:55:34 -05:00
|
|
|
}
|