2022-08-13 22:39:31 +00:00
|
|
|
package photo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2022-08-26 04:21:46 +00:00
|
|
|
"code.nonshy.com/nonshy/website/pkg/config"
|
|
|
|
"code.nonshy.com/nonshy/website/pkg/models"
|
|
|
|
"code.nonshy.com/nonshy/website/pkg/session"
|
|
|
|
"code.nonshy.com/nonshy/website/pkg/templates"
|
2022-08-13 22:39:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// SiteGallery controller (/photo/gallery) to view all members' public gallery pics.
|
|
|
|
func SiteGallery() http.HandlerFunc {
|
|
|
|
tmpl := templates.Must("photo/gallery.html")
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Query params.
|
|
|
|
var (
|
|
|
|
viewStyle = r.FormValue("view") // cards (default), full
|
|
|
|
)
|
|
|
|
if viewStyle != "full" {
|
|
|
|
viewStyle = "cards"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load the current user.
|
|
|
|
currentUser, err := session.CurrentUser(r)
|
|
|
|
if err != nil {
|
|
|
|
session.FlashError(w, r, "Unexpected error: couldn't get CurrentUser")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the page of photos.
|
|
|
|
pager := &models.Pagination{
|
|
|
|
Page: 1,
|
2022-08-14 05:44:57 +00:00
|
|
|
PerPage: config.PageSizeSiteGallery,
|
2022-08-13 22:39:31 +00:00
|
|
|
Sort: "created_at desc",
|
|
|
|
}
|
|
|
|
pager.ParsePage(r)
|
2022-08-15 00:45:55 +00:00
|
|
|
photos, err := models.PaginateGalleryPhotos(currentUser.ID, currentUser.IsAdmin, currentUser.Explicit, pager)
|
2022-08-13 22:39:31 +00:00
|
|
|
|
|
|
|
// Bulk load the users associated with these photos.
|
|
|
|
var userIDs = []uint64{}
|
|
|
|
for _, photo := range photos {
|
|
|
|
userIDs = append(userIDs, photo.UserID)
|
|
|
|
}
|
|
|
|
userMap, err := models.MapUsers(userIDs)
|
|
|
|
if err != nil {
|
|
|
|
session.FlashError(w, r, "Failed to MapUsers: %s", err)
|
|
|
|
}
|
|
|
|
|
2022-08-25 04:17:34 +00:00
|
|
|
// Get Likes information about these photos.
|
|
|
|
var photoIDs = []uint64{}
|
|
|
|
for _, p := range photos {
|
|
|
|
photoIDs = append(photoIDs, p.ID)
|
|
|
|
}
|
|
|
|
likeMap := models.MapLikes(currentUser, "photos", photoIDs)
|
|
|
|
|
2022-08-13 22:39:31 +00:00
|
|
|
var vars = map[string]interface{}{
|
|
|
|
"IsSiteGallery": true,
|
|
|
|
"Photos": photos,
|
|
|
|
"UserMap": userMap,
|
2022-08-25 04:17:34 +00:00
|
|
|
"LikeMap": likeMap,
|
2022-08-13 22:39:31 +00:00
|
|
|
"Pager": pager,
|
|
|
|
"ViewStyle": viewStyle,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := tmpl.Execute(w, r, vars); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|