@@ -25,7 +25,7 @@ func (actions ActionList) getUserIDs() []int64 {
25
25
return keysInt64 (userIDs )
26
26
}
27
27
28
- func (actions ActionList ) loadUsers (e db.Engine ) ([ ]* user_model.User , error ) {
28
+ func (actions ActionList ) loadUsers (e db.Engine ) (map [ int64 ]* user_model.User , error ) {
29
29
if len (actions ) == 0 {
30
30
return nil , nil
31
31
}
@@ -42,7 +42,7 @@ func (actions ActionList) loadUsers(e db.Engine) ([]*user_model.User, error) {
42
42
for _ , action := range actions {
43
43
action .ActUser = userMaps [action .ActUserID ]
44
44
}
45
- return valuesUser ( userMaps ) , nil
45
+ return userMaps , nil
46
46
}
47
47
48
48
func (actions ActionList ) getRepoIDs () []int64 {
@@ -55,35 +55,57 @@ func (actions ActionList) getRepoIDs() []int64 {
55
55
return keysInt64 (repoIDs )
56
56
}
57
57
58
- func (actions ActionList ) loadRepositories (e db.Engine ) ([] * repo_model. Repository , error ) {
58
+ func (actions ActionList ) loadRepositories (e db.Engine ) error {
59
59
if len (actions ) == 0 {
60
- return nil , nil
60
+ return nil
61
61
}
62
62
63
63
repoIDs := actions .getRepoIDs ()
64
64
repoMaps := make (map [int64 ]* repo_model.Repository , len (repoIDs ))
65
- err := e .
66
- In ("id" , repoIDs ).
67
- Find (& repoMaps )
65
+ err := e .In ("id" , repoIDs ).Find (& repoMaps )
68
66
if err != nil {
69
- return nil , fmt .Errorf ("find repository: %v" , err )
67
+ return fmt .Errorf ("find repository: %v" , err )
70
68
}
71
69
72
70
for _ , action := range actions {
73
71
action .Repo = repoMaps [action .RepoID ]
74
72
}
75
- return valuesRepository ( repoMaps ), nil
73
+ return nil
76
74
}
77
75
78
- // loadAttributes loads all attributes
79
- func (actions ActionList ) loadAttributes (e db.Engine ) (err error ) {
80
- if _ , err = actions .loadUsers (e ); err != nil {
81
- return
76
+ func (actions ActionList ) loadRepoOwner (e db.Engine , userMap map [int64 ]* user_model.User ) (err error ) {
77
+ if userMap == nil {
78
+ userMap = make (map [int64 ]* user_model.User )
82
79
}
83
80
84
- if _ , err = actions .loadRepositories (e ); err != nil {
85
- return
81
+ for _ , action := range actions {
82
+ repoOwner , ok := userMap [action .Repo .OwnerID ]
83
+ if ! ok {
84
+ repoOwner , err = user_model .GetUserByID (action .Repo .OwnerID )
85
+ if err != nil {
86
+ if user_model .IsErrUserNotExist (err ) {
87
+ continue
88
+ }
89
+ return err
90
+ }
91
+ userMap [repoOwner .ID ] = repoOwner
92
+ }
93
+ action .Repo .Owner = repoOwner
86
94
}
87
95
88
96
return nil
89
97
}
98
+
99
+ // loadAttributes loads all attributes
100
+ func (actions ActionList ) loadAttributes (e db.Engine ) error {
101
+ userMap , err := actions .loadUsers (e )
102
+ if err != nil {
103
+ return err
104
+ }
105
+
106
+ if err := actions .loadRepositories (e ); err != nil {
107
+ return err
108
+ }
109
+
110
+ return actions .loadRepoOwner (e , userMap )
111
+ }
0 commit comments