Fix site gallery userID + visibility filters

This commit is contained in:
Noah Petherbridge 2023-10-25 20:07:28 -07:00
parent 898be65327
commit 64d2749299
4 changed files with 75 additions and 25 deletions

View File

@ -60,7 +60,7 @@ func SiteGallery() http.HandlerFunc {
if viewStyle != "full" { if viewStyle != "full" {
viewStyle = "cards" viewStyle = "cards"
} }
if who != "friends" && who != "everybody" { if who != "friends" && who != "everybody" && who != "friends+private" {
// Default Who setting should be Friends-only, unless you have no friends. // Default Who setting should be Friends-only, unless you have no friends.
if myFriendCount > 0 { if myFriendCount > 0 {
who = "friends" who = "friends"
@ -85,7 +85,8 @@ func SiteGallery() http.HandlerFunc {
Explicit: filterExplicit, Explicit: filterExplicit,
Visibility: filterVisibility, Visibility: filterVisibility,
AdminView: adminView, AdminView: adminView,
FriendsOnly: isShy || who == "friends", FriendsOnly: who == "friends",
IsShy: isShy || who == "friends+private",
}, pager) }, pager)
// Bulk load the users associated with these photos. // Bulk load the users associated with these photos.

View File

@ -2,6 +2,7 @@ package models
import ( import (
"errors" "errors"
"fmt"
"strings" "strings"
"time" "time"
@ -399,6 +400,7 @@ type Gallery struct {
Explicit string // Explicit filter Explicit string // Explicit filter
Visibility string // Visibility filter Visibility string // Visibility filter
AdminView bool // Show all images AdminView bool // Show all images
IsShy bool // Current user is like a Shy Account (or: show self/friends and private photo grants only)
FriendsOnly bool // Only show self/friends instead of everybody's pics FriendsOnly bool // Only show self/friends instead of everybody's pics
} }
@ -412,7 +414,8 @@ func PaginateGalleryPhotos(user *User, conf Gallery, pager *Pagination) ([]*Phot
filterExplicit = conf.Explicit filterExplicit = conf.Explicit
filterVisibility = conf.Visibility filterVisibility = conf.Visibility
adminView = conf.AdminView adminView = conf.AdminView
friendsOnly = conf.FriendsOnly friendsOnly = conf.FriendsOnly // Show only self and friends pictures
isShy = conf.IsShy // Self, friends, and private photo grants only
p = []*Photo{} p = []*Photo{}
query *gorm.DB query *gorm.DB
@ -420,16 +423,16 @@ func PaginateGalleryPhotos(user *User, conf Gallery, pager *Pagination) ([]*Phot
userID = user.ID userID = user.ID
explicitOK = user.Explicit // User opted-in for explicit content explicitOK = user.Explicit // User opted-in for explicit content
blocklist = BlockedUserIDs(user) blocklist = BlockedUserIDs(user)
friendIDs = FriendIDs(userID) friendIDs = FriendIDs(userID)
privateUserIDs = PrivateGrantedUserIDs(userID) privateUserIDs = PrivateGrantedUserIDs(userID)
wheres = []string{} privateUserIDsAreFriends = PrivateGrantedUserIDsAreFriends(user)
placeholders = []interface{}{} wheres = []string{}
placeholders = []interface{}{}
) )
// Define "all photos visibilities" // Define "all photos visibilities"
var ( var (
photosAll = PhotoVisibilityAll
photosPublic = []PhotoVisibility{ photosPublic = []PhotoVisibility{
PhotoPublic, PhotoPublic,
} }
@ -437,9 +440,11 @@ func PaginateGalleryPhotos(user *User, conf Gallery, pager *Pagination) ([]*Phot
PhotoPublic, PhotoPublic,
PhotoFriends, PhotoFriends,
} }
photosPrivate = []PhotoVisibility{
PhotoPrivate,
}
) )
if user.IsInnerCircle() { if user.IsInnerCircle() {
photosAll = PhotoVisibilityCircle
photosPublic = append(photosPublic, PhotoInnerCircle) photosPublic = append(photosPublic, PhotoInnerCircle)
photosFriends = append(photosFriends, PhotoInnerCircle) photosFriends = append(photosFriends, PhotoInnerCircle)
} }
@ -450,32 +455,52 @@ func PaginateGalleryPhotos(user *User, conf Gallery, pager *Pagination) ([]*Phot
// Include ourself in our friend IDs. // Include ourself in our friend IDs.
friendIDs = append(friendIDs, userID) friendIDs = append(friendIDs, userID)
// What sets of User ID * Visibility filters to query under?
var (
visOrs = []string{}
visPlaceholders = []interface{}{}
)
// Whose photos can you see on the Site Gallery? // Whose photos can you see on the Site Gallery?
if friendsOnly { if isShy {
// Shy users can only see their Friends photos (public or friends visibility) // Shy users can only see their Friends photos (public or friends visibility)
// and any Private photos to whom they were granted access. // and any Private photos to whom they were granted access.
wheres = append(wheres, visOrs = append(visOrs,
"((user_id IN ? AND visibility IN ?) OR "+ "(user_id IN ? AND visibility IN ?)",
"(user_id IN ? AND visibility IN ?))", "(user_id IN ? AND visibility IN ?)",
) )
placeholders = append(placeholders, visPlaceholders = append(visPlaceholders,
friendIDs, PhotoVisibilityFriends, friendIDs, photosFriends,
privateUserIDs, photosAll, privateUserIDs, photosPrivate,
) )
} else if friendsOnly {
// User wants to see only self and friends photos.
visOrs = append(visOrs, "(user_id IN ? AND visibility IN ?)")
visPlaceholders = append(visPlaceholders, friendIDs, photosFriends)
// If their friends granted private photos, include those too.
if len(privateUserIDsAreFriends) > 0 {
visOrs = append(visOrs, "(user_id IN ? AND visibility IN ?)")
visPlaceholders = append(visPlaceholders, privateUserIDsAreFriends, photosPrivate)
}
} else { } else {
// You can see friends' Friend photos but only public for non-friends. // You can see friends' Friend photos but only public for non-friends.
wheres = append(wheres, visOrs = append(visOrs,
"((user_id IN ? AND visibility IN ?) OR "+ "(user_id IN ? AND visibility IN ?)",
"(user_id IN ? AND visibility IN ?) OR "+ "(user_id IN ? AND visibility IN ?)",
"(user_id NOT IN ? AND visibility IN ?))", "(user_id NOT IN ? AND visibility IN ?)",
) )
placeholders = append(placeholders, visPlaceholders = append(placeholders,
friendIDs, photosFriends, friendIDs, photosFriends,
privateUserIDs, photosAll, privateUserIDs, photosPrivate,
friendIDs, photosPublic, friendIDs, photosPublic,
) )
} }
// Join the User ID * Visibility filters into a nested "OR"
wheres = append(wheres, fmt.Sprintf("(%s)", strings.Join(visOrs, " OR ")))
placeholders = append(placeholders, visPlaceholders...)
// Gallery photos only. // Gallery photos only.
wheres = append(wheres, "gallery = ?") wheres = append(wheres, "gallery = ?")
placeholders = append(placeholders, true) placeholders = append(placeholders, true)

View File

@ -158,6 +158,23 @@ func PrivateGrantedUserIDs(userId uint64) []uint64 {
return userIDs return userIDs
} }
// PrivateGrantedUserIDsAreFriends returns user IDs who have granted us their private pictures, and are also our friends.
func PrivateGrantedUserIDsAreFriends(currentUser *User) []uint64 {
var (
ps = []*PrivatePhoto{}
userIDs = []uint64{}
)
DB.Model(&PrivatePhoto{}).Joins(
"JOIN friends ON friends.source_user_id = private_photos.source_user_id AND friends.target_user_id = private_photos.target_user_id",
).Where(
"private_photos.target_user_id = ? AND friends.approved IS true", currentUser.ID,
).Find(&ps)
for _, row := range ps {
userIDs = append(userIDs, row.SourceUserID)
}
return userIDs
}
// PrivateGranteeUserIDs are the users whom WE have granted access to our photos (userId is the photo owners). // PrivateGranteeUserIDs are the users whom WE have granted access to our photos (userId is the photo owners).
func PrivateGranteeUserIDs(userId uint64) []uint64 { func PrivateGranteeUserIDs(userId uint64) []uint64 {
var ( var (

View File

@ -226,9 +226,15 @@
<!-- Indicator if friends-only is selected. --> <!-- Indicator if friends-only is selected. -->
{{if eq .FilterWho "friends"}} {{if eq .FilterWho "friends"}}
<div class="notification is-success is-light"> <div class="notification is-success is-light">
Showing you all recent photos from <strong>your friends.</strong> Showing you all recent photos from <strong>yourself &amp; your friends.</strong>
<a href="{{.Request.URL.Path}}?who=everybody">See all certified members' gallery photos?</a> <a href="{{.Request.URL.Path}}?who=everybody">See all certified members' gallery photos?</a>
</div> </div>
{{else if eq .FilterWho "friends+private"}}
<div class="notification is-success is-light">
Showing you all recent photos from <strong>yourself &amp; your friends</strong> as well
as any <strong>private photos shared with you</strong> by others on the site (if they are
marked to appear in the Site Gallery).
</div>
{{end}} {{end}}
<!-- Filters --> <!-- Filters -->
@ -256,7 +262,8 @@
<label class="label" for="who">Whose photos:</label> <label class="label" for="who">Whose photos:</label>
<div class="select is-fullwidth"> <div class="select is-fullwidth">
<select id="who" name="who"> <select id="who" name="who">
<option value="friends"{{if eq .FilterWho "friends"}} selected{{end}}>My friends only</option> <option value="friends"{{if eq .FilterWho "friends"}} selected{{end}}>Myself &amp; friends only</option>
<option value="friends+private"{{if eq .FilterWho "friends+private"}} selected{{end}}>Myself, friends, &amp; private photo grants</option>
<option value="everybody"{{if eq .FilterWho "everybody"}} selected{{end}}>All certified members</option> <option value="everybody"{{if eq .FilterWho "everybody"}} selected{{end}}>All certified members</option>
</select> </select>
</div> </div>