From 2d7f8c0d8703dfe9fd1204163ecd999dd9be6c2e Mon Sep 17 00:00:00 2001 From: Noah Petherbridge Date: Fri, 1 Sep 2023 22:41:33 -0700 Subject: [PATCH] Fix private grants page --- pkg/controller/photo/private.go | 2 ++ pkg/models/deletion/delete_user.go | 11 +++++++++++ pkg/models/private_photo.go | 25 +++++++++++++++++++++++-- 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/pkg/controller/photo/private.go b/pkg/controller/photo/private.go index f6434af..90ea1b2 100644 --- a/pkg/controller/photo/private.go +++ b/pkg/controller/photo/private.go @@ -42,6 +42,8 @@ func Private() http.HandlerFunc { return } + log.Error("pager: %+v, len: %d", pager, len(users)) + var vars = map[string]interface{}{ "IsGrantee": isGrantee, "CountGrantee": models.CountPrivateGrantee(currentUser.ID), diff --git a/pkg/models/deletion/delete_user.go b/pkg/models/deletion/delete_user.go index c67c8f5..f673211 100644 --- a/pkg/models/deletion/delete_user.go +++ b/pkg/models/deletion/delete_user.go @@ -28,6 +28,7 @@ func DeleteUser(user *models.User) error { {"Comments", DeleteComments}, {"Subscriptions", DeleteSubscriptions}, {"Photos", DeleteUserPhotos}, + {"Private Photo Grants", DeletePrivateGrants}, {"Certification Photo", DeleteCertification}, {"Who's Nearby Locations", DeleteUserLocation}, {"Comment Photos", DeleteUserCommentPhotos}, @@ -170,6 +171,16 @@ func DeleteFriends(userID uint64) error { return result.Error } +// DeletePrivateGrants scrubs data for deleting a user. +func DeletePrivateGrants(userID uint64) error { + log.Error("DeleteUser: DeletePrivateGrants(%d)", userID) + result := models.DB.Where( + "source_user_id = ? OR target_user_id = ?", + userID, userID, + ).Delete(&models.PrivatePhoto{}) + return result.Error +} + // DeleteNotifications scrubs all notifications about a user. func DeleteNotifications(userID uint64) error { log.Error("DeleteUser: DeleteNotifications(%d)", userID) diff --git a/pkg/models/private_photo.go b/pkg/models/private_photo.go index a743452..ab7c45b 100644 --- a/pkg/models/private_photo.go +++ b/pkg/models/private_photo.go @@ -197,6 +197,16 @@ func PaginatePrivatePhotoList(user *User, grantee bool, pager *Pagination) ([]*U placeholders = append(placeholders, user.ID) } + // Users that actually exist. + wheres = append(wheres, ` + EXISTS ( + SELECT 1 + FROM users + WHERE users.id = private_photos.target_user_id + OR users.id = private_photos.source_user_id + )`, + ) + query = DB.Where( strings.Join(wheres, " AND "), placeholders..., @@ -233,13 +243,24 @@ type PrivateGranteeMap map[uint64]bool // MapShyAccounts looks up a set of user IDs in bulk and returns a PrivateGranteeMap suitable for templates. func MapPrivatePhotoGrantee(currentUser *User, users []*User) PrivateGranteeMap { var ( - usermap = PrivateGranteeMap{} + usermap = PrivateGranteeMap{} + set = map[uint64]interface{}{} + distinct = []uint64{} ) + // Uniqueify users. + for _, user := range users { + if _, ok := set[user.ID]; ok { + continue + } + set[user.ID] = nil + distinct = append(distinct, user.ID) + } + var ( matched = []*PrivatePhoto{} result = DB.Model(&PrivatePhoto{}). - Where("target_user_id = ?", currentUser.ID). + Where("target_user_id = ? AND source_user_id IN ?", currentUser.ID, distinct). Find(&matched) )