diff --git a/pkg/controller/account/search.go b/pkg/controller/account/search.go index b0fcf85..cc52a56 100644 --- a/pkg/controller/account/search.go +++ b/pkg/controller/account/search.go @@ -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 { diff --git a/pkg/models/photo.go b/pkg/models/photo.go index ebecf73..f98bc61 100644 --- a/pkg/models/photo.go +++ b/pkg/models/photo.go @@ -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( diff --git a/pkg/models/user_inner_circle.go b/pkg/models/user_inner_circle.go index bac7403..9c17318 100644 --- a/pkg/models/user_inner_circle.go +++ b/pkg/models/user_inner_circle.go @@ -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) } diff --git a/web/templates/account/search.html b/web/templates/account/search.html index 4b2de64..99d0feb 100644 --- a/web/templates/account/search.html +++ b/web/templates/account/search.html @@ -194,8 +194,8 @@ {{.Username}} {{if not .Certified}} - - + + Not Certified! {{end}} @@ -204,11 +204,17 @@ {{if ne .Status "active"}}({{.Status}}){{end}} {{if .IsAdmin}} - - + + Admin {{end}} + + + + + {{$Root.PhotoCountMap.Get .ID}} +

{{if .GetProfileField "city"}}