diff --git a/pkg/controller/account/profile.go b/pkg/controller/account/profile.go index ff8d607..8cc22d6 100644 --- a/pkg/controller/account/profile.go +++ b/pkg/controller/account/profile.go @@ -108,7 +108,7 @@ func Profile() http.HandlerFunc { likeMap := models.MapLikes(currentUser, "users", []uint64{user.ID}) // Get the summary of WHO liked this picture. - likeExample, likeRemainder, err := models.WhoLikes("users", user.ID) + likeExample, likeRemainder, err := models.WhoLikes(currentUser, "users", user.ID) if err != nil { log.Error("WhoLikes(user %d): %s", user.ID, err) } diff --git a/pkg/controller/photo/view.go b/pkg/controller/photo/view.go index ede0a05..7cdf654 100644 --- a/pkg/controller/photo/view.go +++ b/pkg/controller/photo/view.go @@ -97,7 +97,7 @@ func View() http.HandlerFunc { commentLikeMap := models.MapLikes(currentUser, "comments", commentIDs) // Get the summary of WHO liked this picture. - likeExample, likeRemainder, err := models.WhoLikes("photos", photo.ID) + likeExample, likeRemainder, err := models.WhoLikes(currentUser, "photos", photo.ID) if err != nil { log.Error("WhoLikes(photo %d): %s", photo.ID, err) } diff --git a/pkg/models/like.go b/pkg/models/like.go index f144b97..418fd54 100644 --- a/pkg/models/like.go +++ b/pkg/models/like.go @@ -1,6 +1,7 @@ package models import ( + "strings" "time" "code.nonshy.com/nonshy/website/pkg/log" @@ -64,21 +65,37 @@ func CountLikes(tableName string, tableID uint64) int64 { } // WhoLikes something. Returns the first couple users and a count of the remainder. -func WhoLikes(tableName string, tableID uint64) ([]*User, int64, error) { +func WhoLikes(currentUser *User, tableName string, tableID uint64) ([]*User, int64, error) { var ( - userIDs = []uint64{} - likes = []*Like{} - res = DB.Model(&Like{}).Where( - "table_name = ? AND table_id = ?", - tableName, tableID, - ).Order("created_at DESC").Limit(2).Scan(&likes) - total = CountLikes(tableName, tableID) - remainder = total - int64(len(likes)) + userIDs = []uint64{} + likes = []*Like{} + total = CountLikes(tableName, tableID) + remainder = total + wheres = []string{} + placeholders = []interface{}{} + blockedUserIDs = BlockedUserIDs(currentUser.ID) ) + + wheres = append(wheres, "table_name = ? AND table_id = ?") + placeholders = append(placeholders, tableName, tableID) + + if len(blockedUserIDs) > 0 { + wheres = append(wheres, "user_id NOT IN ?") + placeholders = append(placeholders, blockedUserIDs) + } + + res := DB.Model(&Like{}).Where( + strings.Join(wheres, " AND "), + placeholders..., + ).Order("created_at DESC").Limit(2).Scan(&likes) + if res.Error != nil { return nil, 0, res.Error } + // Subtract the (up to two) likes from the total. + remainder -= int64(len(likes)) + // Collect the user IDs to look up. for _, row := range likes { userIDs = append(userIDs, row.UserID) @@ -96,13 +113,24 @@ func WhoLikes(tableName string, tableID uint64) ([]*User, int64, error) { // PaginateLikes returns a paged view of users who've liked something. func PaginateLikes(currentUser *User, tableName string, tableID uint64, pager *Pagination) ([]*User, error) { var ( - l = []*Like{} - userIDs = []uint64{} + l = []*Like{} + userIDs = []uint64{} + wheres = []string{} + placeholders = []interface{}{} + blockedUserIDs = BlockedUserIDs(currentUser.ID) ) + wheres = append(wheres, "table_name = ? AND table_id = ?") + placeholders = append(placeholders, tableName, tableID) + + if len(blockedUserIDs) > 0 { + wheres = append(wheres, "user_id NOT IN ?") + placeholders = append(placeholders, blockedUserIDs) + } + query := DB.Where( - "table_name = ? AND table_id = ?", - tableName, tableID, + strings.Join(wheres, " AND "), + placeholders..., ).Order( pager.Sort, ) diff --git a/web/templates/partials/like_modal.html b/web/templates/partials/like_modal.html index 2f3f17d..396c9ce 100644 --- a/web/templates/partials/like_modal.html +++ b/web/templates/partials/like_modal.html @@ -53,8 +53,7 @@ href="#" onclick="ShowLikeModal('{{.LikeTableName}}', {{.LikeTableID}}); return false" > - {{.LikeRemainder}} other{{Pluralize64 .LikeRemainder}} - . + {{.LikeRemainder}} other{{Pluralize64 .LikeRemainder}}. {{end}} {{end}}