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 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. "code.gitea.io/gitea/models/db"
  8. user_model "code.gitea.io/gitea/models/user"
  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 db.Engine) ([]*user_model.User, error) {
  22. if len(actions) == 0 {
  23. return nil, nil
  24. }
  25. userIDs := actions.getUserIDs()
  26. userMaps := make(map[int64]*user_model.User, len(userIDs))
  27. err := e.
  28. In("id", userIDs).
  29. Find(&userMaps)
  30. if err != nil {
  31. return nil, fmt.Errorf("find user: %v", err)
  32. }
  33. for _, action := range actions {
  34. action.ActUser = userMaps[action.ActUserID]
  35. }
  36. return valuesUser(userMaps), nil
  37. }
  38. // LoadUsers loads actions' all users
  39. func (actions ActionList) LoadUsers() ([]*user_model.User, error) {
  40. return actions.loadUsers(db.GetEngine(db.DefaultContext))
  41. }
  42. func (actions ActionList) getRepoIDs() []int64 {
  43. repoIDs := make(map[int64]struct{}, len(actions))
  44. for _, action := range actions {
  45. if _, ok := repoIDs[action.RepoID]; !ok {
  46. repoIDs[action.RepoID] = struct{}{}
  47. }
  48. }
  49. return keysInt64(repoIDs)
  50. }
  51. func (actions ActionList) loadRepositories(e db.Engine) ([]*Repository, error) {
  52. if len(actions) == 0 {
  53. return nil, nil
  54. }
  55. repoIDs := actions.getRepoIDs()
  56. repoMaps := make(map[int64]*Repository, len(repoIDs))
  57. err := e.
  58. In("id", repoIDs).
  59. Find(&repoMaps)
  60. if err != nil {
  61. return nil, fmt.Errorf("find repository: %v", err)
  62. }
  63. for _, action := range actions {
  64. action.Repo = repoMaps[action.RepoID]
  65. }
  66. return valuesRepository(repoMaps), nil
  67. }
  68. // LoadRepositories loads actions' all repositories
  69. func (actions ActionList) LoadRepositories() ([]*Repository, error) {
  70. return actions.loadRepositories(db.GetEngine(db.DefaultContext))
  71. }
  72. // loadAttributes loads all attributes
  73. func (actions ActionList) loadAttributes(e db.Engine) (err error) {
  74. if _, err = actions.loadUsers(e); err != nil {
  75. return
  76. }
  77. if _, err = actions.loadRepositories(e); err != nil {
  78. return
  79. }
  80. return nil
  81. }
  82. // LoadAttributes loads attributes of the actions
  83. func (actions ActionList) LoadAttributes() error {
  84. return actions.loadAttributes(db.GetEngine(db.DefaultContext))
  85. }