From 944dbb749b988b3f23fc53497a0d3395cb112101 Mon Sep 17 00:00:00 2001 From: Noah Petherbridge Date: Fri, 1 Sep 2023 22:27:18 -0700 Subject: [PATCH] Private photos: see who shares back --- pkg/controller/photo/private.go | 1 + pkg/models/private_photo.go | 30 ++++++++++++++++++++++++++++++ web/templates/photo/private.html | 17 ++++++++++++++++- 3 files changed, 47 insertions(+), 1 deletion(-) diff --git a/pkg/controller/photo/private.go b/pkg/controller/photo/private.go index 7c58bd8..f6434af 100644 --- a/pkg/controller/photo/private.go +++ b/pkg/controller/photo/private.go @@ -45,6 +45,7 @@ func Private() http.HandlerFunc { var vars = map[string]interface{}{ "IsGrantee": isGrantee, "CountGrantee": models.CountPrivateGrantee(currentUser.ID), + "GranteeMap": models.MapPrivatePhotoGrantee(currentUser, users), "Users": users, "Pager": pager, } diff --git a/pkg/models/private_photo.go b/pkg/models/private_photo.go index b332b26..a743452 100644 --- a/pkg/models/private_photo.go +++ b/pkg/models/private_photo.go @@ -226,3 +226,33 @@ func (pb *PrivatePhoto) Save() error { result := DB.Save(pb) return result.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. +func MapPrivatePhotoGrantee(currentUser *User, users []*User) PrivateGranteeMap { + var ( + usermap = PrivateGranteeMap{} + ) + + var ( + matched = []*PrivatePhoto{} + result = DB.Model(&PrivatePhoto{}). + Where("target_user_id = ?", currentUser.ID). + Find(&matched) + ) + + if result.Error == nil { + for _, row := range matched { + usermap[row.SourceUserID] = true + } + } + + return usermap +} + +// Get a user from the PrivateGranteeMap. +func (um PrivateGranteeMap) Get(id uint64) bool { + return um[id] +} diff --git a/web/templates/photo/private.html b/web/templates/photo/private.html index 23a9e12..c423d29 100644 --- a/web/templates/photo/private.html +++ b/web/templates/photo/private.html @@ -91,7 +91,7 @@

{{.NameOrUsername}}

-

+

{{.Username}} {{if not .Certified}} @@ -108,6 +108,21 @@ {{end}}

+ + +
+ {{if $Root.GranteeMap.Get .ID}} + + + Sharing with me too + + {{else}} + + + Not sharing with me + + {{end}} +