Fix private grants page

This commit is contained in:
Noah Petherbridge 2023-09-01 22:41:33 -07:00
parent 944dbb749b
commit 2d7f8c0d87
3 changed files with 36 additions and 2 deletions

View File

@ -42,6 +42,8 @@ func Private() http.HandlerFunc {
return return
} }
log.Error("pager: %+v, len: %d", pager, len(users))
var vars = map[string]interface{}{ var vars = map[string]interface{}{
"IsGrantee": isGrantee, "IsGrantee": isGrantee,
"CountGrantee": models.CountPrivateGrantee(currentUser.ID), "CountGrantee": models.CountPrivateGrantee(currentUser.ID),

View File

@ -28,6 +28,7 @@ func DeleteUser(user *models.User) error {
{"Comments", DeleteComments}, {"Comments", DeleteComments},
{"Subscriptions", DeleteSubscriptions}, {"Subscriptions", DeleteSubscriptions},
{"Photos", DeleteUserPhotos}, {"Photos", DeleteUserPhotos},
{"Private Photo Grants", DeletePrivateGrants},
{"Certification Photo", DeleteCertification}, {"Certification Photo", DeleteCertification},
{"Who's Nearby Locations", DeleteUserLocation}, {"Who's Nearby Locations", DeleteUserLocation},
{"Comment Photos", DeleteUserCommentPhotos}, {"Comment Photos", DeleteUserCommentPhotos},
@ -170,6 +171,16 @@ func DeleteFriends(userID uint64) error {
return result.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. // DeleteNotifications scrubs all notifications about a user.
func DeleteNotifications(userID uint64) error { func DeleteNotifications(userID uint64) error {
log.Error("DeleteUser: DeleteNotifications(%d)", userID) log.Error("DeleteUser: DeleteNotifications(%d)", userID)

View File

@ -197,6 +197,16 @@ func PaginatePrivatePhotoList(user *User, grantee bool, pager *Pagination) ([]*U
placeholders = append(placeholders, user.ID) 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( query = DB.Where(
strings.Join(wheres, " AND "), strings.Join(wheres, " AND "),
placeholders..., 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. // MapShyAccounts looks up a set of user IDs in bulk and returns a PrivateGranteeMap suitable for templates.
func MapPrivatePhotoGrantee(currentUser *User, users []*User) PrivateGranteeMap { func MapPrivatePhotoGrantee(currentUser *User, users []*User) PrivateGranteeMap {
var ( 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 ( var (
matched = []*PrivatePhoto{} matched = []*PrivatePhoto{}
result = DB.Model(&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) Find(&matched)
) )