diff --git a/pkg/controller/photo/upload.go b/pkg/controller/photo/upload.go index 91b05da..66922a7 100644 --- a/pkg/controller/photo/upload.go +++ b/pkg/controller/photo/upload.go @@ -157,9 +157,15 @@ func notifyFriendsNewPhoto(photo *models.Photo, currentUser *models.User) { friendIDs = models.PrivateGranteeUserIDs(currentUser.ID) log.Info("Notify %d private grantees about the new photo by %s", len(friendIDs), currentUser.Username) } else { - // Friends - friendIDs = models.FriendIDs(currentUser.ID) - log.Info("Notify %d friends about the new photo by %s", len(friendIDs), currentUser.Username) + // 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) + log.Info("Notify %d friends about the new photo by %s", len(friendIDs), currentUser.Username) + } } for _, fid := range friendIDs { diff --git a/pkg/controller/photo/user_gallery.go b/pkg/controller/photo/user_gallery.go index f9c56e1..d4c1b8b 100644 --- a/pkg/controller/photo/user_gallery.go +++ b/pkg/controller/photo/user_gallery.go @@ -5,6 +5,7 @@ import ( "regexp" "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/session" "code.nonshy.com/nonshy/website/pkg/templates" @@ -114,6 +115,9 @@ func UserPhotos() http.HandlerFunc { } pager.ParsePage(r) 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. var explicitCount int64 diff --git a/pkg/models/friend.go b/pkg/models/friend.go index dd7b954..541465d 100644 --- a/pkg/models/friend.go +++ b/pkg/models/friend.go @@ -4,6 +4,7 @@ import ( "errors" "time" + "code.nonshy.com/nonshy/website/pkg/log" "gorm.io/gorm" ) @@ -104,6 +105,32 @@ func FriendIDs(userId uint64) []uint64 { 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. func CountFriendRequests(userID uint64) (int64, error) { var count int64