Photo counts on member directory + inner circle removal privacy fix

face-detect
Noah Petherbridge 2023-07-23 15:02:41 -07:00
parent 271af4e9a2
commit 61011efd9f
4 changed files with 60 additions and 5 deletions

View File

@ -100,6 +100,9 @@ func Search() http.HandlerFunc {
"AgeMin": ageMin,
"AgeMax": ageMax,
"Sort": sort,
// Photo counts mapped to users
"PhotoCountMap": models.MapPhotoCounts(users),
}
if err := tmpl.Execute(w, r, vars); err != nil {

View File

@ -145,6 +145,52 @@ func CountPhotos(userID uint64) int64 {
return count
}
// MapPhotoCounts returns a mapping of user ID to the CountPhotos()-equivalent result for each.
// It's used on the member directory to show photo counts on each user card.
func MapPhotoCounts(users []*User) PhotoCountMap {
var (
userIDs = []uint64{}
result = PhotoCountMap{}
)
for _, user := range users {
userIDs = append(userIDs, user.ID)
}
type group struct {
UserID uint64
PhotoCount int64
}
var groups = []group{}
if res := DB.Table(
"photos",
).Select(
"user_id, count(id) AS photo_count",
).Where(
"user_id IN ?", userIDs,
).Group("user_id").Scan(&groups); res.Error != nil {
log.Error("CountPhotosForUsers: %s", res.Error)
}
// Map the results in.
for _, row := range groups {
result[row.UserID] = row.PhotoCount
}
return result
}
type PhotoCountMap map[uint64]int64
// Get a photo count for the given user ID from the map.
func (pc PhotoCountMap) Get(id uint64) int64 {
if value, ok := pc[id]; ok {
return value
}
return 0
}
// CountExplicitPhotos returns the number of explicit photos a user has (so non-explicit viewers can see some do exist)
func CountExplicitPhotos(userID uint64, visibility []PhotoVisibility) (int64, error) {
query := DB.Where(

View File

@ -56,7 +56,7 @@ func RemoveFromInnerCircle(u *User) error {
if err := DB.Model(&Photo{}).Where(
"user_id = ? AND visibility = ?",
u.ID, PhotoInnerCircle,
).Update("visibility", PhotoPublic); err != nil {
).Update("visibility", PhotoFriends); err != nil {
log.Error("RemoveFromInnerCircle: couldn't update photo visibility: %s", err)
}

View File

@ -194,8 +194,8 @@
<span class="icon"><i class="fa fa-user"></i></span>
<a href="/u/{{.Username}}">{{.Username}}</a>
{{if not .Certified}}
<span class="has-text-danger">
<span class="icon"><i class="fa fa-certificate"></i></span>
<span class="has-text-danger is-size-7">
<i class="fa fa-certificate"></i>
<span>Not Certified!</span>
</span>
{{end}}
@ -204,11 +204,17 @@
{{if ne .Status "active"}}<small>({{.Status}})</small>{{end}}
{{if .IsAdmin}}
<span class="has-text-danger">
<span class="icon"><i class="fa fa-gavel"></i></span>
<span class="tag is-danger is-light p-1" style="font-size: x-small">
<i class="fa fa-gavel mr-1"></i>
<span>Admin</span>
</span>
{{end}}
<!-- Photo count pulled to the right -->
<a href="/photo/u/{{.Username}}" class="tag is-info is-light is-pulled-right">
<i class="fa fa-camera mr-2"></i>
{{$Root.PhotoCountMap.Get .ID}}
</a>
</p>
{{if .GetProfileField "city"}}
<p class="subtitle is-6 mb-2">