From 6b0246edad1713c2fbe9d5f1791fb1db4029050f Mon Sep 17 00:00:00 2001 From: Noah Petherbridge Date: Fri, 1 Sep 2023 23:07:15 -0700 Subject: [PATCH] Tweak private share badges --- pkg/controller/photo/private.go | 12 ++++++++- pkg/models/private_photo.go | 43 ++++++++++++++++++++++++++++++- web/templates/photo/private.html | 44 ++++++++++++++++++++++---------- 3 files changed, 84 insertions(+), 15 deletions(-) diff --git a/pkg/controller/photo/private.go b/pkg/controller/photo/private.go index 90ea1b2..7b75eeb 100644 --- a/pkg/controller/photo/private.go +++ b/pkg/controller/photo/private.go @@ -44,10 +44,20 @@ func Private() http.HandlerFunc { log.Error("pager: %+v, len: %d", pager, len(users)) + // Map reverse grantee statuses. + var GranteeMap interface{} + if isGrantee { + // Shared With Me page: map whether we grant them shares back. + GranteeMap = models.MapPrivatePhotoGranted(currentUser, users) + } else { + // My Shares page: map whether they share back with us. + GranteeMap = models.MapPrivatePhotoGrantee(currentUser, users) + } + var vars = map[string]interface{}{ "IsGrantee": isGrantee, "CountGrantee": models.CountPrivateGrantee(currentUser.ID), - "GranteeMap": models.MapPrivatePhotoGrantee(currentUser, users), + "GranteeMap": GranteeMap, "Users": users, "Pager": pager, } diff --git a/pkg/models/private_photo.go b/pkg/models/private_photo.go index ab7c45b..e3d9865 100644 --- a/pkg/models/private_photo.go +++ b/pkg/models/private_photo.go @@ -240,7 +240,7 @@ func (pb *PrivatePhoto) Save() error { // PrivateGranteeMap maps user IDs to whether they have granted you their private photos. type PrivateGranteeMap map[uint64]bool -// MapShyAccounts looks up a set of user IDs in bulk and returns a PrivateGranteeMap suitable for templates. +// MapPrivatePhotoGrantee looks up a set of user IDs in bulk and returns a PrivateGranteeMap suitable for templates. func MapPrivatePhotoGrantee(currentUser *User, users []*User) PrivateGranteeMap { var ( usermap = PrivateGranteeMap{} @@ -277,3 +277,44 @@ func MapPrivatePhotoGrantee(currentUser *User, users []*User) PrivateGranteeMap func (um PrivateGranteeMap) Get(id uint64) bool { return um[id] } + +// PrivateGrantedMap maps user IDs to whether we have granted our private pictures to them. +type PrivateGrantedMap map[uint64]bool + +// MapPrivatePhotoGranted looks up a set of user IDs in bulk and returns a PrivateGrantedMap suitable for templates. +func MapPrivatePhotoGranted(currentUser *User, users []*User) PrivateGrantedMap { + var ( + usermap = PrivateGrantedMap{} + 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 ( + matched = []*PrivatePhoto{} + result = DB.Model(&PrivatePhoto{}). + Where("source_user_id = ? AND target_user_id IN ?", currentUser.ID, distinct). + Find(&matched) + ) + + if result.Error == nil { + for _, row := range matched { + usermap[row.TargetUserID] = true + } + } + + return usermap +} + +// Get a user from the PrivateGrantedMap. +func (um PrivateGrantedMap) Get(id uint64) bool { + return um[id] +} diff --git a/web/templates/photo/private.html b/web/templates/photo/private.html index c423d29..c19ca21 100644 --- a/web/templates/photo/private.html +++ b/web/templates/photo/private.html @@ -110,19 +110,37 @@

-
- {{if $Root.GranteeMap.Get .ID}} - - - Sharing with me too - - {{else}} - - - Not sharing with me - - {{end}} -
+ {{if not $Root.IsGrantee}} + +
+ {{if $Root.GranteeMap.Get .ID}} + + + Sharing with me + + {{else}} + + + Not sharing with me + + {{end}} +
+ {{else}} + +
+ {{if $Root.GranteeMap.Get .ID}} + + + I share with them + + {{else}} + + + I do not share with them + + {{end}} +
+ {{end}}