2022-08-13 06:11:36 +00:00
|
|
|
package photo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"regexp"
|
|
|
|
|
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
|
|
|
)
|
|
|
|
|
|
|
|
var UserPhotosRegexp = regexp.MustCompile(`^/photo/u/([^@]+?)$`)
|
|
|
|
|
|
|
|
// 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{
|
|
|
|
"created_at desc",
|
|
|
|
"created_at asc",
|
|
|
|
}
|
|
|
|
|
2022-08-13 06:11:36 +00:00
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Query params.
|
|
|
|
var (
|
|
|
|
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"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the username out of the URL parameters.
|
|
|
|
var username string
|
|
|
|
m := UserPhotosRegexp.FindStringSubmatch(r.URL.Path)
|
|
|
|
if m != nil {
|
|
|
|
username = m[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-05-24 03:04:17 +00:00
|
|
|
// Inner circle photos.
|
|
|
|
if currentUser.IsInnerCircle() {
|
|
|
|
visibility = append(visibility, models.PhotoInnerCircle)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
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,
|
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),
|
|
|
|
"InnerCircleMinimumPublicPhotos": config.InnerCircleMinimumPublicPhotos,
|
|
|
|
"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
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|