You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

action_list.go 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. // Copyright 2018 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package activities
  4. import (
  5. "context"
  6. "fmt"
  7. "strconv"
  8. "code.gitea.io/gitea/models/db"
  9. issues_model "code.gitea.io/gitea/models/issues"
  10. repo_model "code.gitea.io/gitea/models/repo"
  11. user_model "code.gitea.io/gitea/models/user"
  12. "code.gitea.io/gitea/modules/container"
  13. "code.gitea.io/gitea/modules/util"
  14. "xorm.io/builder"
  15. )
  16. // ActionList defines a list of actions
  17. type ActionList []*Action
  18. func (actions ActionList) getUserIDs() []int64 {
  19. return container.FilterSlice(actions, func(action *Action) (int64, bool) {
  20. return action.ActUserID, true
  21. })
  22. }
  23. func (actions ActionList) LoadActUsers(ctx context.Context) (map[int64]*user_model.User, error) {
  24. if len(actions) == 0 {
  25. return nil, nil
  26. }
  27. userIDs := actions.getUserIDs()
  28. userMaps := make(map[int64]*user_model.User, len(userIDs))
  29. err := db.GetEngine(ctx).
  30. In("id", userIDs).
  31. Find(&userMaps)
  32. if err != nil {
  33. return nil, fmt.Errorf("find user: %w", err)
  34. }
  35. for _, action := range actions {
  36. action.ActUser = userMaps[action.ActUserID]
  37. }
  38. return userMaps, nil
  39. }
  40. func (actions ActionList) getRepoIDs() []int64 {
  41. return container.FilterSlice(actions, func(action *Action) (int64, bool) {
  42. return action.RepoID, true
  43. })
  44. }
  45. func (actions ActionList) LoadRepositories(ctx context.Context) error {
  46. if len(actions) == 0 {
  47. return nil
  48. }
  49. repoIDs := actions.getRepoIDs()
  50. repoMaps := make(map[int64]*repo_model.Repository, len(repoIDs))
  51. err := db.GetEngine(ctx).In("id", repoIDs).Find(&repoMaps)
  52. if err != nil {
  53. return fmt.Errorf("find repository: %w", err)
  54. }
  55. for _, action := range actions {
  56. action.Repo = repoMaps[action.RepoID]
  57. }
  58. repos := repo_model.RepositoryList(util.ValuesOfMap(repoMaps))
  59. return repos.LoadUnits(ctx)
  60. }
  61. func (actions ActionList) loadRepoOwner(ctx context.Context, userMap map[int64]*user_model.User) (err error) {
  62. if userMap == nil {
  63. userMap = make(map[int64]*user_model.User)
  64. }
  65. missingUserIDs := container.FilterSlice(actions, func(action *Action) (int64, bool) {
  66. if action.Repo == nil {
  67. return 0, false
  68. }
  69. _, alreadyLoaded := userMap[action.Repo.OwnerID]
  70. return action.Repo.OwnerID, !alreadyLoaded
  71. })
  72. if err := db.GetEngine(ctx).
  73. In("id", missingUserIDs).
  74. Find(&userMap); err != nil {
  75. return fmt.Errorf("find user: %w", err)
  76. }
  77. for _, action := range actions {
  78. if action.Repo != nil {
  79. action.Repo.Owner = userMap[action.Repo.OwnerID]
  80. }
  81. }
  82. return nil
  83. }
  84. // LoadAttributes loads all attributes
  85. func (actions ActionList) LoadAttributes(ctx context.Context) error {
  86. // the load sequence cannot be changed because of the dependencies
  87. userMap, err := actions.LoadActUsers(ctx)
  88. if err != nil {
  89. return err
  90. }
  91. if err := actions.LoadRepositories(ctx); err != nil {
  92. return err
  93. }
  94. if err := actions.loadRepoOwner(ctx, userMap); err != nil {
  95. return err
  96. }
  97. if err := actions.LoadIssues(ctx); err != nil {
  98. return err
  99. }
  100. return actions.LoadComments(ctx)
  101. }
  102. func (actions ActionList) LoadComments(ctx context.Context) error {
  103. if len(actions) == 0 {
  104. return nil
  105. }
  106. commentIDs := make([]int64, 0, len(actions))
  107. for _, action := range actions {
  108. if action.CommentID > 0 {
  109. commentIDs = append(commentIDs, action.CommentID)
  110. }
  111. }
  112. commentsMap := make(map[int64]*issues_model.Comment, len(commentIDs))
  113. if err := db.GetEngine(ctx).In("id", commentIDs).Find(&commentsMap); err != nil {
  114. return fmt.Errorf("find comment: %w", err)
  115. }
  116. for _, action := range actions {
  117. if action.CommentID > 0 {
  118. action.Comment = commentsMap[action.CommentID]
  119. if action.Comment != nil {
  120. action.Comment.Issue = action.Issue
  121. }
  122. }
  123. }
  124. return nil
  125. }
  126. func (actions ActionList) LoadIssues(ctx context.Context) error {
  127. if len(actions) == 0 {
  128. return nil
  129. }
  130. conditions := builder.NewCond()
  131. issueNum := 0
  132. for _, action := range actions {
  133. if action.IsIssueEvent() {
  134. infos := action.GetIssueInfos()
  135. if len(infos) == 0 {
  136. continue
  137. }
  138. index, _ := strconv.ParseInt(infos[0], 10, 64)
  139. if index > 0 {
  140. conditions = conditions.Or(builder.Eq{
  141. "repo_id": action.RepoID,
  142. "`index`": index,
  143. })
  144. issueNum++
  145. }
  146. }
  147. }
  148. if !conditions.IsValid() {
  149. return nil
  150. }
  151. issuesMap := make(map[string]*issues_model.Issue, issueNum)
  152. issues := make([]*issues_model.Issue, 0, issueNum)
  153. if err := db.GetEngine(ctx).Where(conditions).Find(&issues); err != nil {
  154. return fmt.Errorf("find issue: %w", err)
  155. }
  156. for _, issue := range issues {
  157. issuesMap[fmt.Sprintf("%d-%d", issue.RepoID, issue.Index)] = issue
  158. }
  159. for _, action := range actions {
  160. if !action.IsIssueEvent() {
  161. continue
  162. }
  163. if index := action.getIssueIndex(); index > 0 {
  164. if issue, ok := issuesMap[fmt.Sprintf("%d-%d", action.RepoID, index)]; ok {
  165. action.Issue = issue
  166. action.Issue.Repo = action.Repo
  167. }
  168. }
  169. }
  170. return nil
  171. }