Don't show blocked users on Likes lists

This commit is contained in:
Noah Petherbridge 2023-09-17 22:28:21 -07:00
parent 37420e8051
commit 41beba54f2
4 changed files with 44 additions and 17 deletions

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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,
)

View File

@ -53,8 +53,7 @@
href="#"
onclick="ShowLikeModal('{{.LikeTableName}}', {{.LikeTableID}}); return false"
>
{{.LikeRemainder}} other{{Pluralize64 .LikeRemainder}}
</a>.
{{.LikeRemainder}} other{{Pluralize64 .LikeRemainder}}</a>.
{{end}}
</div>
{{end}}