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.4KB

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