2018-02-21 05:55:34 -05:00
|
|
|
// Copyright 2018 The Gitea Authors. All rights reserved.
|
2022-11-27 13:20:29 -05:00
|
|
|
// SPDX-License-Identifier: MIT
|
2018-02-21 05:55:34 -05:00
|
|
|
|
2022-08-24 21:31:57 -05:00
|
|
|
package activities
|
2018-02-21 05:55:34 -05:00
|
|
|
|
2021-09-19 06:49:59 -05:00
|
|
|
import (
|
2022-05-20 09:08:52 -05:00
|
|
|
"context"
|
2021-09-19 06:49:59 -05:00
|
|
|
"fmt"
|
2024-03-11 23:57:19 -05:00
|
|
|
"strconv"
|
2021-09-19 06:49:59 -05:00
|
|
|
|
|
|
|
"code.gitea.io/gitea/models/db"
|
2024-03-11 23:57:19 -05:00
|
|
|
issues_model "code.gitea.io/gitea/models/issues"
|
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 04:20:39 -05:00
|
|
|
"code.gitea.io/gitea/modules/container"
|
2024-03-11 23:57:19 -05:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
|
|
|
|
|
|
|
"xorm.io/builder"
|
2021-09-19 06:49:59 -05:00
|
|
|
)
|
2018-02-21 05:55:34 -05:00
|
|
|
|
|
|
|
// ActionList defines a list of actions
|
|
|
|
type ActionList []*Action
|
|
|
|
|
|
|
|
func (actions ActionList) getUserIDs() []int64 {
|
2022-10-12 00:18:26 -05:00
|
|
|
userIDs := make(container.Set[int64], len(actions))
|
2018-02-21 05:55:34 -05:00
|
|
|
for _, action := range actions {
|
2022-10-12 00:18:26 -05:00
|
|
|
userIDs.Add(action.ActUserID)
|
2018-02-21 05:55:34 -05:00
|
|
|
}
|
2022-10-12 00:18:26 -05:00
|
|
|
return userIDs.Values()
|
2018-02-21 05:55:34 -05:00
|
|
|
}
|
|
|
|
|
2024-03-11 23:57:19 -05:00
|
|
|
func (actions ActionList) LoadActUsers(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 09:08:52 -05:00
|
|
|
err := db.GetEngine(ctx).
|
2018-02-21 05:55:34 -05:00
|
|
|
In("id", userIDs).
|
|
|
|
Find(&userMaps)
|
|
|
|
if err != nil {
|
2022-10-24 14:29:17 -05:00
|
|
|
return nil, fmt.Errorf("find user: %w", err)
|
2018-02-21 05:55:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, action := range actions {
|
|
|
|
action.ActUser = userMaps[action.ActUserID]
|
|
|
|
}
|
2022-03-13 11:40:47 -05:00
|
|
|
return userMaps, nil
|
2018-02-21 05:55:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (actions ActionList) getRepoIDs() []int64 {
|
2022-10-12 00:18:26 -05:00
|
|
|
repoIDs := make(container.Set[int64], len(actions))
|
2018-02-21 05:55:34 -05:00
|
|
|
for _, action := range actions {
|
2022-10-12 00:18:26 -05:00
|
|
|
repoIDs.Add(action.RepoID)
|
2018-02-21 05:55:34 -05:00
|
|
|
}
|
2022-10-12 00:18:26 -05:00
|
|
|
return repoIDs.Values()
|
2018-02-21 05:55:34 -05:00
|
|
|
}
|
|
|
|
|
2024-03-11 23:57:19 -05:00
|
|
|
func (actions ActionList) LoadRepositories(ctx context.Context) error {
|
2018-02-21 05:55:34 -05:00
|
|
|
if len(actions) == 0 {
|
2022-03-13 11:40:47 -05: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 09:08:52 -05:00
|
|
|
err := db.GetEngine(ctx).In("id", repoIDs).Find(&repoMaps)
|
2018-02-21 05:55:34 -05:00
|
|
|
if err != nil {
|
2022-10-24 14:29:17 -05:00
|
|
|
return fmt.Errorf("find repository: %w", err)
|
2018-02-21 05:55:34 -05:00
|
|
|
}
|
|
|
|
for _, action := range actions {
|
|
|
|
action.Repo = repoMaps[action.RepoID]
|
|
|
|
}
|
2024-03-11 23:57:19 -05:00
|
|
|
repos := repo_model.RepositoryList(util.ValuesOfMap(repoMaps))
|
|
|
|
return repos.LoadUnits(ctx)
|
2018-02-21 05:55:34 -05:00
|
|
|
}
|
|
|
|
|
2022-05-20 09:08:52 -05:00
|
|
|
func (actions ActionList) loadRepoOwner(ctx context.Context, userMap map[int64]*user_model.User) (err error) {
|
2022-03-13 11:40:47 -05:00
|
|
|
if userMap == nil {
|
|
|
|
userMap = make(map[int64]*user_model.User)
|
2018-02-21 05:55:34 -05:00
|
|
|
}
|
|
|
|
|
2024-03-11 23:57:19 -05:00
|
|
|
userSet := make(container.Set[int64], len(actions))
|
2022-03-13 11:40:47 -05:00
|
|
|
for _, action := range actions {
|
2022-05-05 10:39:26 -05:00
|
|
|
if action.Repo == nil {
|
|
|
|
continue
|
|
|
|
}
|
2024-03-11 23:57:19 -05:00
|
|
|
if _, ok := userMap[action.Repo.OwnerID]; !ok {
|
|
|
|
userSet.Add(action.Repo.OwnerID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := db.GetEngine(ctx).
|
|
|
|
In("id", userSet.Values()).
|
|
|
|
Find(&userMap); err != nil {
|
|
|
|
return fmt.Errorf("find user: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, action := range actions {
|
|
|
|
if action.Repo != nil {
|
|
|
|
action.Repo.Owner = userMap[action.Repo.OwnerID]
|
2022-03-13 11:40:47 -05:00
|
|
|
}
|
2018-02-21 05:55:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-03-11 23:57:19 -05:00
|
|
|
// LoadAttributes loads all attributes
|
|
|
|
func (actions ActionList) LoadAttributes(ctx context.Context) error {
|
|
|
|
// the load sequence cannot be changed because of the dependencies
|
|
|
|
userMap, err := actions.LoadActUsers(ctx)
|
2022-03-13 11:40:47 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-03-11 23:57:19 -05:00
|
|
|
if err := actions.LoadRepositories(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := actions.loadRepoOwner(ctx, userMap); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := actions.LoadIssues(ctx); err != nil {
|
2022-03-13 11:40:47 -05:00
|
|
|
return err
|
|
|
|
}
|
2024-03-11 23:57:19 -05:00
|
|
|
return actions.LoadComments(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (actions ActionList) LoadComments(ctx context.Context) error {
|
|
|
|
if len(actions) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
commentIDs := make([]int64, 0, len(actions))
|
|
|
|
for _, action := range actions {
|
|
|
|
if action.CommentID > 0 {
|
|
|
|
commentIDs = append(commentIDs, action.CommentID)
|
|
|
|
}
|
|
|
|
}
|
2022-03-13 11:40:47 -05:00
|
|
|
|
2024-03-11 23:57:19 -05:00
|
|
|
commentsMap := make(map[int64]*issues_model.Comment, len(commentIDs))
|
|
|
|
if err := db.GetEngine(ctx).In("id", commentIDs).Find(&commentsMap); err != nil {
|
|
|
|
return fmt.Errorf("find comment: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, action := range actions {
|
|
|
|
if action.CommentID > 0 {
|
|
|
|
action.Comment = commentsMap[action.CommentID]
|
|
|
|
if action.Comment != nil {
|
|
|
|
action.Comment.Issue = action.Issue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (actions ActionList) LoadIssues(ctx context.Context) error {
|
|
|
|
if len(actions) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
conditions := builder.NewCond()
|
|
|
|
issueNum := 0
|
|
|
|
for _, action := range actions {
|
|
|
|
if action.IsIssueEvent() {
|
|
|
|
infos := action.GetIssueInfos()
|
|
|
|
if len(infos) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
index, _ := strconv.ParseInt(infos[0], 10, 64)
|
|
|
|
if index > 0 {
|
|
|
|
conditions = conditions.Or(builder.Eq{
|
|
|
|
"repo_id": action.RepoID,
|
|
|
|
"`index`": index,
|
|
|
|
})
|
|
|
|
issueNum++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !conditions.IsValid() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
issuesMap := make(map[string]*issues_model.Issue, issueNum)
|
|
|
|
issues := make([]*issues_model.Issue, 0, issueNum)
|
|
|
|
if err := db.GetEngine(ctx).Where(conditions).Find(&issues); err != nil {
|
|
|
|
return fmt.Errorf("find issue: %w", err)
|
|
|
|
}
|
|
|
|
for _, issue := range issues {
|
|
|
|
issuesMap[fmt.Sprintf("%d-%d", issue.RepoID, issue.Index)] = issue
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, action := range actions {
|
|
|
|
if !action.IsIssueEvent() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if index := action.getIssueIndex(); index > 0 {
|
|
|
|
if issue, ok := issuesMap[fmt.Sprintf("%d-%d", action.RepoID, index)]; ok {
|
|
|
|
action.Issue = issue
|
|
|
|
action.Issue.Repo = action.Repo
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2018-02-21 05:55:34 -05:00
|
|
|
}
|