From b4cd57c8c304ad169bb08f7ae3d563ffd7cd8a9f Mon Sep 17 00:00:00 2001 From: Noah Petherbridge Date: Wed, 10 Jan 2024 22:25:50 -0800 Subject: [PATCH] Tweak friends-only pic notification revoke --- pkg/controller/friend/request.go | 4 ++-- pkg/controller/photo/private.go | 2 +- pkg/models/friend.go | 14 ++++++++------ pkg/models/notification.go | 17 ++++++++++++++--- pkg/models/photo.go | 28 ++++++++++++++++++++++++++++ pkg/models/private_photo.go | 27 ++++----------------------- 6 files changed, 57 insertions(+), 35 deletions(-) diff --git a/pkg/controller/friend/request.go b/pkg/controller/friend/request.go index 05548f2..d1fc409 100644 --- a/pkg/controller/friend/request.go +++ b/pkg/controller/friend/request.go @@ -61,8 +61,8 @@ func AddFriend() http.HandlerFunc { } // Revoke any friends-only photo notifications they had received before. - if err := models.RevokeFriendPhotoNotifications(currentUser, user.ID); err != nil { - log.Error("Couldn't revoke friend photo notifications from %s to %s: %s", currentUser.Username, user.Username, err) + if err := models.RevokeFriendPhotoNotifications(currentUser, user); err != nil { + log.Error("Couldn't revoke friend photo notifications between %s and %s: %s", currentUser.Username, user.Username, err) } var message string diff --git a/pkg/controller/photo/private.go b/pkg/controller/photo/private.go index 13cdd98..70f2402 100644 --- a/pkg/controller/photo/private.go +++ b/pkg/controller/photo/private.go @@ -162,7 +162,7 @@ func Share() http.HandlerFunc { models.RemoveSpecificNotification(user.ID, models.NotificationPrivatePhoto, "__private_photos", currentUser.ID) // Revoke any "has uploaded a new private photo" notifications in this user's list. - if err := models.RevokePrivatePhotoNotifications(currentUser, &user.ID); err != nil { + if err := models.RevokePrivatePhotoNotifications(currentUser, user); err != nil { log.Error("RevokePrivatePhotoNotifications(%s): %s", currentUser.Username, err) } return diff --git a/pkg/models/friend.go b/pkg/models/friend.go index 6587160..40e0d53 100644 --- a/pkg/models/friend.go +++ b/pkg/models/friend.go @@ -451,18 +451,20 @@ func RemoveFriend(sourceUserID, targetUserID uint64) error { // // For example: if I unfriend you, all your past notifications that showed my friends-only photos should // be revoked so that you can't see them anymore. -func RevokeFriendPhotoNotifications(currentUser *User, fromUserID uint64) error { - // Gather the IDs of all our private photos to nuke notifications for. - photoIDs, err := currentUser.AllFriendsOnlyPhotoIDs() +// +// Notifications about friend photos are revoked going in both directions. +func RevokeFriendPhotoNotifications(currentUser, other *User) error { + // Gather the IDs of all their friends-only photos to nuke notifications for. + allPhotoIDs, err := AllFriendsOnlyPhotoIDs(currentUser, other) if err != nil { return err - } else if len(photoIDs) == 0 { + } else if len(allPhotoIDs) == 0 { // Nothing to do. return nil } - log.Info("RevokeFriendPhotoNotifications(%s): forget about friend photo uploads for user %d on photo IDs: %v", currentUser.Username, fromUserID, photoIDs) - return RemoveSpecificNotificationBulk(fromUserID, NotificationNewPhoto, "photos", photoIDs) + log.Info("RevokeFriendPhotoNotifications(%s): forget about friend photo uploads for user %s on photo IDs: %v", currentUser.Username, other.Username, allPhotoIDs) + return RemoveSpecificNotificationBulk([]*User{currentUser, other}, NotificationNewPhoto, "photos", allPhotoIDs) } // Save photo. diff --git a/pkg/models/notification.go b/pkg/models/notification.go index 3e1572b..47af9d4 100644 --- a/pkg/models/notification.go +++ b/pkg/models/notification.go @@ -1,6 +1,7 @@ package models import ( + "errors" "strings" "time" @@ -121,10 +122,20 @@ func RemoveSpecificNotification(userID uint64, t NotificationType, tableName str // RemoveSpecificNotificationBulk can remove notifications about several TableIDs of the same type, // e.g. to bulk remove new private photo upload notifications. -func RemoveSpecificNotificationBulk(userID uint64, t NotificationType, tableName string, tableIDs []uint64) error { +func RemoveSpecificNotificationBulk(users []*User, t NotificationType, tableName string, tableIDs []uint64) error { + var userIDs = []uint64{} + for _, user := range users { + userIDs = append(userIDs, user.ID) + } + + if len(userIDs) == 0 { + // Nothing to do. + return errors.New("no user IDs given") + } + result := DB.Where( - "user_id = ? AND type = ? AND table_name = ? AND table_id IN ?", - userID, t, tableName, tableIDs, + "user_id IN ? AND type = ? AND table_name = ? AND table_id IN ?", + userIDs, t, tableName, tableIDs, ).Delete(&Notification{}) return result.Error } diff --git a/pkg/models/photo.go b/pkg/models/photo.go index ba1b1c0..969e4e3 100644 --- a/pkg/models/photo.go +++ b/pkg/models/photo.go @@ -212,6 +212,34 @@ func CountRecentGalleryPhotos(user *User, duration time.Duration) (count int64) return } +// AllFriendsOnlyPhotoIDs returns the listing of all friends-only photo IDs belonging to the user(s) given. +func AllFriendsOnlyPhotoIDs(users ...*User) ([]uint64, error) { + var userIDs = []uint64{} + for _, user := range users { + userIDs = append(userIDs, user.ID) + } + + if len(userIDs) == 0 { + return nil, errors.New("no user IDs given") + } + + var photoIDs = []uint64{} + err := DB.Table( + "photos", + ).Select( + "photos.id AS id", + ).Where( + "user_id IN ? AND visibility = ?", + userIDs, PhotoFriends, + ).Scan(&photoIDs) + + if err.Error != nil { + return photoIDs, fmt.Errorf("AllFriendsOnlyPhotoIDs(%+v): %s", userIDs, err.Error) + } + + return photoIDs, nil +} + // CountPhotosICanSee returns the number of photos on an account which can be seen by the given viewer. func CountPhotosICanSee(user *User, viewer *User) int64 { // Visibility filters to query by. diff --git a/pkg/models/private_photo.go b/pkg/models/private_photo.go index 4396f07..e193aba 100644 --- a/pkg/models/private_photo.go +++ b/pkg/models/private_photo.go @@ -61,7 +61,7 @@ func RevokePrivatePhotosAll(sourceUserID uint64) error { // RevokePrivatePhotoNotifications removes notifications about newly uploaded private photos // that were sent to one (or multiple) members when the user revokes their access later. Pass // a nil fromUserID to revoke the photo upload notifications from ALL users. -func RevokePrivatePhotoNotifications(currentUser *User, fromUserID *uint64) error { +func RevokePrivatePhotoNotifications(currentUser, fromUser *User) error { // Gather the IDs of all our private photos to nuke notifications for. photoIDs, err := currentUser.AllPrivatePhotoIDs() if err != nil { @@ -72,12 +72,12 @@ func RevokePrivatePhotoNotifications(currentUser *User, fromUserID *uint64) erro } // Who to clear the notifications for? - if fromUserID == nil { + if fromUser == nil { log.Info("RevokePrivatePhotoNotifications(%s): forget about private photo uploads for EVERYBODY on photo IDs: %v", currentUser.Username, photoIDs) return RemoveNotificationBulk("photos", photoIDs) } else { - log.Info("RevokePrivatePhotoNotifications(%s): forget about private photo uploads for user %d on photo IDs: %v", currentUser.Username, *fromUserID, photoIDs) - return RemoveSpecificNotificationBulk(*fromUserID, NotificationNewPhoto, "photos", photoIDs) + log.Info("RevokePrivatePhotoNotifications(%s): forget about private photo uploads for user %s on photo IDs: %v", currentUser.Username, fromUser.Username, photoIDs) + return RemoveSpecificNotificationBulk([]*User{currentUser, fromUser}, NotificationNewPhoto, "photos", photoIDs) } } @@ -100,25 +100,6 @@ func (u *User) AllPrivatePhotoIDs() ([]uint64, error) { return photoIDs, nil } -// AllFriendsOnlyPhotoIDs returns the listing of all IDs of the user's private photos. -func (u *User) AllFriendsOnlyPhotoIDs() ([]uint64, error) { - var photoIDs = []uint64{} - err := DB.Table( - "photos", - ).Select( - "photos.id AS id", - ).Where( - "user_id = ? AND visibility = ?", - u.ID, PhotoFriends, - ).Scan(&photoIDs) - - if err.Error != nil { - return photoIDs, fmt.Errorf("AllFriendsOnlyPhotoIDs(%s): %s", u.Username, err.Error) - } - - return photoIDs, nil -} - // AllPhotoIDs returns the listing of all IDs of the user's photos. func (u *User) AllPhotoIDs() ([]uint64, error) { if u.cachePhotoIDs != nil {