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

action_list.go 6.6 kB

3 years ago
2 years ago
3 years ago
2 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. // Copyright 2018 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package models
  5. import (
  6. "fmt"
  7. "strconv"
  8. "xorm.io/builder"
  9. )
  10. // ActionList defines a list of actions
  11. type ActionList []*Action
  12. func (actions ActionList) getUserIDs() []int64 {
  13. userIDs := make(map[int64]struct{}, len(actions))
  14. for _, action := range actions {
  15. if _, ok := userIDs[action.ActUserID]; !ok {
  16. userIDs[action.ActUserID] = struct{}{}
  17. }
  18. }
  19. return keysInt64(userIDs)
  20. }
  21. func (actions ActionList) loadUsers(e Engine) ([]*User, error) {
  22. if len(actions) == 0 {
  23. return nil, nil
  24. }
  25. userIDs := actions.getUserIDs()
  26. userMaps := make(map[int64]*User, len(userIDs))
  27. if len(userIDs) == 0 {
  28. return make([]*User, 0), nil
  29. }
  30. err := e.
  31. In("id", userIDs).
  32. Find(&userMaps)
  33. if err != nil {
  34. return nil, fmt.Errorf("find user: %v", err)
  35. }
  36. for _, action := range actions {
  37. action.ActUser = userMaps[action.ActUserID]
  38. }
  39. return valuesUser(userMaps), nil
  40. }
  41. // LoadUsers loads actions' all users
  42. func (actions ActionList) LoadUsers() ([]*User, error) {
  43. return actions.loadUsers(x)
  44. }
  45. func (actions ActionList) getRepoIDs() []int64 {
  46. repoIDs := make(map[int64]struct{}, len(actions))
  47. for _, action := range actions {
  48. if _, ok := repoIDs[action.RepoID]; !ok {
  49. repoIDs[action.RepoID] = struct{}{}
  50. }
  51. }
  52. return keysInt64(repoIDs)
  53. }
  54. func (actions ActionList) loadRepositories(e Engine) ([]*Repository, error) {
  55. if len(actions) == 0 {
  56. return nil, nil
  57. }
  58. repoIDs := actions.getRepoIDs()
  59. repoMaps := make(map[int64]*Repository, len(repoIDs))
  60. if len(repoIDs) == 0 {
  61. return make([]*Repository, 0), nil
  62. }
  63. err := e.
  64. In("id", repoIDs).
  65. Find(&repoMaps)
  66. if err != nil {
  67. return nil, fmt.Errorf("find repository: %v", err)
  68. }
  69. for _, action := range actions {
  70. action.Repo = repoMaps[action.RepoID]
  71. }
  72. return valuesRepository(repoMaps), nil
  73. }
  74. // LoadRepositories loads actions' all repositories
  75. func (actions ActionList) LoadRepositories() ([]*Repository, error) {
  76. return actions.loadRepositories(x)
  77. }
  78. func (actions ActionList) getCommentIDs() []int64 {
  79. commentIDs := make(map[int64]struct{}, len(actions))
  80. for _, action := range actions {
  81. if action.CommentID == 0 {
  82. continue
  83. }
  84. if _, ok := commentIDs[action.CommentID]; !ok {
  85. commentIDs[action.CommentID] = struct{}{}
  86. }
  87. }
  88. return keysInt64(commentIDs)
  89. }
  90. func (actions ActionList) loadComments(e Engine) ([]*Comment, error) {
  91. if len(actions) == 0 {
  92. return nil, nil
  93. }
  94. commentIDs := actions.getCommentIDs()
  95. commentMaps := make(map[int64]*Comment, len(commentIDs))
  96. if len(commentIDs) == 0 {
  97. return make([]*Comment, 0), nil
  98. }
  99. err := e.
  100. In("id", commentIDs).
  101. Find(&commentMaps)
  102. if err != nil {
  103. return nil, fmt.Errorf("find comment: %v", err)
  104. }
  105. for _, action := range actions {
  106. if action.CommentID > 0 {
  107. action.Comment = commentMaps[action.CommentID]
  108. }
  109. }
  110. return valuesComment(commentMaps), nil
  111. }
  112. // LoadComments loads actions' all comments
  113. func (actions ActionList) LoadComments() ([]*Comment, error) {
  114. return actions.loadComments(x)
  115. }
  116. func (actions ActionList) getCloudbrainIDs() []int64 {
  117. cloudbrainIDs := make(map[int64]struct{}, 0)
  118. for _, action := range actions {
  119. if !action.IsCloudbrainAction() {
  120. continue
  121. }
  122. cloudbrainId, _ := strconv.ParseInt(action.Content, 10, 64)
  123. if _, ok := cloudbrainIDs[cloudbrainId]; !ok {
  124. cloudbrainIDs[cloudbrainId] = struct{}{}
  125. }
  126. }
  127. return keysInt64(cloudbrainIDs)
  128. }
  129. func (actions ActionList) getCloudbrainJobIDs() []string {
  130. cloudbrainJobIDs := make(map[string]struct{}, 0)
  131. for _, action := range actions {
  132. if !action.IsCloudbrainAction() {
  133. continue
  134. }
  135. if _, ok := cloudbrainJobIDs[action.Content]; !ok {
  136. cloudbrainJobIDs[action.Content] = struct{}{}
  137. }
  138. }
  139. return keysString(cloudbrainJobIDs)
  140. }
  141. func (actions ActionList) loadCloudbrains(e Engine) ([]*Cloudbrain, error) {
  142. if len(actions) == 0 {
  143. return nil, nil
  144. }
  145. cloudbrainIDs := actions.getCloudbrainIDs()
  146. cloudbrainJobIDs := actions.getCloudbrainJobIDs()
  147. cloudbrainMaps := make(map[int64]*Cloudbrain, len(cloudbrainIDs))
  148. if len(cloudbrainIDs) == 0 {
  149. return make([]*Cloudbrain, 0), nil
  150. }
  151. //由于各个类型的云脑任务在发布action的时候,content字段保存的ID含义不同,部分取的是ID,部分取的是jobId
  152. //所以在查询action对应的cloudbrain对象时,以这两个字段做为条件查询
  153. cond := builder.Or(builder.In("id", cloudbrainIDs)).Or(builder.In("job_id", cloudbrainJobIDs))
  154. err := e.
  155. Where(cond).Unscoped().
  156. Find(&cloudbrainMaps)
  157. if err != nil {
  158. return nil, fmt.Errorf("find cloudbrain: %v", err)
  159. }
  160. cloudBrainJobIdMap := make(map[string]*Cloudbrain, len(cloudbrainIDs))
  161. for _, v := range cloudbrainMaps {
  162. cloudBrainJobIdMap[v.JobID] = v
  163. }
  164. for _, action := range actions {
  165. if !action.IsCloudbrainAction() {
  166. continue
  167. }
  168. cloudbrainId, _ := strconv.ParseInt(action.Content, 10, 64)
  169. if cloudbrainId > 0 {
  170. if c, ok := cloudbrainMaps[cloudbrainId]; ok {
  171. if c.DisplayJobName == action.RefName || c.JobName == action.RefName {
  172. action.Cloudbrain = c
  173. continue
  174. }
  175. }
  176. }
  177. if c, ok := cloudBrainJobIdMap[action.Content]; ok {
  178. if c.DisplayJobName == action.RefName || c.JobName == action.RefName {
  179. action.Cloudbrain = c
  180. continue
  181. }
  182. }
  183. }
  184. return valuesCloudbrain(cloudbrainMaps), nil
  185. }
  186. // LoadComments loads actions' all comments
  187. func (actions ActionList) LoadCloudbrains() ([]*Comment, error) {
  188. return actions.loadComments(x)
  189. }
  190. // loadAttributes loads all attributes
  191. func (actions ActionList) loadAttributes(e Engine) (err error) {
  192. if _, err = actions.loadUsers(e); err != nil {
  193. return
  194. }
  195. if _, err = actions.loadRepositories(e); err != nil {
  196. return
  197. }
  198. return nil
  199. }
  200. // LoadAttributes loads attributes of the actions
  201. func (actions ActionList) LoadAttributes() error {
  202. return actions.loadAttributes(x)
  203. }
  204. // LoadAllAttributes loads all attributes of the actions
  205. // compare with LoadAttributes() ,LoadAllAttributes() loads Comment and Cloudbrain attribute
  206. func (actions ActionList) LoadAllAttributes() error {
  207. return actions.loadAllAttributes(x)
  208. }
  209. // loadAllAttributes
  210. func (actions ActionList) loadAllAttributes(e Engine) (err error) {
  211. if _, err = actions.loadUsers(e); err != nil {
  212. return
  213. }
  214. if _, err = actions.loadRepositories(e); err != nil {
  215. return
  216. }
  217. if _, err = actions.loadComments(e); err != nil {
  218. return
  219. }
  220. if _, err = actions.loadCloudbrains(e); err != nil {
  221. return
  222. }
  223. return nil
  224. }