Don't show blocked users on Likes lists
This commit is contained in:
parent
37420e8051
commit
41beba54f2
|
@ -108,7 +108,7 @@ func Profile() http.HandlerFunc {
|
||||||
likeMap := models.MapLikes(currentUser, "users", []uint64{user.ID})
|
likeMap := models.MapLikes(currentUser, "users", []uint64{user.ID})
|
||||||
|
|
||||||
// Get the summary of WHO liked this picture.
|
// 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 {
|
if err != nil {
|
||||||
log.Error("WhoLikes(user %d): %s", user.ID, err)
|
log.Error("WhoLikes(user %d): %s", user.ID, err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -97,7 +97,7 @@ func View() http.HandlerFunc {
|
||||||
commentLikeMap := models.MapLikes(currentUser, "comments", commentIDs)
|
commentLikeMap := models.MapLikes(currentUser, "comments", commentIDs)
|
||||||
|
|
||||||
// Get the summary of WHO liked this picture.
|
// 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 {
|
if err != nil {
|
||||||
log.Error("WhoLikes(photo %d): %s", photo.ID, err)
|
log.Error("WhoLikes(photo %d): %s", photo.ID, err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"code.nonshy.com/nonshy/website/pkg/log"
|
"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.
|
// 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 (
|
var (
|
||||||
userIDs = []uint64{}
|
userIDs = []uint64{}
|
||||||
likes = []*Like{}
|
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)
|
total = CountLikes(tableName, tableID)
|
||||||
remainder = total - int64(len(likes))
|
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 {
|
if res.Error != nil {
|
||||||
return nil, 0, res.Error
|
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.
|
// Collect the user IDs to look up.
|
||||||
for _, row := range likes {
|
for _, row := range likes {
|
||||||
userIDs = append(userIDs, row.UserID)
|
userIDs = append(userIDs, row.UserID)
|
||||||
|
@ -98,11 +115,22 @@ func PaginateLikes(currentUser *User, tableName string, tableID uint64, pager *P
|
||||||
var (
|
var (
|
||||||
l = []*Like{}
|
l = []*Like{}
|
||||||
userIDs = []uint64{}
|
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(
|
query := DB.Where(
|
||||||
"table_name = ? AND table_id = ?",
|
strings.Join(wheres, " AND "),
|
||||||
tableName, tableID,
|
placeholders...,
|
||||||
).Order(
|
).Order(
|
||||||
pager.Sort,
|
pager.Sort,
|
||||||
)
|
)
|
||||||
|
|
|
@ -53,8 +53,7 @@
|
||||||
href="#"
|
href="#"
|
||||||
onclick="ShowLikeModal('{{.LikeTableName}}', {{.LikeTableID}}); return false"
|
onclick="ShowLikeModal('{{.LikeTableName}}', {{.LikeTableID}}); return false"
|
||||||
>
|
>
|
||||||
{{.LikeRemainder}} other{{Pluralize64 .LikeRemainder}}
|
{{.LikeRemainder}} other{{Pluralize64 .LikeRemainder}}</a>.
|
||||||
</a>.
|
|
||||||
{{end}}
|
{{end}}
|
||||||
</div>
|
</div>
|
||||||
{{end}}
|
{{end}}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user