Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

action_list.go 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. 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(map[int64]struct{}, len(actions))
  16. for _, action := range actions {
  17. if _, ok := userIDs[action.ActUserID]; !ok {
  18. userIDs[action.ActUserID] = struct{}{}
  19. }
  20. }
  21. return container.KeysInt64(userIDs)
  22. }
  23. func (actions ActionList) loadUsers(e db.Engine) (map[int64]*user_model.User, error) {
  24. if len(actions) == 0 {
  25. return nil, nil
  26. }
  27. userIDs := actions.getUserIDs()
  28. userMaps := make(map[int64]*user_model.User, len(userIDs))
  29. err := e.
  30. In("id", userIDs).
  31. Find(&userMaps)
  32. if err != nil {
  33. return nil, fmt.Errorf("find user: %v", err)
  34. }
  35. for _, action := range actions {
  36. action.ActUser = userMaps[action.ActUserID]
  37. }
  38. return userMaps, nil
  39. }
  40. func (actions ActionList) getRepoIDs() []int64 {
  41. repoIDs := make(map[int64]struct{}, len(actions))
  42. for _, action := range actions {
  43. if _, ok := repoIDs[action.RepoID]; !ok {
  44. repoIDs[action.RepoID] = struct{}{}
  45. }
  46. }
  47. return container.KeysInt64(repoIDs)
  48. }
  49. func (actions ActionList) loadRepositories(e db.Engine) error {
  50. if len(actions) == 0 {
  51. return nil
  52. }
  53. repoIDs := actions.getRepoIDs()
  54. repoMaps := make(map[int64]*repo_model.Repository, len(repoIDs))
  55. err := e.In("id", repoIDs).Find(&repoMaps)
  56. if err != nil {
  57. return fmt.Errorf("find repository: %v", err)
  58. }
  59. for _, action := range actions {
  60. action.Repo = repoMaps[action.RepoID]
  61. }
  62. return nil
  63. }
  64. func (actions ActionList) loadRepoOwner(e db.Engine, userMap map[int64]*user_model.User) (err error) {
  65. if userMap == nil {
  66. userMap = make(map[int64]*user_model.User)
  67. }
  68. for _, action := range actions {
  69. if action.Repo == nil {
  70. continue
  71. }
  72. repoOwner, ok := userMap[action.Repo.OwnerID]
  73. if !ok {
  74. repoOwner, err = user_model.GetUserByID(action.Repo.OwnerID)
  75. if err != nil {
  76. if user_model.IsErrUserNotExist(err) {
  77. continue
  78. }
  79. return err
  80. }
  81. userMap[repoOwner.ID] = repoOwner
  82. }
  83. action.Repo.Owner = repoOwner
  84. }
  85. return nil
  86. }
  87. // loadAttributes loads all attributes
  88. func (actions ActionList) loadAttributes(e db.Engine) error {
  89. userMap, err := actions.loadUsers(e)
  90. if err != nil {
  91. return err
  92. }
  93. if err := actions.loadRepositories(e); err != nil {
  94. return err
  95. }
  96. return actions.loadRepoOwner(e, userMap)
  97. }