2018-02-21 16:25:34 +05:30
|
|
|
// Copyright 2018 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2018-02-21 16:25:34 +05:30
|
|
|
|
2022-08-25 08:01:57 +05:30
|
|
|
package activities
|
2018-02-21 16:25:34 +05:30
|
|
|
|
2021-09-19 17:19:59 +05:30
|
|
|
import (
|
2022-05-20 19:38:52 +05:30
|
|
|
"context"
|
2021-09-19 17:19:59 +05:30
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-12-10 06:57:50 +05:30
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-24 15:19:20 +05:30
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2022-03-31 14:50:39 +05:30
|
|
|
"code.gitea.io/gitea/modules/container"
|
2021-09-19 17:19:59 +05:30
|
|
|
)
|
2018-02-21 16:25:34 +05:30
|
|
|
|
|
|
|
// ActionList defines a list of actions
|
|
|
|
type ActionList []*Action
|
|
|
|
|
|
|
|
func (actions ActionList) getUserIDs() []int64 {
|
2022-10-12 10:48:26 +05:30
|
|
|
userIDs := make(container.Set[int64], len(actions))
|
2018-02-21 16:25:34 +05:30
|
|
|
for _, action := range actions {
|
2022-10-12 10:48:26 +05:30
|
|
|
userIDs.Add(action.ActUserID)
|
2018-02-21 16:25:34 +05:30
|
|
|
}
|
2022-10-12 10:48:26 +05:30
|
|
|
return userIDs.Values()
|
2018-02-21 16:25:34 +05:30
|
|
|
}
|
|
|
|
|
2022-05-20 19:38:52 +05:30
|
|
|
func (actions ActionList) loadUsers(ctx context.Context) (map[int64]*user_model.User, error) {
|
2018-02-21 16:25:34 +05:30
|
|
|
if len(actions) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
userIDs := actions.getUserIDs()
|
2021-11-24 15:19:20 +05:30
|
|
|
userMaps := make(map[int64]*user_model.User, len(userIDs))
|
2022-05-20 19:38:52 +05:30
|
|
|
err := db.GetEngine(ctx).
|
2018-02-21 16:25:34 +05:30
|
|
|
In("id", userIDs).
|
|
|
|
Find(&userMaps)
|
|
|
|
if err != nil {
|
2022-10-25 00:59:17 +05:30
|
|
|
return nil, fmt.Errorf("find user: %w", err)
|
2018-02-21 16:25:34 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
for _, action := range actions {
|
|
|
|
action.ActUser = userMaps[action.ActUserID]
|
|
|
|
}
|
2022-03-13 22:10:47 +05:30
|
|
|
return userMaps, nil
|
2018-02-21 16:25:34 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func (actions ActionList) getRepoIDs() []int64 {
|
2022-10-12 10:48:26 +05:30
|
|
|
repoIDs := make(container.Set[int64], len(actions))
|
2018-02-21 16:25:34 +05:30
|
|
|
for _, action := range actions {
|
2022-10-12 10:48:26 +05:30
|
|
|
repoIDs.Add(action.RepoID)
|
2018-02-21 16:25:34 +05:30
|
|
|
}
|
2022-10-12 10:48:26 +05:30
|
|
|
return repoIDs.Values()
|
2018-02-21 16:25:34 +05:30
|
|
|
}
|
|
|
|
|
2022-05-20 19:38:52 +05:30
|
|
|
func (actions ActionList) loadRepositories(ctx context.Context) error {
|
2018-02-21 16:25:34 +05:30
|
|
|
if len(actions) == 0 {
|
2022-03-13 22:10:47 +05:30
|
|
|
return nil
|
2018-02-21 16:25:34 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
repoIDs := actions.getRepoIDs()
|
2021-12-10 06:57:50 +05:30
|
|
|
repoMaps := make(map[int64]*repo_model.Repository, len(repoIDs))
|
2022-05-20 19:38:52 +05:30
|
|
|
err := db.GetEngine(ctx).In("id", repoIDs).Find(&repoMaps)
|
2018-02-21 16:25:34 +05:30
|
|
|
if err != nil {
|
2022-10-25 00:59:17 +05:30
|
|
|
return fmt.Errorf("find repository: %w", err)
|
2018-02-21 16:25:34 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
for _, action := range actions {
|
|
|
|
action.Repo = repoMaps[action.RepoID]
|
|
|
|
}
|
2022-03-13 22:10:47 +05:30
|
|
|
return nil
|
2018-02-21 16:25:34 +05:30
|
|
|
}
|
|
|
|
|
2022-05-20 19:38:52 +05:30
|
|
|
func (actions ActionList) loadRepoOwner(ctx context.Context, userMap map[int64]*user_model.User) (err error) {
|
2022-03-13 22:10:47 +05:30
|
|
|
if userMap == nil {
|
|
|
|
userMap = make(map[int64]*user_model.User)
|
2018-02-21 16:25:34 +05:30
|
|
|
}
|
|
|
|
|
2022-03-13 22:10:47 +05:30
|
|
|
for _, action := range actions {
|
2022-05-05 21:09:26 +05:30
|
|
|
if action.Repo == nil {
|
|
|
|
continue
|
|
|
|
}
|
2022-03-13 22:10:47 +05:30
|
|
|
repoOwner, ok := userMap[action.Repo.OwnerID]
|
|
|
|
if !ok {
|
2022-12-03 08:18:26 +05:30
|
|
|
repoOwner, err = user_model.GetUserByID(ctx, action.Repo.OwnerID)
|
2022-03-13 22:10:47 +05:30
|
|
|
if err != nil {
|
|
|
|
if user_model.IsErrUserNotExist(err) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
userMap[repoOwner.ID] = repoOwner
|
|
|
|
}
|
|
|
|
action.Repo.Owner = repoOwner
|
2018-02-21 16:25:34 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-13 22:10:47 +05:30
|
|
|
// loadAttributes loads all attributes
|
2022-05-20 19:38:52 +05:30
|
|
|
func (actions ActionList) loadAttributes(ctx context.Context) error {
|
|
|
|
userMap, err := actions.loadUsers(ctx)
|
2022-03-13 22:10:47 +05:30
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-05-20 19:38:52 +05:30
|
|
|
if err := actions.loadRepositories(ctx); err != nil {
|
2022-03-13 22:10:47 +05:30
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-05-20 19:38:52 +05:30
|
|
|
return actions.loadRepoOwner(ctx, userMap)
|
2018-02-21 16:25:34 +05:30
|
|
|
}
|