diff --git a/pkg/controller/photo/site_gallery.go b/pkg/controller/photo/site_gallery.go
index 13171fc..a387afc 100644
--- a/pkg/controller/photo/site_gallery.go
+++ b/pkg/controller/photo/site_gallery.go
@@ -18,6 +18,10 @@ func SiteGallery() http.HandlerFunc {
var sortWhitelist = []string{
"created_at desc",
"created_at asc",
+
+ // Custom (advanced) sort options.
+ "by_likes",
+ "by_comments",
}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
diff --git a/pkg/controller/photo/user_gallery.go b/pkg/controller/photo/user_gallery.go
index a9b6962..b73786e 100644
--- a/pkg/controller/photo/user_gallery.go
+++ b/pkg/controller/photo/user_gallery.go
@@ -19,6 +19,10 @@ func UserPhotos() http.HandlerFunc {
"pinned desc nulls last, updated_at desc",
"created_at desc",
"created_at asc",
+
+ // Custom (advanced) sort options.
+ "by_likes",
+ "by_comments",
}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
diff --git a/pkg/models/photo.go b/pkg/models/photo.go
index ee422db..e510eef 100644
--- a/pkg/models/photo.go
+++ b/pkg/models/photo.go
@@ -135,6 +135,24 @@ func PaginateUserPhotos(userID uint64, conf UserGallery, pager *Pagination) ([]*
placeholders = append(placeholders, explicit[0])
}
+ // Custom SORT parameters.
+ switch pager.Sort {
+ case "by_likes":
+ pager.Sort = `(
+ SELECT count(likes.id)
+ FROM likes
+ WHERE likes.table_name = 'photos'
+ AND likes.table_id = photos.id
+ ) DESC`
+ case "by_comments":
+ pager.Sort = `(
+ SELECT count(comments.id)
+ FROM comments
+ WHERE comments.table_name = 'photos'
+ AND comments.table_id = photos.id
+ ) DESC NULLS LAST`
+ }
+
query := DB.Where(
strings.Join(wheres, " AND "),
placeholders...,
@@ -672,6 +690,24 @@ func PaginateGalleryPhotos(user *User, conf Gallery, pager *Pagination) ([]*Phot
)
}
+ // Custom SORT parameters.
+ switch pager.Sort {
+ case "by_likes":
+ pager.Sort = `(
+ SELECT count(likes.id)
+ FROM likes
+ WHERE likes.table_name = 'photos'
+ AND likes.table_id = photos.id
+ ) DESC`
+ case "by_comments":
+ pager.Sort = `(
+ SELECT count(comments.id)
+ FROM comments
+ WHERE comments.table_name = 'photos'
+ AND comments.table_id = photos.id
+ ) DESC NULLS LAST`
+ }
+
query = query.Order(pager.Sort)
query.Model(&Photo{}).Count(&pager.Total)
diff --git a/web/templates/photo/gallery.html b/web/templates/photo/gallery.html
index e3223fa..708eb73 100644
--- a/web/templates/photo/gallery.html
+++ b/web/templates/photo/gallery.html
@@ -414,6 +414,8 @@
{{end}}
+
+