Tweak private share badges

This commit is contained in:
Noah Petherbridge 2023-09-01 23:07:15 -07:00
parent 2d7f8c0d87
commit 6b0246edad
3 changed files with 84 additions and 15 deletions

View File

@ -44,10 +44,20 @@ func Private() http.HandlerFunc {
log.Error("pager: %+v, len: %d", pager, len(users)) 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{}{ var vars = map[string]interface{}{
"IsGrantee": isGrantee, "IsGrantee": isGrantee,
"CountGrantee": models.CountPrivateGrantee(currentUser.ID), "CountGrantee": models.CountPrivateGrantee(currentUser.ID),
"GranteeMap": models.MapPrivatePhotoGrantee(currentUser, users), "GranteeMap": GranteeMap,
"Users": users, "Users": users,
"Pager": pager, "Pager": pager,
} }

View File

@ -240,7 +240,7 @@ func (pb *PrivatePhoto) Save() error {
// PrivateGranteeMap maps user IDs to whether they have granted you their private photos. // PrivateGranteeMap maps user IDs to whether they have granted you their private photos.
type PrivateGranteeMap map[uint64]bool 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 { func MapPrivatePhotoGrantee(currentUser *User, users []*User) PrivateGranteeMap {
var ( var (
usermap = PrivateGranteeMap{} usermap = PrivateGranteeMap{}
@ -277,3 +277,44 @@ func MapPrivatePhotoGrantee(currentUser *User, users []*User) PrivateGranteeMap
func (um PrivateGranteeMap) Get(id uint64) bool { func (um PrivateGranteeMap) Get(id uint64) bool {
return um[id] 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]
}

View File

@ -110,11 +110,13 @@
</p> </p>
<!-- Indicator if they are sharing back --> <!-- Indicator if they are sharing back -->
{{if not $Root.IsGrantee}}
<!-- "My Shares" tab - GranteeMap is whether they granted me back -->
<div> <div>
{{if $Root.GranteeMap.Get .ID}} {{if $Root.GranteeMap.Get .ID}}
<span class="has-text-success"> <span class="has-text-success">
<i class="fa fa-check mr-1"></i> <i class="fa fa-check mr-1"></i>
Sharing with me too Sharing with me
</span> </span>
{{else}} {{else}}
<span class="has-text-danger"> <span class="has-text-danger">
@ -123,6 +125,22 @@
</span> </span>
{{end}} {{end}}
</div> </div>
{{else}}
<!-- "Shared With Me" tab - GranteeMap is whether I granted them -->
<div>
{{if $Root.GranteeMap.Get .ID}}
<span class="has-text-success">
<i class="fa fa-check mr-1"></i>
I share with them
</span>
{{else}}
<span class="has-text-danger">
<i class="fa fa-xmark mr-1"></i>
I do not share with them
</span>
{{end}}
</div>
{{end}}
</div> </div>
</div> </div>
</div> </div>