Don't show banned friends on friend page

pull/38/head
Noah Petherbridge 2024-02-14 20:25:24 -08:00
parent 7da650ffc4
commit 7ceb14053b
1 changed files with 76 additions and 30 deletions

View File

@ -6,7 +6,6 @@ import (
"time" "time"
"code.nonshy.com/nonshy/website/pkg/log" "code.nonshy.com/nonshy/website/pkg/log"
"gorm.io/gorm"
) )
// Friend table. // Friend table.
@ -249,11 +248,19 @@ func FriendIDsInCircleAreExplicit(userId uint64) []uint64 {
// CountFriendRequests gets a count of pending requests for the user. // CountFriendRequests gets a count of pending requests for the user.
func CountFriendRequests(userID uint64) (int64, error) { func CountFriendRequests(userID uint64) (int64, error) {
var count int64 var (
count int64
wheres = []string{
"target_user_id = ? AND approved = ? AND ignored IS NOT true",
"EXISTS (SELECT 1 FROM users WHERE users.id = source_user_id AND users.status = 'active')",
}
placeholders = []interface{}{
userID, false,
}
)
result := DB.Where( result := DB.Where(
"target_user_id = ? AND approved = ? AND ignored IS NOT true", strings.Join(wheres, " AND "),
userID, placeholders...,
false,
).Model(&Friend{}).Count(&count) ).Model(&Friend{}).Count(&count)
return count, result.Error return count, result.Error
} }
@ -262,7 +269,7 @@ func CountFriendRequests(userID uint64) (int64, error) {
func CountIgnoredFriendRequests(userID uint64) (int64, error) { func CountIgnoredFriendRequests(userID uint64) (int64, error) {
var count int64 var count int64
result := DB.Where( result := DB.Where(
"target_user_id = ? AND approved = ? AND ignored = ?", "target_user_id = ? AND approved = ? AND ignored = ? AND EXISTS (SELECT 1 FROM users WHERE users.id = friends.source_user_id AND users.status = 'active')",
userID, userID,
false, false,
true, true,
@ -295,38 +302,77 @@ have sent and have not been answered.
func PaginateFriends(user *User, requests bool, sent bool, ignored bool, pager *Pagination) ([]*User, error) { func PaginateFriends(user *User, requests bool, sent bool, ignored bool, pager *Pagination) ([]*User, error) {
// We paginate over the Friend table. // We paginate over the Friend table.
var ( var (
fs = []*Friend{} fs = []*Friend{}
userIDs = []uint64{} userIDs = []uint64{}
query *gorm.DB blockedUserIDs = BlockedUserIDs(user)
wheres = []string{}
placeholders = []interface{}{}
query = DB.Model(&Friend{})
) )
if requests && sent && ignored { if requests && sent && ignored {
return nil, errors.New("requests and sent are mutually exclusive options, use one or neither") return nil, errors.New("requests and sent are mutually exclusive options, use one or neither")
} }
if requests { // Don't show our blocked users in the result.
query = DB.Where( if len(blockedUserIDs) > 0 {
"target_user_id = ? AND approved = ? AND ignored IS NOT true", wheres = append(wheres, "target_user_id NOT IN ?")
user.ID, false, placeholders = append(placeholders, blockedUserIDs)
)
} else if sent {
query = DB.Where(
"source_user_id = ? AND approved = ? AND ignored IS NOT true",
user.ID, false,
)
} else if ignored {
query = DB.Where(
"target_user_id = ? AND approved = ? AND ignored = ?",
user.ID, false, true,
)
} else {
query = DB.Where(
"source_user_id = ? AND approved = ?",
user.ID, true,
)
} }
query = query.Order(pager.Sort) // Don't show disabled or banned users.
var (
// Source user is banned (Requests, Ignored tabs)
bannedWhereRequest = `
EXISTS (
SELECT 1
FROM users
WHERE users.id = friends.source_user_id
AND users.status = 'active'
)
`
// Target user is banned (Friends, Sent tabs)
bannedWhereFriend = `
EXISTS (
SELECT 1
FROM users
WHERE users.id = friends.target_user_id
AND users.status = 'active'
)
`
)
if requests {
wheres = append(wheres, "target_user_id = ? AND approved = ? AND ignored IS NOT true")
placeholders = append(placeholders, user.ID, false)
// Don't show friend requests from currently banned/disabled users.
wheres = append(wheres, bannedWhereRequest)
} else if sent {
wheres = append(wheres, "source_user_id = ? AND approved = ? AND ignored IS NOT true")
placeholders = append(placeholders, user.ID, false)
// Don't show friends who are currently banned/disabled.
wheres = append(wheres, bannedWhereFriend)
} else if ignored {
wheres = append(wheres, "target_user_id = ? AND approved = ? AND ignored = ?")
placeholders = append(placeholders, user.ID, false, true)
// Don't show friend requests from currently banned/disabled users.
wheres = append(wheres, bannedWhereRequest)
} else {
wheres = append(wheres, "source_user_id = ? AND approved = ?")
placeholders = append(placeholders, user.ID, true)
// Don't show friends who are currently banned/disabled.
wheres = append(wheres, bannedWhereFriend)
}
query = query.Where(
strings.Join(wheres, " AND "),
placeholders...,
).Order(pager.Sort)
query.Model(&Friend{}).Count(&pager.Total) query.Model(&Friend{}).Count(&pager.Total)
result := query.Offset(pager.GetOffset()).Limit(pager.PerPage).Find(&fs) result := query.Offset(pager.GetOffset()).Limit(pager.PerPage).Find(&fs)
if result.Error != nil { if result.Error != nil {