From 64d274929939f12ade6374fa527c5d51259b8bac Mon Sep 17 00:00:00 2001 From: Noah Petherbridge Date: Wed, 25 Oct 2023 20:07:28 -0700 Subject: [PATCH] Fix site gallery userID + visibility filters --- pkg/controller/photo/site_gallery.go | 5 ++- pkg/models/photo.go | 67 +++++++++++++++++++--------- pkg/models/private_photo.go | 17 +++++++ web/templates/photo/gallery.html | 11 ++++- 4 files changed, 75 insertions(+), 25 deletions(-) diff --git a/pkg/controller/photo/site_gallery.go b/pkg/controller/photo/site_gallery.go index bf35648..ad7e021 100644 --- a/pkg/controller/photo/site_gallery.go +++ b/pkg/controller/photo/site_gallery.go @@ -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. diff --git a/pkg/models/photo.go b/pkg/models/photo.go index 6aa4d8e..e055854 100644 --- a/pkg/models/photo.go +++ b/pkg/models/photo.go @@ -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) diff --git a/pkg/models/private_photo.go b/pkg/models/private_photo.go index c237bd0..c6d8f2d 100644 --- a/pkg/models/private_photo.go +++ b/pkg/models/private_photo.go @@ -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 ( diff --git a/web/templates/photo/gallery.html b/web/templates/photo/gallery.html index 801477c..cba81a1 100644 --- a/web/templates/photo/gallery.html +++ b/web/templates/photo/gallery.html @@ -226,9 +226,15 @@ {{if eq .FilterWho "friends"}}
- Showing you all recent photos from your friends. + Showing you all recent photos from yourself & your friends. See all certified members' gallery photos?
+ {{else if eq .FilterWho "friends+private"}} +
+ Showing you all recent photos from yourself & your friends as well + as any private photos shared with you by others on the site (if they are + marked to appear in the Site Gallery). +
{{end}} @@ -256,7 +262,8 @@