Private photos: see who shares back

This commit is contained in:
Noah Petherbridge 2023-09-01 22:27:18 -07:00
parent 577c92386e
commit 944dbb749b
3 changed files with 47 additions and 1 deletions

View File

@ -45,6 +45,7 @@ func Private() http.HandlerFunc {
var vars = map[string]interface{}{ var vars = map[string]interface{}{
"IsGrantee": isGrantee, "IsGrantee": isGrantee,
"CountGrantee": models.CountPrivateGrantee(currentUser.ID), "CountGrantee": models.CountPrivateGrantee(currentUser.ID),
"GranteeMap": models.MapPrivatePhotoGrantee(currentUser, users),
"Users": users, "Users": users,
"Pager": pager, "Pager": pager,
} }

View File

@ -226,3 +226,33 @@ func (pb *PrivatePhoto) Save() error {
result := DB.Save(pb) result := DB.Save(pb)
return result.Error 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]
}

View File

@ -91,7 +91,7 @@
<p class="title is-4"> <p class="title is-4">
<a href="/u/{{.Username}}" class="has-text-dark">{{.NameOrUsername}}</a> <a href="/u/{{.Username}}" class="has-text-dark">{{.NameOrUsername}}</a>
</p> </p>
<p class="subtitle is-6"> <p class="subtitle is-6 mb-1">
<span class="icon"><i class="fa fa-user"></i></span> <span class="icon"><i class="fa fa-user"></i></span>
<a href="/u/{{.Username}}">{{.Username}}</a> <a href="/u/{{.Username}}">{{.Username}}</a>
{{if not .Certified}} {{if not .Certified}}
@ -108,6 +108,21 @@
</span> </span>
{{end}} {{end}}
</p> </p>
<!-- Indicator if they are sharing back -->
<div>
{{if $Root.GranteeMap.Get .ID}}
<span class="has-text-success">
<i class="fa fa-check mr-1"></i>
Sharing with me too
</span>
{{else}}
<span class="has-text-danger">
<i class="fa fa-xmark mr-1"></i>
Not sharing with me
</span>
{{end}}
</div>
</div> </div>
</div> </div>
</div> </div>