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"
|
|
|
|
"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")
|
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
|
|
|
|
)
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
var isOwnPhotos = currentUser.ID == user.ID
|
|
|
|
|
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?
|
|
|
|
var (
|
|
|
|
areFriends = models.AreFriends(user.ID, currentUser.ID)
|
|
|
|
isPrivate = user.Visibility == models.UserVisibilityPrivate && !areFriends
|
|
|
|
)
|
|
|
|
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}
|
2022-09-08 04:18:54 +00:00
|
|
|
if isOwnPhotos || isGrantee || currentUser.IsAdmin {
|
2022-08-13 06:11:36 +00:00
|
|
|
visibility = append(visibility, models.PhotoFriends, models.PhotoPrivate)
|
2022-08-14 05:44:57 +00:00
|
|
|
} else if models.AreFriends(user.ID, currentUser.ID) {
|
|
|
|
visibility = append(visibility, models.PhotoFriends)
|
2022-08-13 06:11:36 +00:00
|
|
|
}
|
|
|
|
|
2022-08-13 22:39:31 +00:00
|
|
|
// Explicit photo filter?
|
|
|
|
explicit := currentUser.Explicit
|
|
|
|
if isOwnPhotos {
|
|
|
|
explicit = true
|
|
|
|
}
|
|
|
|
|
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,
|
2022-08-13 06:11:36 +00:00
|
|
|
Sort: "created_at desc",
|
|
|
|
}
|
|
|
|
pager.ParsePage(r)
|
2022-08-13 22:39:31 +00:00
|
|
|
photos, err := models.PaginateUserPhotos(user.ID, visibility, explicit, pager)
|
|
|
|
|
|
|
|
// Get the count of explicit photos if we are not viewing explicit photos.
|
|
|
|
var explicitCount int64
|
|
|
|
if !explicit {
|
|
|
|
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{}{
|
2022-09-08 04:18:54 +00:00
|
|
|
"IsOwnPhotos": currentUser.ID == user.ID,
|
|
|
|
"IsMyPrivateUnlockedFor": isGranted, // have WE granted THIS USER to see our private pics?
|
2022-12-25 07:00:59 +00:00
|
|
|
"AreWeGrantedPrivate": isGrantee, // have THEY granted US private photo access.
|
2022-09-08 04:18:54 +00:00
|
|
|
"User": user,
|
|
|
|
"Photos": photos,
|
|
|
|
"PhotoCount": models.CountPhotos(user.ID),
|
|
|
|
"Pager": pager,
|
|
|
|
"LikeMap": likeMap,
|
|
|
|
"CommentMap": commentMap,
|
|
|
|
"ViewStyle": viewStyle,
|
|
|
|
"ExplicitCount": explicitCount,
|
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
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|