diff --git a/controllers/user/profile.go b/controllers/user/profile.go index d8f28714..eac2857d 100644 --- a/controllers/user/profile.go +++ b/controllers/user/profile.go @@ -55,17 +55,16 @@ func UserProfileHandler(c *gin.Context) { c.Request.URL.RawQuery = query.Encode() var torrents []models.Torrent var err error - nbTorrents := 0 + _, userProfile.Torrents, _, err = search.ByQuery(c, 1) if currentUser.CurrentOrAdmin(userProfile.ID) { - _, torrents, nbTorrents, err = search.ByQuery(c, 1) + userProfile.Splice(1, 20) } else { - _, torrents, nbTorrents, err = search.ByQueryNoHidden(c, 1) + userProfile.Splice(1, 20).Filter() } if err != nil { messages.AddErrorT("errors", "retrieve_torrents_error") } - userProfile.Torrents = torrents - templates.UserProfile(c, userProfile, nbTorrents) + templates.UserProfile(c, userProfile) } } else { c.Status(http.StatusNotFound) diff --git a/models/user.go b/models/user.go index cb521fc0..4bf3e249 100644 --- a/models/user.go +++ b/models/user.go @@ -362,3 +362,31 @@ func (u *User) Delete(currentUser *User) (int, error) { func (u *User) toMap() map[string]interface{} { return structs.Map(u) } + +// Splice : get a subset of torrents +func (u *User) Splice(start int, length int) *User { + if (len(u.Torrents) <= length && start == 0) || len(u.Torrents) == 0 { + return u + } + if start > len(u.Torrents) { + u.Torrents = []Torrent{} + return u + } + if len(u.Torrents) < length { + length = len(u.Torrents) + } + u.Torrents = u.Torrents[start:length] + return u +} + +// Filter : filter the hidden torrents +func (u *User) Filter() *User { + torrents := []Torrent{} + for _, t := range u.Torrents { + if !t.Hidden { + torrents = append(torrents, t) + } + } + u.Torrents = torrents + return u +} diff --git a/templates/layouts/partials/menu/profile.jet.html b/templates/layouts/partials/menu/profile.jet.html index 45ae33ca..95cd5054 100644 --- a/templates/layouts/partials/menu/profile.jet.html +++ b/templates/layouts/partials/menu/profile.jet.html @@ -13,7 +13,7 @@
{{UserProfile.GetRole()}}
-{{ T("torrents_uploaded") }}:{{ NbTorrents }}
+{{ T("torrents_uploaded") }}:{{ len(UserProfile.Torrents) }}
diff --git a/templates/template.go b/templates/template.go index 7b88dde9..afbeaa5f 100644 --- a/templates/template.go +++ b/templates/template.go @@ -167,7 +167,6 @@ func UserProfileEdit(c *gin.Context, userProfile *models.User, userForm userVali func UserProfile(c *gin.Context, userProfile *models.User, nbTorrents int) { variables := Commonvariables(c) variables.Set("UserProfile", userProfile) - variables.Set("NbTorrents", nbTorrents) Render(c, path.Join(SiteDir, "user/torrents.jet.html"), variables) } diff --git a/templates/template_test.go b/templates/template_test.go index f14a335c..a5afc261 100644 --- a/templates/template_test.go +++ b/templates/template_test.go @@ -211,7 +211,6 @@ func mockupCommonvariables(t *testing.T) jet.VarMap { variables.Set("Infos", make(map[string][]string)) variables.Set("Errors", make(map[string][]string)) variables.Set("UserProfile", &models.User{}) - variables.Set("NbTorrents", 0) variables = templateFunctions(variables) return variables }