|
| 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 | + |
| 5 | +package models |
| 6 | + |
| 7 | +import "fmt" |
| 8 | + |
| 9 | +// ActionList defines a list of actions |
| 10 | +type ActionList []*Action |
| 11 | + |
| 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 | + |
| 22 | +func (actions ActionList) loadUsers(e Engine) ([]*User, error) { |
| 23 | + if len(actions) == 0 { |
| 24 | + return nil, nil |
| 25 | + } |
| 26 | + |
| 27 | + userIDs := actions.getUserIDs() |
| 28 | + userMaps := make(map[int64]*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 | + |
| 36 | + for _, action := range actions { |
| 37 | + action.ActUser = userMaps[action.ActUserID] |
| 38 | + } |
| 39 | + return valuesUser(userMaps), nil |
| 40 | +} |
| 41 | + |
| 42 | +// LoadUsers loads actions' all users |
| 43 | +func (actions ActionList) LoadUsers() ([]*User, error) { |
| 44 | + return actions.loadUsers(x) |
| 45 | +} |
| 46 | + |
| 47 | +func (actions ActionList) getRepoIDs() []int64 { |
| 48 | + repoIDs := make(map[int64]struct{}, len(actions)) |
| 49 | + for _, action := range actions { |
| 50 | + if _, ok := repoIDs[action.RepoID]; !ok { |
| 51 | + repoIDs[action.RepoID] = struct{}{} |
| 52 | + } |
| 53 | + } |
| 54 | + return keysInt64(repoIDs) |
| 55 | +} |
| 56 | + |
| 57 | +func (actions ActionList) loadRepositories(e Engine) ([]*Repository, error) { |
| 58 | + if len(actions) == 0 { |
| 59 | + return nil, nil |
| 60 | + } |
| 61 | + |
| 62 | + repoIDs := actions.getRepoIDs() |
| 63 | + repoMaps := make(map[int64]*Repository, len(repoIDs)) |
| 64 | + err := e. |
| 65 | + In("id", repoIDs). |
| 66 | + Find(&repoMaps) |
| 67 | + if err != nil { |
| 68 | + return nil, fmt.Errorf("find repository: %v", err) |
| 69 | + } |
| 70 | + |
| 71 | + for _, action := range actions { |
| 72 | + action.Repo = repoMaps[action.RepoID] |
| 73 | + } |
| 74 | + return valuesRepository(repoMaps), nil |
| 75 | +} |
| 76 | + |
| 77 | +// LoadRepositories loads actions' all repositories |
| 78 | +func (actions ActionList) LoadRepositories() ([]*Repository, error) { |
| 79 | + return actions.loadRepositories(x) |
| 80 | +} |
| 81 | + |
| 82 | +// loadAttributes loads all attributes |
| 83 | +func (actions ActionList) loadAttributes(e Engine) (err error) { |
| 84 | + if _, err = actions.loadUsers(e); err != nil { |
| 85 | + return |
| 86 | + } |
| 87 | + |
| 88 | + if _, err = actions.loadRepositories(e); err != nil { |
| 89 | + return |
| 90 | + } |
| 91 | + |
| 92 | + return nil |
| 93 | +} |
| 94 | + |
| 95 | +// LoadAttributes loads attributes of the actions |
| 96 | +func (actions ActionList) LoadAttributes() error { |
| 97 | + return actions.loadAttributes(x) |
| 98 | +} |
0 commit comments