Filters on User Gallery Pages

face-detect
Noah Petherbridge 2023-10-22 16:03:17 -07:00
parent 481bd0ae61
commit 39c825d4ca
6 changed files with 120 additions and 31 deletions

View File

@ -144,7 +144,7 @@ func Share() http.HandlerFunc {
Type: models.NotificationPrivatePhoto,
TableName: "__private_photos",
TableID: currentUser.ID,
Link: fmt.Sprintf("/photo/u/%s", currentUser.Username),
Link: fmt.Sprintf("/photo/u/%s?visibility=private", currentUser.Username),
}
if err := models.CreateNotification(notif); err != nil {
log.Error("Couldn't create PrivatePhoto notification: %s", err)

View File

@ -40,7 +40,7 @@ func SiteGallery() http.HandlerFunc {
}
}
if !sortOK {
sort = "created_at desc"
sort = sortWhitelist[0]
}
// Defaults.

View File

@ -16,11 +16,37 @@ 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 {
tmpl := templates.Must("photo/gallery.html")
// Whitelist for ordering options.
var sortWhitelist = []string{
"created_at desc",
"created_at asc",
}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Query params.
var (
viewStyle = r.FormValue("view") // cards (default), full
// Search filters.
filterExplicit = r.FormValue("explicit")
filterVisibility = r.FormValue("visibility")
sort = r.FormValue("sort")
sortOK bool
)
// Sort options.
for _, v := range sortWhitelist {
if sort == v {
sortOK = true
break
}
}
if !sortOK {
sort = sortWhitelist[0]
}
// Defaults.
if viewStyle != "full" {
viewStyle = "cards"
}
@ -45,9 +71,11 @@ func UserPhotos() http.HandlerFunc {
session.FlashError(w, r, "Unexpected error: couldn't get CurrentUser")
}
var (
areFriends = models.AreFriends(user.ID, currentUser.ID)
isPrivate = user.Visibility == models.UserVisibilityPrivate && !areFriends
isOwnPhotos = currentUser.ID == user.ID
isShy = currentUser.IsShy()
isShyFrom = !isOwnPhotos && (currentUser.IsShyFrom(user) || (isShy && !models.AreFriends(currentUser.ID, user.ID)))
isShyFrom = !isOwnPhotos && (currentUser.IsShyFrom(user) || (isShy && !areFriends))
)
// Bail early if we are shy from this user.
@ -77,10 +105,6 @@ func UserPhotos() http.HandlerFunc {
}
// 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)
@ -98,7 +122,7 @@ func UserPhotos() http.HandlerFunc {
if isOwnPhotos || isGrantee || currentUser.HasAdminScope(config.ScopePhotoModerator) {
visibility = append(visibility, models.PhotoPrivate)
}
if isOwnPhotos || models.AreFriends(user.ID, currentUser.ID) || currentUser.HasAdminScope(config.ScopePhotoModerator) {
if isOwnPhotos || areFriends || currentUser.HasAdminScope(config.ScopePhotoModerator) {
visibility = append(visibility, models.PhotoFriends)
}
@ -107,27 +131,52 @@ func UserPhotos() http.HandlerFunc {
visibility = append(visibility, models.PhotoInnerCircle)
}
// Explicit photo filter?
explicit := currentUser.Explicit
if isOwnPhotos {
explicit = true
// 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"
}
}
// Get the page of photos.
pager := &models.Pagination{
Page: 1,
PerPage: config.PageSizeUserGallery,
Sort: "created_at desc",
Sort: sort,
}
pager.ParsePage(r)
photos, err := models.PaginateUserPhotos(user.ID, visibility, explicit, pager)
photos, err := models.PaginateUserPhotos(user.ID, models.UserGallery{
Explicit: filterExplicit,
Visibility: visibility,
}, pager)
if err != nil {
log.Error("PaginateUserPhotos(%s): %s", user.Username, err)
}
// Get the count of explicit photos if we are not viewing explicit photos.
var explicitCount int64
if !explicit {
if filterExplicit == "false" {
explicitCount, _ = models.CountExplicitPhotos(user.ID, visibility)
}
@ -145,6 +194,7 @@ func UserPhotos() http.HandlerFunc {
"IsShyFrom": isShyFrom,
"IsMyPrivateUnlockedFor": isGranted, // have WE granted THIS USER to see our private pics?
"AreWeGrantedPrivate": isGrantee, // have THEY granted US private photo access.
"AreFriends": areFriends,
"User": user,
"Photos": photos,
"PhotoCount": models.CountPhotosICanSee(user, currentUser),
@ -157,6 +207,11 @@ func UserPhotos() http.HandlerFunc {
"CommentMap": commentMap,
"ViewStyle": viewStyle,
"ExplicitCount": explicitCount,
// Search filters
"Sort": sort,
"FilterExplicit": filterExplicit,
"FilterVisibility": filterVisibility,
}
if err := tmpl.Execute(w, r, vars); err != nil {

View File

@ -61,8 +61,9 @@ func DeleteUserPhotos(userID uint64) error {
for {
photos, err := models.PaginateUserPhotos(
userID,
models.PhotoVisibilityAll,
true,
models.UserGallery{
Visibility: models.PhotoVisibilityAll,
},
pager,
)

View File

@ -34,6 +34,10 @@ const (
PhotoFriends PhotoVisibility = "friends" // only friends can see it
PhotoPrivate PhotoVisibility = "private" // private
PhotoInnerCircle PhotoVisibility = "circle" // inner circle
// Special visibility in case, on User Gallery view, user applies a filter
// for friends-only picture but they are not friends with the user.
PhotoNotAvailable PhotoVisibility = "not_available"
)
// PhotoVisibility preset settings.
@ -103,22 +107,41 @@ func GetPhotos(IDs []uint64) (map[uint64]*Photo, error) {
return mp, result.Error
}
// UserGallery configuration for filtering gallery pages.
type UserGallery struct {
Explicit string // "", "true", "false"
Visibility []PhotoVisibility
}
/*
PaginateUserPhotos gets a page of photos belonging to a user ID.
*/
func PaginateUserPhotos(userID uint64, visibility []PhotoVisibility, explicitOK bool, pager *Pagination) ([]*Photo, error) {
var p = []*Photo{}
func PaginateUserPhotos(userID uint64, conf UserGallery, pager *Pagination) ([]*Photo, error) {
var (
p = []*Photo{}
wheres = []string{}
placeholders = []interface{}{}
)
var explicit = []bool{false}
if explicitOK {
explicit = []bool{true, false}
var explicit = []bool{}
switch conf.Explicit {
case "true":
explicit = []bool{true}
case "false":
explicit = []bool{false}
}
wheres = append(wheres, "user_id = ? AND visibility IN ?")
placeholders = append(placeholders, userID, conf.Visibility)
if len(explicit) > 0 {
wheres = append(wheres, "explicit = ?")
placeholders = append(placeholders, explicit[0])
}
query := DB.Where(
"user_id = ? AND visibility IN ? AND explicit IN ?",
userID,
visibility,
explicit,
strings.Join(wheres, " AND "),
placeholders...,
).Order(
pager.Sort,
)

View File

@ -198,6 +198,11 @@
{{.ExplicitCount}} explicit photo{{Pluralize64 .ExplicitCount}} hidden per your <a href="/settings#prefs">settings</a>.
{{end}}
</span>
{{else if .ExplicitCount}}
<!-- No pager, but still show explicit hint, e.g. in case user filters by Private but all privates are explicit -->
<span>
{{.ExplicitCount}} explicit photo{{Pluralize64 .ExplicitCount}} hidden per your <a href="/settings#prefs">settings</a>.
</span>
{{end}}
</div>
</div>
@ -218,9 +223,9 @@
</div>
</div>
{{if .IsSiteGallery}}
<!-- Filters -->
<div class="block">
<form action="/photo/gallery" method="GET">
<form action="{{.Request.URL.Path}}" method="GET">
<div class="card nonshy-collapsible-mobile">
<header class="card-header has-background-link-light">
@ -236,7 +241,7 @@
<div class="card-content">
<div class="columns is-multiline mb-0">
{{if .CurrentUser.Explicit}}
{{if or .CurrentUser.Explicit .IsOwnPhotos}}
<div class="column">
<div class="field">
<label class="label" for="explicit">Explicit:</label>
@ -261,8 +266,14 @@
{{if .CurrentUser.IsInnerCircle}}
<option value="circle"{{if eq .FilterVisibility "circle"}} selected{{end}}>Inner circle only</option>
{{end}}
<!-- Friends & Private: always show on Site Gallery, show if available on User Gallery -->
{{if or .IsSiteGallery .AreFriends .IsOwnPhotos}}
<option value="friends"{{if eq .FilterVisibility "friends"}} selected{{end}}>Friends only</option>
{{end}}
{{if or .IsSiteGallery .AreWeGrantedPrivate .IsOwnPhotos}}
<option value="private"{{if eq .FilterVisibility "private"}} selected{{end}}>Private only</option>
{{end}}
</select>
</div>
</div>
@ -280,7 +291,7 @@
</div>
</div>
{{if .CurrentUser.HasAdminScope "social.moderator.photo"}}
{{if and .IsSiteGallery (.CurrentUser.HasAdminScope "social.moderator.photo")}}
<div class="column">
<div class="field">
<label class="label has-text-danger" for="admin_view">Admin view:</label>
@ -306,7 +317,6 @@
</form>
</div>
{{end}}
{{if .IsOwnPhotos}}
<div class="block">