Fix site gallery userID + visibility filters

face-detect
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" {
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.
if myFriendCount > 0 {
who = "friends"
@ -85,7 +85,8 @@ func SiteGallery() http.HandlerFunc {
Explicit: filterExplicit,
Visibility: filterVisibility,
AdminView: adminView,
FriendsOnly: isShy || who == "friends",
FriendsOnly: who == "friends",
IsShy: isShy || who == "friends+private",
}, pager)
// Bulk load the users associated with these photos.

View File

@ -2,6 +2,7 @@ package models
import (
"errors"
"fmt"
"strings"
"time"
@ -399,6 +400,7 @@ type Gallery struct {
Explicit string // Explicit filter
Visibility string // Visibility filter
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
}
@ -412,7 +414,8 @@ func PaginateGalleryPhotos(user *User, conf Gallery, pager *Pagination) ([]*Phot
filterExplicit = conf.Explicit
filterVisibility = conf.Visibility
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{}
query *gorm.DB
@ -420,16 +423,16 @@ func PaginateGalleryPhotos(user *User, conf Gallery, pager *Pagination) ([]*Phot
userID = user.ID
explicitOK = user.Explicit // User opted-in for explicit content
blocklist = BlockedUserIDs(user)
friendIDs = FriendIDs(userID)
privateUserIDs = PrivateGrantedUserIDs(userID)
wheres = []string{}
placeholders = []interface{}{}
blocklist = BlockedUserIDs(user)
friendIDs = FriendIDs(userID)
privateUserIDs = PrivateGrantedUserIDs(userID)
privateUserIDsAreFriends = PrivateGrantedUserIDsAreFriends(user)
wheres = []string{}
placeholders = []interface{}{}
)
// Define "all photos visibilities"
var (
photosAll = PhotoVisibilityAll
photosPublic = []PhotoVisibility{
PhotoPublic,
}
@ -437,9 +440,11 @@ func PaginateGalleryPhotos(user *User, conf Gallery, pager *Pagination) ([]*Phot
PhotoPublic,
PhotoFriends,
}
photosPrivate = []PhotoVisibility{
PhotoPrivate,
}
)
if user.IsInnerCircle() {
photosAll = PhotoVisibilityCircle
photosPublic = append(photosPublic, PhotoInnerCircle)
photosFriends = append(photosFriends, PhotoInnerCircle)
}
@ -450,32 +455,52 @@ func PaginateGalleryPhotos(user *User, conf Gallery, pager *Pagination) ([]*Phot
// Include ourself in our friend IDs.
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?
if friendsOnly {
if isShy {
// Shy users can only see their Friends photos (public or friends visibility)
// and any Private photos to whom they were granted access.
wheres = append(wheres,
"((user_id IN ? AND visibility IN ?) OR "+
"(user_id IN ? AND visibility IN ?))",
visOrs = append(visOrs,
"(user_id IN ? AND visibility IN ?)",
"(user_id IN ? AND visibility IN ?)",
)
placeholders = append(placeholders,
friendIDs, PhotoVisibilityFriends,
privateUserIDs, photosAll,
visPlaceholders = append(visPlaceholders,
friendIDs, photosFriends,
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 {
// You can see friends' Friend photos but only public for non-friends.
wheres = append(wheres,
"((user_id IN ? AND visibility IN ?) OR "+
"(user_id IN ? AND visibility IN ?) OR "+
"(user_id NOT IN ? AND visibility IN ?))",
visOrs = append(visOrs,
"(user_id IN ? AND visibility IN ?)",
"(user_id IN ? AND visibility IN ?)",
"(user_id NOT IN ? AND visibility IN ?)",
)
placeholders = append(placeholders,
visPlaceholders = append(placeholders,
friendIDs, photosFriends,
privateUserIDs, photosAll,
privateUserIDs, photosPrivate,
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.
wheres = append(wheres, "gallery = ?")
placeholders = append(placeholders, true)

View File

@ -158,6 +158,23 @@ func PrivateGrantedUserIDs(userId uint64) []uint64 {
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).
func PrivateGranteeUserIDs(userId uint64) []uint64 {
var (

View File

@ -226,9 +226,15 @@
<!-- Indicator if friends-only is selected. -->
{{if eq .FilterWho "friends"}}
<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>
</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}}
<!-- Filters -->
@ -256,7 +262,8 @@
<label class="label" for="who">Whose photos:</label>
<div class="select is-fullwidth">
<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>
</select>
</div>