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"
|
2024-09-12 02:28:52 +00:00
|
|
|
"code.nonshy.com/nonshy/website/pkg/log"
|
2022-08-26 04:21:46 +00:00
|
|
|
"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")
|
2022-09-08 04:18:54 +00:00
|
|
|
|
|
|
|
// Whitelist for ordering options.
|
|
|
|
var sortWhitelist = []string{
|
|
|
|
"created_at desc",
|
|
|
|
"created_at asc",
|
2024-09-22 00:25:36 +00:00
|
|
|
"like_count desc",
|
|
|
|
"comment_count desc",
|
2024-09-26 05:46:33 +00:00
|
|
|
"views desc",
|
2022-09-08 04:18:54 +00:00
|
|
|
}
|
|
|
|
|
2022-08-13 22:39:31 +00:00
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Query params.
|
|
|
|
var (
|
|
|
|
viewStyle = r.FormValue("view") // cards (default), full
|
2022-09-08 04:18:54 +00:00
|
|
|
|
|
|
|
// Search filters.
|
2023-10-23 02:17:49 +00:00
|
|
|
who = r.FormValue("who")
|
2022-09-08 04:18:54 +00:00
|
|
|
filterExplicit = r.FormValue("explicit")
|
|
|
|
filterVisibility = r.FormValue("visibility")
|
2022-09-10 03:11:50 +00:00
|
|
|
adminView = r.FormValue("admin_view") == "true"
|
2022-09-08 04:18:54 +00:00
|
|
|
sort = r.FormValue("sort")
|
|
|
|
sortOK bool
|
2022-08-13 22:39:31 +00:00
|
|
|
)
|
2022-09-08 04:18:54 +00:00
|
|
|
|
|
|
|
// Sort options.
|
|
|
|
for _, v := range sortWhitelist {
|
|
|
|
if sort == v {
|
|
|
|
sortOK = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !sortOK {
|
2023-10-22 23:03:17 +00:00
|
|
|
sort = sortWhitelist[0]
|
2022-09-08 04:18:54 +00:00
|
|
|
}
|
|
|
|
|
2022-08-13 22:39:31 +00:00
|
|
|
// Load the current user.
|
|
|
|
currentUser, err := session.CurrentUser(r)
|
|
|
|
if err != nil {
|
|
|
|
session.FlashError(w, r, "Unexpected error: couldn't get CurrentUser")
|
|
|
|
}
|
|
|
|
|
2023-02-14 06:19:18 +00:00
|
|
|
// Is the current viewer shy?
|
2023-10-23 02:57:18 +00:00
|
|
|
var (
|
|
|
|
isShy = currentUser.IsShy()
|
|
|
|
myFriendCount = models.CountFriends(currentUser.ID)
|
|
|
|
)
|
|
|
|
|
|
|
|
// Defaults.
|
|
|
|
if viewStyle != "full" {
|
|
|
|
viewStyle = "cards"
|
|
|
|
}
|
2023-11-24 19:37:01 +00:00
|
|
|
if who == "" {
|
|
|
|
// They didn't post a "Whose photos" filter, restore it from their last saved default.
|
|
|
|
who = currentUser.GetProfileField("site_gallery_default")
|
|
|
|
}
|
2024-07-26 05:39:11 +00:00
|
|
|
if who != "friends" && who != "everybody" && who != "friends+private" && who != "likes" && who != "uncertified" {
|
2023-10-23 02:57:18 +00:00
|
|
|
// Default Who setting should be Friends-only, unless you have no friends.
|
|
|
|
if myFriendCount > 0 {
|
|
|
|
who = "friends"
|
|
|
|
} else {
|
|
|
|
who = "everybody"
|
|
|
|
}
|
2024-07-26 05:39:11 +00:00
|
|
|
|
|
|
|
// Admin only who option.
|
|
|
|
if who == "uncertified" && !currentUser.HasAdminScope(config.ScopePhotoModerator) {
|
|
|
|
who = "friends"
|
|
|
|
}
|
2023-10-23 02:57:18 +00:00
|
|
|
}
|
2023-02-14 06:19:18 +00:00
|
|
|
|
2023-11-24 19:37:01 +00:00
|
|
|
// Store their "Whose photos" filter on their page to default it for next time.
|
|
|
|
currentUser.SetProfileField("site_gallery_default", who)
|
|
|
|
|
2023-08-02 03:39:48 +00:00
|
|
|
// Admin scope warning.
|
|
|
|
if adminView && !currentUser.HasAdminScope(config.ScopePhotoModerator) {
|
|
|
|
session.FlashError(w, r, "Missing admin scope: %s", config.ScopePhotoModerator)
|
|
|
|
}
|
|
|
|
|
2022-08-13 22:39:31 +00:00
|
|
|
// Get the page of photos.
|
|
|
|
pager := &models.Pagination{
|
|
|
|
Page: 1,
|
2022-08-14 05:44:57 +00:00
|
|
|
PerPage: config.PageSizeSiteGallery,
|
2022-09-08 04:18:54 +00:00
|
|
|
Sort: sort,
|
2022-08-13 22:39:31 +00:00
|
|
|
}
|
|
|
|
pager.ParsePage(r)
|
2023-02-14 06:19:18 +00:00
|
|
|
photos, _ := models.PaginateGalleryPhotos(currentUser, models.Gallery{
|
2023-10-23 02:17:49 +00:00
|
|
|
Explicit: filterExplicit,
|
|
|
|
Visibility: filterVisibility,
|
|
|
|
AdminView: adminView,
|
2023-10-26 03:07:28 +00:00
|
|
|
FriendsOnly: who == "friends",
|
|
|
|
IsShy: isShy || who == "friends+private",
|
2024-04-28 02:06:17 +00:00
|
|
|
MyLikes: who == "likes",
|
2024-07-26 05:39:11 +00:00
|
|
|
Uncertified: who == "uncertified",
|
2023-02-14 06:19:18 +00:00
|
|
|
}, 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)
|
|
|
|
}
|
2022-09-09 04:42:20 +00:00
|
|
|
userMap, err := models.MapUsers(currentUser, userIDs)
|
2022-08-13 22:39:31 +00:00
|
|
|
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-27 02:50:33 +00:00
|
|
|
commentMap := models.MapCommentCounts("photos", photoIDs)
|
2022-08-25 04:17:34 +00:00
|
|
|
|
2024-09-12 02:28:52 +00:00
|
|
|
// Ping this user as having used the forums today.
|
|
|
|
go func() {
|
|
|
|
if err := models.LogDailyGalleryUser(currentUser); err != nil {
|
|
|
|
log.Error("LogDailyGalleryUser(%s): error logging their usage statistic: %s", currentUser.Username, err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
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-27 02:50:33 +00:00
|
|
|
"CommentMap": commentMap,
|
2022-08-13 22:39:31 +00:00
|
|
|
"Pager": pager,
|
|
|
|
"ViewStyle": viewStyle,
|
2022-09-08 04:18:54 +00:00
|
|
|
|
|
|
|
// Search filters
|
|
|
|
"Sort": sort,
|
2023-10-23 02:17:49 +00:00
|
|
|
"FilterWho": who,
|
2022-09-08 04:18:54 +00:00
|
|
|
"FilterExplicit": filterExplicit,
|
|
|
|
"FilterVisibility": filterVisibility,
|
2022-09-10 03:11:50 +00:00
|
|
|
"AdminView": adminView,
|
2023-02-14 06:19:18 +00:00
|
|
|
|
|
|
|
// Is the current user shy?
|
|
|
|
"IsShyUser": isShy,
|
2022-08-13 22:39:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := tmpl.Execute(w, r, vars); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|