diff --git a/pkg/models/comment.go b/pkg/models/comment.go index 2319357..81dc339 100644 --- a/pkg/models/comment.go +++ b/pkg/models/comment.go @@ -1,6 +1,7 @@ package models import ( + "strings" "time" "code.nonshy.com/nonshy/website/pkg/log" @@ -71,11 +72,21 @@ func PaginateComments(user *User, tableName string, tableID uint64, pager *Pagin cs = []*Comment{} query = (&Comment{}).Preload() blockedUserIDs = BlockedUserIDs(user.ID) + wheres = []string{} + placeholders = []interface{}{} ) + 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 = query.Where( - "table_name = ? AND table_id = ? AND user_id NOT IN ?", - tableName, tableID, blockedUserIDs, + strings.Join(wheres, " AND "), + placeholders..., ).Order(pager.Sort) query.Model(&Comment{}).Count(&pager.Total) @@ -92,10 +103,21 @@ func ListComments(user *User, tableName string, tableID uint64) ([]*Comment, err var ( cs []*Comment blockedUserIDs = BlockedUserIDs(user.ID) + wheres = []string{} + placeholders = []interface{}{} ) + + 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) + } + result := (&Comment{}).Preload().Where( - "table_name = ? AND table_id = ? AND user_id NOT IN ?", - tableName, tableID, blockedUserIDs, + strings.Join(wheres, " AND "), + placeholders..., ).Order("created_at asc").Find(&cs) return cs, result.Error }