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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright 2018 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package activities
  4. import (
  5. "context"
  6. "fmt"
  7. "code.gitea.io/gitea/models/db"
  8. repo_model "code.gitea.io/gitea/models/repo"
  9. user_model "code.gitea.io/gitea/models/user"
  10. "code.gitea.io/gitea/modules/container"
  11. )
  12. // ActionList defines a list of actions
  13. type ActionList []*Action
  14. func (actions ActionList) getUserIDs() []int64 {
  15. userIDs := make(container.Set[int64], len(actions))
  16. for _, action := range actions {
  17. userIDs.Add(action.ActUserID)
  18. }
  19. return userIDs.Values()
  20. }
  21. func (actions ActionList) loadUsers(ctx context.Context) (map[int64]*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 := db.GetEngine(ctx).
  28. In("id", userIDs).
  29. Find(&userMaps)
  30. if err != nil {
  31. return nil, fmt.Errorf("find user: %w", err)
  32. }
  33. for _, action := range actions {
  34. action.ActUser = userMaps[action.ActUserID]
  35. }
  36. return userMaps, nil
  37. }
  38. func (actions ActionList) getRepoIDs() []int64 {
  39. repoIDs := make(container.Set[int64], len(actions))
  40. for _, action := range actions {
  41. repoIDs.Add(action.RepoID)
  42. }
  43. return repoIDs.Values()
  44. }
  45. func (actions ActionList) loadRepositories(ctx context.Context) error {
  46. if len(actions) == 0 {
  47. return nil
  48. }
  49. repoIDs := actions.getRepoIDs()
  50. repoMaps := make(map[int64]*repo_model.Repository, len(repoIDs))
  51. err := db.GetEngine(ctx).In("id", repoIDs).Find(&repoMaps)
  52. if err != nil {
  53. return fmt.Errorf("find repository: %w", err)
  54. }
  55. for _, action := range actions {
  56. action.Repo = repoMaps[action.RepoID]
  57. }
  58. return nil
  59. }
  60. func (actions ActionList) loadRepoOwner(ctx context.Context, userMap map[int64]*user_model.User) (err error) {
  61. if userMap == nil {
  62. userMap = make(map[int64]*user_model.User)
  63. }
  64. for _, action := range actions {
  65. if action.Repo == nil {
  66. continue
  67. }
  68. repoOwner, ok := userMap[action.Repo.OwnerID]
  69. if !ok {
  70. repoOwner, err = user_model.GetUserByIDCtx(ctx, action.Repo.OwnerID)
  71. if err != nil {
  72. if user_model.IsErrUserNotExist(err) {
  73. continue
  74. }
  75. return err
  76. }
  77. userMap[repoOwner.ID] = repoOwner
  78. }
  79. action.Repo.Owner = repoOwner
  80. }
  81. return nil
  82. }
  83. // loadAttributes loads all attributes
  84. func (actions ActionList) loadAttributes(ctx context.Context) error {
  85. userMap, err := actions.loadUsers(ctx)
  86. if err != nil {
  87. return err
  88. }
  89. if err := actions.loadRepositories(ctx); err != nil {
  90. return err
  91. }
  92. return actions.loadRepoOwner(ctx, userMap)
  93. }