本站源代码
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.

99 lines
2.3KB

  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 "fmt"
  6. // ActionList defines a list of actions
  7. type ActionList []*Action
  8. func (actions ActionList) getUserIDs() []int64 {
  9. userIDs := make(map[int64]struct{}, len(actions))
  10. for _, action := range actions {
  11. if _, ok := userIDs[action.ActUserID]; !ok {
  12. userIDs[action.ActUserID] = struct{}{}
  13. }
  14. }
  15. return keysInt64(userIDs)
  16. }
  17. func (actions ActionList) loadUsers(e Engine) ([]*User, error) {
  18. if len(actions) == 0 {
  19. return nil, nil
  20. }
  21. userIDs := actions.getUserIDs()
  22. userMaps := make(map[int64]*User, len(userIDs))
  23. err := e.
  24. In("id", userIDs).
  25. Find(&userMaps)
  26. if err != nil {
  27. return nil, fmt.Errorf("find user: %v", err)
  28. }
  29. for _, action := range actions {
  30. action.ActUser = userMaps[action.ActUserID]
  31. }
  32. return valuesUser(userMaps), nil
  33. }
  34. // LoadUsers loads actions' all users
  35. func (actions ActionList) LoadUsers() ([]*User, error) {
  36. return actions.loadUsers(x)
  37. }
  38. func (actions ActionList) getRepoIDs() []int64 {
  39. repoIDs := make(map[int64]struct{}, len(actions))
  40. for _, action := range actions {
  41. if _, ok := repoIDs[action.RepoID]; !ok {
  42. repoIDs[action.RepoID] = struct{}{}
  43. }
  44. }
  45. return keysInt64(repoIDs)
  46. }
  47. func (actions ActionList) loadRepositories(e Engine) ([]*Repository, error) {
  48. if len(actions) == 0 {
  49. return nil, nil
  50. }
  51. repoIDs := actions.getRepoIDs()
  52. repoMaps := make(map[int64]*Repository, len(repoIDs))
  53. err := e.
  54. In("id", repoIDs).
  55. Find(&repoMaps)
  56. if err != nil {
  57. return nil, fmt.Errorf("find repository: %v", err)
  58. }
  59. for _, action := range actions {
  60. action.Repo = repoMaps[action.RepoID]
  61. }
  62. return valuesRepository(repoMaps), nil
  63. }
  64. // LoadRepositories loads actions' all repositories
  65. func (actions ActionList) LoadRepositories() ([]*Repository, error) {
  66. return actions.loadRepositories(x)
  67. }
  68. // loadAttributes loads all attributes
  69. func (actions ActionList) loadAttributes(e Engine) (err error) {
  70. if _, err = actions.loadUsers(e); err != nil {
  71. return
  72. }
  73. if _, err = actions.loadRepositories(e); err != nil {
  74. return
  75. }
  76. return nil
  77. }
  78. // LoadAttributes loads attributes of the actions
  79. func (actions ActionList) LoadAttributes() error {
  80. return actions.loadAttributes(x)
  81. }
上海开阖软件有限公司 沪ICP备12045867号-1