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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. "context"
  7. "fmt"
  8. "code.gitea.io/gitea/models/db"
  9. repo_model "code.gitea.io/gitea/models/repo"
  10. user_model "code.gitea.io/gitea/models/user"
  11. "code.gitea.io/gitea/modules/container"
  12. )
  13. // ActionList defines a list of actions
  14. type ActionList []*Action
  15. func (actions ActionList) getUserIDs() []int64 {
  16. userIDs := make(map[int64]struct{}, len(actions))
  17. for _, action := range actions {
  18. if _, ok := userIDs[action.ActUserID]; !ok {
  19. userIDs[action.ActUserID] = struct{}{}
  20. }
  21. }
  22. return container.KeysInt64(userIDs)
  23. }
  24. func (actions ActionList) loadUsers(ctx context.Context) (map[int64]*user_model.User, error) {
  25. if len(actions) == 0 {
  26. return nil, nil
  27. }
  28. userIDs := actions.getUserIDs()
  29. userMaps := make(map[int64]*user_model.User, len(userIDs))
  30. err := db.GetEngine(ctx).
  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 userMaps, nil
  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 container.KeysInt64(repoIDs)
  49. }
  50. func (actions ActionList) loadRepositories(ctx context.Context) error {
  51. if len(actions) == 0 {
  52. return nil
  53. }
  54. repoIDs := actions.getRepoIDs()
  55. repoMaps := make(map[int64]*repo_model.Repository, len(repoIDs))
  56. err := db.GetEngine(ctx).In("id", repoIDs).Find(&repoMaps)
  57. if err != nil {
  58. return fmt.Errorf("find repository: %v", err)
  59. }
  60. for _, action := range actions {
  61. action.Repo = repoMaps[action.RepoID]
  62. }
  63. return nil
  64. }
  65. func (actions ActionList) loadRepoOwner(ctx context.Context, userMap map[int64]*user_model.User) (err error) {
  66. if userMap == nil {
  67. userMap = make(map[int64]*user_model.User)
  68. }
  69. for _, action := range actions {
  70. if action.Repo == nil {
  71. continue
  72. }
  73. repoOwner, ok := userMap[action.Repo.OwnerID]
  74. if !ok {
  75. repoOwner, err = user_model.GetUserByIDCtx(ctx, action.Repo.OwnerID)
  76. if err != nil {
  77. if user_model.IsErrUserNotExist(err) {
  78. continue
  79. }
  80. return err
  81. }
  82. userMap[repoOwner.ID] = repoOwner
  83. }
  84. action.Repo.Owner = repoOwner
  85. }
  86. return nil
  87. }
  88. // loadAttributes loads all attributes
  89. func (actions ActionList) loadAttributes(ctx context.Context) error {
  90. userMap, err := actions.loadUsers(ctx)
  91. if err != nil {
  92. return err
  93. }
  94. if err := actions.loadRepositories(ctx); err != nil {
  95. return err
  96. }
  97. return actions.loadRepoOwner(ctx, userMap)
  98. }