Filters on User Gallery Pages

This commit is contained in:
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, Type: models.NotificationPrivatePhoto,
TableName: "__private_photos", TableName: "__private_photos",
TableID: currentUser.ID, 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 { if err := models.CreateNotification(notif); err != nil {
log.Error("Couldn't create PrivatePhoto notification: %s", err) log.Error("Couldn't create PrivatePhoto notification: %s", err)

View File

@ -40,7 +40,7 @@ func SiteGallery() http.HandlerFunc {
} }
} }
if !sortOK { if !sortOK {
sort = "created_at desc" sort = sortWhitelist[0]
} }
// Defaults. // 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. // UserPhotos controller (/photo/u/:username) to view a user's gallery or manage if it's yourself.
func UserPhotos() http.HandlerFunc { func UserPhotos() http.HandlerFunc {
tmpl := templates.Must("photo/gallery.html") 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) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Query params. // Query params.
var ( var (
viewStyle = r.FormValue("view") // cards (default), full 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" { if viewStyle != "full" {
viewStyle = "cards" viewStyle = "cards"
} }
@ -45,9 +71,11 @@ func UserPhotos() http.HandlerFunc {
session.FlashError(w, r, "Unexpected error: couldn't get CurrentUser") session.FlashError(w, r, "Unexpected error: couldn't get CurrentUser")
} }
var ( var (
areFriends = models.AreFriends(user.ID, currentUser.ID)
isPrivate = user.Visibility == models.UserVisibilityPrivate && !areFriends
isOwnPhotos = currentUser.ID == user.ID isOwnPhotos = currentUser.ID == user.ID
isShy = currentUser.IsShy() 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. // 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? // 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 { if isPrivate && !currentUser.IsAdmin && !isOwnPhotos {
session.FlashError(w, r, "This user's profile page and photo gallery are private.") session.FlashError(w, r, "This user's profile page and photo gallery are private.")
templates.Redirect(w, "/u/"+user.Username) templates.Redirect(w, "/u/"+user.Username)
@ -98,7 +122,7 @@ func UserPhotos() http.HandlerFunc {
if isOwnPhotos || isGrantee || currentUser.HasAdminScope(config.ScopePhotoModerator) { if isOwnPhotos || isGrantee || currentUser.HasAdminScope(config.ScopePhotoModerator) {
visibility = append(visibility, models.PhotoPrivate) 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) visibility = append(visibility, models.PhotoFriends)
} }
@ -107,27 +131,52 @@ func UserPhotos() http.HandlerFunc {
visibility = append(visibility, models.PhotoInnerCircle) visibility = append(visibility, models.PhotoInnerCircle)
} }
// Explicit photo filter? // If we are Filtering by Visibility, ensure the target visibility is accessible to us.
explicit := currentUser.Explicit if filterVisibility != "" {
if isOwnPhotos { var isOK bool
explicit = true 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. // Get the page of photos.
pager := &models.Pagination{ pager := &models.Pagination{
Page: 1, Page: 1,
PerPage: config.PageSizeUserGallery, PerPage: config.PageSizeUserGallery,
Sort: "created_at desc", Sort: sort,
} }
pager.ParsePage(r) 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 { if err != nil {
log.Error("PaginateUserPhotos(%s): %s", user.Username, err) log.Error("PaginateUserPhotos(%s): %s", user.Username, err)
} }
// Get the count of explicit photos if we are not viewing explicit photos. // Get the count of explicit photos if we are not viewing explicit photos.
var explicitCount int64 var explicitCount int64
if !explicit { if filterExplicit == "false" {
explicitCount, _ = models.CountExplicitPhotos(user.ID, visibility) explicitCount, _ = models.CountExplicitPhotos(user.ID, visibility)
} }
@ -145,6 +194,7 @@ func UserPhotos() http.HandlerFunc {
"IsShyFrom": isShyFrom, "IsShyFrom": isShyFrom,
"IsMyPrivateUnlockedFor": isGranted, // have WE granted THIS USER to see our private pics? "IsMyPrivateUnlockedFor": isGranted, // have WE granted THIS USER to see our private pics?
"AreWeGrantedPrivate": isGrantee, // have THEY granted US private photo access. "AreWeGrantedPrivate": isGrantee, // have THEY granted US private photo access.
"AreFriends": areFriends,
"User": user, "User": user,
"Photos": photos, "Photos": photos,
"PhotoCount": models.CountPhotosICanSee(user, currentUser), "PhotoCount": models.CountPhotosICanSee(user, currentUser),
@ -157,6 +207,11 @@ func UserPhotos() http.HandlerFunc {
"CommentMap": commentMap, "CommentMap": commentMap,
"ViewStyle": viewStyle, "ViewStyle": viewStyle,
"ExplicitCount": explicitCount, "ExplicitCount": explicitCount,
// Search filters
"Sort": sort,
"FilterExplicit": filterExplicit,
"FilterVisibility": filterVisibility,
} }
if err := tmpl.Execute(w, r, vars); err != nil { if err := tmpl.Execute(w, r, vars); err != nil {

View File

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

View File

@ -34,6 +34,10 @@ const (
PhotoFriends PhotoVisibility = "friends" // only friends can see it PhotoFriends PhotoVisibility = "friends" // only friends can see it
PhotoPrivate PhotoVisibility = "private" // private PhotoPrivate PhotoVisibility = "private" // private
PhotoInnerCircle PhotoVisibility = "circle" // inner circle 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. // PhotoVisibility preset settings.
@ -103,22 +107,41 @@ func GetPhotos(IDs []uint64) (map[uint64]*Photo, error) {
return mp, result.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. PaginateUserPhotos gets a page of photos belonging to a user ID.
*/ */
func PaginateUserPhotos(userID uint64, visibility []PhotoVisibility, explicitOK bool, pager *Pagination) ([]*Photo, error) { func PaginateUserPhotos(userID uint64, conf UserGallery, pager *Pagination) ([]*Photo, error) {
var p = []*Photo{} var (
p = []*Photo{}
wheres = []string{}
placeholders = []interface{}{}
)
var explicit = []bool{false} var explicit = []bool{}
if explicitOK { switch conf.Explicit {
explicit = []bool{true, false} 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( query := DB.Where(
"user_id = ? AND visibility IN ? AND explicit IN ?", strings.Join(wheres, " AND "),
userID, placeholders...,
visibility,
explicit,
).Order( ).Order(
pager.Sort, pager.Sort,
) )

View File

@ -198,6 +198,11 @@
{{.ExplicitCount}} explicit photo{{Pluralize64 .ExplicitCount}} hidden per your <a href="/settings#prefs">settings</a>. {{.ExplicitCount}} explicit photo{{Pluralize64 .ExplicitCount}} hidden per your <a href="/settings#prefs">settings</a>.
{{end}} {{end}}
</span> </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}} {{end}}
</div> </div>
</div> </div>
@ -218,9 +223,9 @@
</div> </div>
</div> </div>
{{if .IsSiteGallery}} <!-- Filters -->
<div class="block"> <div class="block">
<form action="/photo/gallery" method="GET"> <form action="{{.Request.URL.Path}}" method="GET">
<div class="card nonshy-collapsible-mobile"> <div class="card nonshy-collapsible-mobile">
<header class="card-header has-background-link-light"> <header class="card-header has-background-link-light">
@ -236,7 +241,7 @@
<div class="card-content"> <div class="card-content">
<div class="columns is-multiline mb-0"> <div class="columns is-multiline mb-0">
{{if .CurrentUser.Explicit}} {{if or .CurrentUser.Explicit .IsOwnPhotos}}
<div class="column"> <div class="column">
<div class="field"> <div class="field">
<label class="label" for="explicit">Explicit:</label> <label class="label" for="explicit">Explicit:</label>
@ -261,8 +266,14 @@
{{if .CurrentUser.IsInnerCircle}} {{if .CurrentUser.IsInnerCircle}}
<option value="circle"{{if eq .FilterVisibility "circle"}} selected{{end}}>Inner circle only</option> <option value="circle"{{if eq .FilterVisibility "circle"}} selected{{end}}>Inner circle only</option>
{{end}} {{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> <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> <option value="private"{{if eq .FilterVisibility "private"}} selected{{end}}>Private only</option>
{{end}}
</select> </select>
</div> </div>
</div> </div>
@ -280,7 +291,7 @@
</div> </div>
</div> </div>
{{if .CurrentUser.HasAdminScope "social.moderator.photo"}} {{if and .IsSiteGallery (.CurrentUser.HasAdminScope "social.moderator.photo")}}
<div class="column"> <div class="column">
<div class="field"> <div class="field">
<label class="label has-text-danger" for="admin_view">Admin view:</label> <label class="label has-text-danger" for="admin_view">Admin view:</label>
@ -306,7 +317,6 @@
</form> </form>
</div> </div>
{{end}}
{{if .IsOwnPhotos}} {{if .IsOwnPhotos}}
<div class="block"> <div class="block">