Tweak friends-only pic notification revoke
This commit is contained in:
parent
eed971d997
commit
b4cd57c8c3
|
@ -61,8 +61,8 @@ func AddFriend() http.HandlerFunc {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Revoke any friends-only photo notifications they had received before.
|
// Revoke any friends-only photo notifications they had received before.
|
||||||
if err := models.RevokeFriendPhotoNotifications(currentUser, user.ID); err != nil {
|
if err := models.RevokeFriendPhotoNotifications(currentUser, user); err != nil {
|
||||||
log.Error("Couldn't revoke friend photo notifications from %s to %s: %s", currentUser.Username, user.Username, err)
|
log.Error("Couldn't revoke friend photo notifications between %s and %s: %s", currentUser.Username, user.Username, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var message string
|
var message string
|
||||||
|
|
|
@ -162,7 +162,7 @@ func Share() http.HandlerFunc {
|
||||||
models.RemoveSpecificNotification(user.ID, models.NotificationPrivatePhoto, "__private_photos", currentUser.ID)
|
models.RemoveSpecificNotification(user.ID, models.NotificationPrivatePhoto, "__private_photos", currentUser.ID)
|
||||||
|
|
||||||
// Revoke any "has uploaded a new private photo" notifications in this user's list.
|
// 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)
|
log.Error("RevokePrivatePhotoNotifications(%s): %s", currentUser.Username, err)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
|
|
|
@ -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
|
// 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.
|
// 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.
|
// Notifications about friend photos are revoked going in both directions.
|
||||||
photoIDs, err := currentUser.AllFriendsOnlyPhotoIDs()
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
} else if len(photoIDs) == 0 {
|
} else if len(allPhotoIDs) == 0 {
|
||||||
// Nothing to do.
|
// Nothing to do.
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Info("RevokeFriendPhotoNotifications(%s): forget about friend photo uploads for user %d on photo IDs: %v", currentUser.Username, fromUserID, photoIDs)
|
log.Info("RevokeFriendPhotoNotifications(%s): forget about friend photo uploads for user %s on photo IDs: %v", currentUser.Username, other.Username, allPhotoIDs)
|
||||||
return RemoveSpecificNotificationBulk(fromUserID, NotificationNewPhoto, "photos", photoIDs)
|
return RemoveSpecificNotificationBulk([]*User{currentUser, other}, NotificationNewPhoto, "photos", allPhotoIDs)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save photo.
|
// Save photo.
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -121,10 +122,20 @@ func RemoveSpecificNotification(userID uint64, t NotificationType, tableName str
|
||||||
|
|
||||||
// RemoveSpecificNotificationBulk can remove notifications about several TableIDs of the same type,
|
// RemoveSpecificNotificationBulk can remove notifications about several TableIDs of the same type,
|
||||||
// e.g. to bulk remove new private photo upload notifications.
|
// 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(
|
result := DB.Where(
|
||||||
"user_id = ? AND type = ? AND table_name = ? AND table_id IN ?",
|
"user_id IN ? AND type = ? AND table_name = ? AND table_id IN ?",
|
||||||
userID, t, tableName, tableIDs,
|
userIDs, t, tableName, tableIDs,
|
||||||
).Delete(&Notification{})
|
).Delete(&Notification{})
|
||||||
return result.Error
|
return result.Error
|
||||||
}
|
}
|
||||||
|
|
|
@ -212,6 +212,34 @@ func CountRecentGalleryPhotos(user *User, duration time.Duration) (count int64)
|
||||||
return
|
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.
|
// CountPhotosICanSee returns the number of photos on an account which can be seen by the given viewer.
|
||||||
func CountPhotosICanSee(user *User, viewer *User) int64 {
|
func CountPhotosICanSee(user *User, viewer *User) int64 {
|
||||||
// Visibility filters to query by.
|
// Visibility filters to query by.
|
||||||
|
|
|
@ -61,7 +61,7 @@ func RevokePrivatePhotosAll(sourceUserID uint64) error {
|
||||||
// RevokePrivatePhotoNotifications removes notifications about newly uploaded private photos
|
// RevokePrivatePhotoNotifications removes notifications about newly uploaded private photos
|
||||||
// that were sent to one (or multiple) members when the user revokes their access later. Pass
|
// 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.
|
// 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.
|
// Gather the IDs of all our private photos to nuke notifications for.
|
||||||
photoIDs, err := currentUser.AllPrivatePhotoIDs()
|
photoIDs, err := currentUser.AllPrivatePhotoIDs()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -72,12 +72,12 @@ func RevokePrivatePhotoNotifications(currentUser *User, fromUserID *uint64) erro
|
||||||
}
|
}
|
||||||
|
|
||||||
// Who to clear the notifications for?
|
// 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)
|
log.Info("RevokePrivatePhotoNotifications(%s): forget about private photo uploads for EVERYBODY on photo IDs: %v", currentUser.Username, photoIDs)
|
||||||
return RemoveNotificationBulk("photos", photoIDs)
|
return RemoveNotificationBulk("photos", photoIDs)
|
||||||
} else {
|
} else {
|
||||||
log.Info("RevokePrivatePhotoNotifications(%s): forget about private photo uploads for user %d on photo IDs: %v", currentUser.Username, *fromUserID, photoIDs)
|
log.Info("RevokePrivatePhotoNotifications(%s): forget about private photo uploads for user %s on photo IDs: %v", currentUser.Username, fromUser.Username, photoIDs)
|
||||||
return RemoveSpecificNotificationBulk(*fromUserID, NotificationNewPhoto, "photos", photoIDs)
|
return RemoveSpecificNotificationBulk([]*User{currentUser, fromUser}, NotificationNewPhoto, "photos", photoIDs)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -100,25 +100,6 @@ func (u *User) AllPrivatePhotoIDs() ([]uint64, error) {
|
||||||
return photoIDs, nil
|
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.
|
// AllPhotoIDs returns the listing of all IDs of the user's photos.
|
||||||
func (u *User) AllPhotoIDs() ([]uint64, error) {
|
func (u *User) AllPhotoIDs() ([]uint64, error) {
|
||||||
if u.cachePhotoIDs != nil {
|
if u.cachePhotoIDs != nil {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user