Photo upload notification: only notify explicit friends of new explicit photo

This commit is contained in:
Noah Petherbridge 2023-05-07 13:16:22 -07:00
parent 7af6d82fc7
commit ba9e90b32a
3 changed files with 40 additions and 3 deletions

View File

@ -157,10 +157,16 @@ func notifyFriendsNewPhoto(photo *models.Photo, currentUser *models.User) {
friendIDs = models.PrivateGranteeUserIDs(currentUser.ID) friendIDs = models.PrivateGranteeUserIDs(currentUser.ID)
log.Info("Notify %d private grantees about the new photo by %s", len(friendIDs), currentUser.Username) log.Info("Notify %d private grantees about the new photo by %s", len(friendIDs), currentUser.Username)
} else { } else {
// Friends // Get all our friend IDs. If this photo is Explicit, only select
// the friends who've opted-in for Explicit photo visibility.
if photo.Explicit {
friendIDs = models.FriendIDsAreExplicit(currentUser.ID)
log.Info("Notify %d EXPLICIT friends about the new photo by %s", len(friendIDs), currentUser.Username)
} else {
friendIDs = models.FriendIDs(currentUser.ID) friendIDs = models.FriendIDs(currentUser.ID)
log.Info("Notify %d friends about the new photo by %s", len(friendIDs), currentUser.Username) log.Info("Notify %d friends about the new photo by %s", len(friendIDs), currentUser.Username)
} }
}
for _, fid := range friendIDs { for _, fid := range friendIDs {
notif := &models.Notification{ notif := &models.Notification{

View File

@ -5,6 +5,7 @@ import (
"regexp" "regexp"
"code.nonshy.com/nonshy/website/pkg/config" "code.nonshy.com/nonshy/website/pkg/config"
"code.nonshy.com/nonshy/website/pkg/log"
"code.nonshy.com/nonshy/website/pkg/models" "code.nonshy.com/nonshy/website/pkg/models"
"code.nonshy.com/nonshy/website/pkg/session" "code.nonshy.com/nonshy/website/pkg/session"
"code.nonshy.com/nonshy/website/pkg/templates" "code.nonshy.com/nonshy/website/pkg/templates"
@ -114,6 +115,9 @@ func UserPhotos() http.HandlerFunc {
} }
pager.ParsePage(r) pager.ParsePage(r)
photos, err := models.PaginateUserPhotos(user.ID, visibility, explicit, pager) photos, err := models.PaginateUserPhotos(user.ID, visibility, explicit, pager)
if err != nil {
log.Error("PaginateUserPhotos(%s): %s", user.Username, err)
}
// Get the count of explicit photos if we are not viewing explicit photos. // Get the count of explicit photos if we are not viewing explicit photos.
var explicitCount int64 var explicitCount int64

View File

@ -4,6 +4,7 @@ import (
"errors" "errors"
"time" "time"
"code.nonshy.com/nonshy/website/pkg/log"
"gorm.io/gorm" "gorm.io/gorm"
) )
@ -104,6 +105,32 @@ func FriendIDs(userId uint64) []uint64 {
return userIDs return userIDs
} }
// FriendIDsAreExplicit returns friend IDs who have opted-in for Explicit content,
// e.g. to notify only them when you uploaded a new Explicit photo so that non-explicit
// users don't need to see that notification.
func FriendIDsAreExplicit(userId uint64) []uint64 {
var (
userIDs = []uint64{}
)
err := DB.Table(
"friends",
).Joins(
"JOIN users ON (users.id = friends.target_user_id)",
).Select(
"friends.target_user_id AS friend_id",
).Where(
"friends.source_user_id = ? AND friends.approved = ? AND users.explicit = ?",
userId, true, true,
).Scan(&userIDs)
if err.Error != nil {
log.Error("SQL error collecting explicit FriendIDs for %d: %s", userId, err)
}
return userIDs
}
// CountFriendRequests gets a count of pending requests for the user. // CountFriendRequests gets a count of pending requests for the user.
func CountFriendRequests(userID uint64) (int64, error) { func CountFriendRequests(userID uint64) (int64, error) {
var count int64 var count int64