2022-08-13 06:11:36 +00:00
|
|
|
package photo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2022-08-26 04:21:46 +00:00
|
|
|
"code.nonshy.com/nonshy/website/pkg/config"
|
2023-05-07 20:16:22 +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 06:11:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// UserPhotos controller (/photo/u/:username) to view a user's gallery or manage if it's yourself.
|
|
|
|
func UserPhotos() http.HandlerFunc {
|
2022-08-13 22:39:31 +00:00
|
|
|
tmpl := templates.Must("photo/gallery.html")
|
2023-10-22 23:03:17 +00:00
|
|
|
|
|
|
|
// Whitelist for ordering options.
|
|
|
|
var sortWhitelist = []string{
|
2024-07-07 21:00:58 +00:00
|
|
|
"pinned desc nulls last, updated_at desc",
|
2023-10-22 23:03:17 +00:00
|
|
|
"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",
|
2023-10-22 23:03:17 +00:00
|
|
|
}
|
|
|
|
|
2022-08-13 06:11:36 +00:00
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Query params.
|
|
|
|
var (
|
2024-02-11 00:17:15 +00:00
|
|
|
username = r.PathValue("username")
|
2022-08-13 06:11:36 +00:00
|
|
|
viewStyle = r.FormValue("view") // cards (default), full
|
2023-10-22 23:03:17 +00:00
|
|
|
|
|
|
|
// Search filters.
|
|
|
|
filterExplicit = r.FormValue("explicit")
|
|
|
|
filterVisibility = r.FormValue("visibility")
|
|
|
|
sort = r.FormValue("sort")
|
|
|
|
sortOK bool
|
2022-08-13 06:11:36 +00:00
|
|
|
)
|
2023-10-22 23:03:17 +00:00
|
|
|
|
|
|
|
// Sort options.
|
|
|
|
for _, v := range sortWhitelist {
|
|
|
|
if sort == v {
|
|
|
|
sortOK = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !sortOK {
|
|
|
|
sort = sortWhitelist[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Defaults.
|
2022-08-13 06:11:36 +00:00
|
|
|
if viewStyle != "full" {
|
|
|
|
viewStyle = "cards"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find this user.
|
|
|
|
user, err := models.FindUser(username)
|
|
|
|
if err != nil {
|
|
|
|
templates.NotFoundPage(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load the current user in case they are viewing their own page.
|
|
|
|
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
|
|
|
var (
|
2023-10-22 23:03:17 +00:00
|
|
|
areFriends = models.AreFriends(user.ID, currentUser.ID)
|
|
|
|
isPrivate = user.Visibility == models.UserVisibilityPrivate && !areFriends
|
2023-02-14 06:19:18 +00:00
|
|
|
isOwnPhotos = currentUser.ID == user.ID
|
|
|
|
isShy = currentUser.IsShy()
|
2023-10-22 23:03:17 +00:00
|
|
|
isShyFrom = !isOwnPhotos && (currentUser.IsShyFrom(user) || (isShy && !areFriends))
|
2023-02-14 06:19:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Bail early if we are shy from this user.
|
|
|
|
if isShy && isShyFrom {
|
|
|
|
var vars = map[string]interface{}{
|
|
|
|
"IsOwnPhotos": currentUser.ID == user.ID,
|
|
|
|
"IsShyUser": isShy,
|
|
|
|
"IsShyFrom": isShyFrom,
|
|
|
|
// "IsMyPrivateUnlockedFor": isGranted, // have WE granted THIS USER to see our private pics?
|
|
|
|
// "AreWeGrantedPrivate": isGrantee, // have THEY granted US private photo access.
|
|
|
|
"User": user,
|
|
|
|
"Photos": []*models.Photo{},
|
|
|
|
"PhotoCount": models.CountPhotos(user.ID),
|
2023-08-16 00:33:33 +00:00
|
|
|
"Pager": &models.Pagination{},
|
2023-02-14 06:19:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := tmpl.Execute(w, r, vars); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2022-08-13 06:11:36 +00:00
|
|
|
|
2022-08-15 00:45:55 +00:00
|
|
|
// Is either one blocking?
|
|
|
|
if models.IsBlocking(currentUser.ID, user.ID) && !currentUser.IsAdmin {
|
|
|
|
templates.NotFoundPage(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-22 00:29:39 +00:00
|
|
|
// Is this user private and we're not friends?
|
|
|
|
if isPrivate && !currentUser.IsAdmin && !isOwnPhotos {
|
|
|
|
session.FlashError(w, r, "This user's profile page and photo gallery are private.")
|
|
|
|
templates.Redirect(w, "/u/"+user.Username)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-08 04:18:54 +00:00
|
|
|
// Has this user granted access to see their privates?
|
|
|
|
var (
|
|
|
|
isGrantee = models.IsPrivateUnlocked(user.ID, currentUser.ID) // THEY have granted US access
|
|
|
|
isGranted = models.IsPrivateUnlocked(currentUser.ID, user.ID) // WE have granted THEM access
|
|
|
|
)
|
|
|
|
|
2022-08-13 06:11:36 +00:00
|
|
|
// What set of visibilities to query?
|
|
|
|
visibility := []models.PhotoVisibility{models.PhotoPublic}
|
2023-08-02 03:39:48 +00:00
|
|
|
if isOwnPhotos || isGrantee || currentUser.HasAdminScope(config.ScopePhotoModerator) {
|
2023-08-15 01:50:34 +00:00
|
|
|
visibility = append(visibility, models.PhotoPrivate)
|
|
|
|
}
|
2023-10-22 23:03:17 +00:00
|
|
|
if isOwnPhotos || areFriends || currentUser.HasAdminScope(config.ScopePhotoModerator) {
|
2022-08-14 05:44:57 +00:00
|
|
|
visibility = append(visibility, models.PhotoFriends)
|
2022-08-13 06:11:36 +00:00
|
|
|
}
|
|
|
|
|
2023-10-22 23:03:17 +00:00
|
|
|
// If we are Filtering by Visibility, ensure the target visibility is accessible to us.
|
|
|
|
if filterVisibility != "" {
|
|
|
|
var isOK bool
|
|
|
|
for _, allowed := range visibility {
|
|
|
|
if allowed == models.PhotoVisibility(filterVisibility) {
|
|
|
|
isOK = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the filter is within the set we are allowed to see, update the set.
|
|
|
|
if isOK {
|
|
|
|
visibility = []models.PhotoVisibility{models.PhotoVisibility(filterVisibility)}
|
|
|
|
} else {
|
|
|
|
session.FlashError(w, r, "Could not filter pictures by that visibility setting: it is not available for you.")
|
|
|
|
visibility = []models.PhotoVisibility{models.PhotoNotAvailable}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Explicit photo filter? The default ("") will defer to the user's Explicit opt-in.
|
|
|
|
if filterExplicit == "" {
|
|
|
|
// If the viewer does not opt-in to explicit AND is not looking at their own gallery,
|
|
|
|
// then default the explicit filter to "do not show explicit"
|
|
|
|
if !currentUser.Explicit && !isOwnPhotos {
|
|
|
|
filterExplicit = "false"
|
|
|
|
}
|
2022-08-13 22:39:31 +00:00
|
|
|
}
|
|
|
|
|
2022-08-13 06:11:36 +00:00
|
|
|
// Get the page of photos.
|
|
|
|
pager := &models.Pagination{
|
|
|
|
Page: 1,
|
2022-08-14 05:44:57 +00:00
|
|
|
PerPage: config.PageSizeUserGallery,
|
2023-10-22 23:03:17 +00:00
|
|
|
Sort: sort,
|
2022-08-13 06:11:36 +00:00
|
|
|
}
|
|
|
|
pager.ParsePage(r)
|
2023-10-22 23:03:17 +00:00
|
|
|
photos, err := models.PaginateUserPhotos(user.ID, models.UserGallery{
|
|
|
|
Explicit: filterExplicit,
|
|
|
|
Visibility: visibility,
|
|
|
|
}, pager)
|
2023-05-07 20:16:22 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("PaginateUserPhotos(%s): %s", user.Username, err)
|
|
|
|
}
|
2022-08-13 22:39:31 +00:00
|
|
|
|
|
|
|
// Get the count of explicit photos if we are not viewing explicit photos.
|
|
|
|
var explicitCount int64
|
2023-10-22 23:03:17 +00:00
|
|
|
if filterExplicit == "false" {
|
2022-08-13 22:39:31 +00:00
|
|
|
explicitCount, _ = models.CountExplicitPhotos(user.ID, visibility)
|
|
|
|
}
|
2022-08-13 06:11:36 +00:00
|
|
|
|
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-01-07 22:20:01 +00:00
|
|
|
// Can we see their default profile picture? If no: show a hint on the Gallery page that
|
|
|
|
// their default pic isn't visible.
|
|
|
|
var profilePictureHidden models.PhotoVisibility
|
|
|
|
if ok, visibility := user.CanSeeProfilePicture(currentUser); !ok && visibility != models.PhotoPublic {
|
|
|
|
profilePictureHidden = visibility
|
|
|
|
}
|
|
|
|
|
2024-08-08 06:05:23 +00:00
|
|
|
// Friend Photos Notification Opt-out:
|
|
|
|
// If your friend posts too many photos and you want to mute them.
|
|
|
|
// NOTE: notifications are "on by default" and only an explicit "false"
|
|
|
|
// stored in the database indicates an opt-out.
|
|
|
|
// New photo upload notification subscription status.
|
|
|
|
var areNotificationsMuted bool
|
|
|
|
if exists, v := models.IsSubscribed(currentUser, "friend.photos", user.ID); exists {
|
|
|
|
areNotificationsMuted = !v
|
|
|
|
}
|
|
|
|
|
2022-08-13 06:11:36 +00:00
|
|
|
var vars = map[string]interface{}{
|
2023-05-24 18:27:42 +00:00
|
|
|
"IsOwnPhotos": currentUser.ID == user.ID,
|
|
|
|
"IsShyUser": isShy,
|
|
|
|
"IsShyFrom": isShyFrom,
|
|
|
|
"IsMyPrivateUnlockedFor": isGranted, // have WE granted THIS USER to see our private pics?
|
|
|
|
"AreWeGrantedPrivate": isGrantee, // have THEY granted US private photo access.
|
2023-10-22 23:03:17 +00:00
|
|
|
"AreFriends": areFriends,
|
2024-08-08 06:05:23 +00:00
|
|
|
"AreNotificationsMuted": areNotificationsMuted,
|
2024-01-07 22:20:01 +00:00
|
|
|
"ProfilePictureHiddenVisibility": profilePictureHidden,
|
2023-05-24 18:27:42 +00:00
|
|
|
"User": user,
|
|
|
|
"Photos": photos,
|
2023-08-15 01:50:34 +00:00
|
|
|
"PhotoCount": models.CountPhotosICanSee(user, currentUser),
|
2023-09-16 20:46:26 +00:00
|
|
|
"NoteCount": models.CountNotesAboutUser(currentUser, user),
|
2023-10-22 22:02:24 +00:00
|
|
|
"FriendCount": models.CountFriends(user.ID),
|
2023-05-24 18:27:42 +00:00
|
|
|
"PublicPhotoCount": models.CountPublicPhotos(user.ID),
|
|
|
|
"Pager": pager,
|
|
|
|
"LikeMap": likeMap,
|
|
|
|
"CommentMap": commentMap,
|
|
|
|
"ViewStyle": viewStyle,
|
|
|
|
"ExplicitCount": explicitCount,
|
2023-10-22 23:03:17 +00:00
|
|
|
|
|
|
|
// Search filters
|
|
|
|
"Sort": sort,
|
|
|
|
"FilterExplicit": filterExplicit,
|
|
|
|
"FilterVisibility": filterVisibility,
|
2022-08-13 06:11:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := tmpl.Execute(w, r, vars); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|