🐛 Fix some visibility issues
This commit is contained in:
parent
390ac7e883
commit
5630988ac2
@ -4,6 +4,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"git.solsynth.dev/hypernet/nexus/pkg/nex/cruda"
|
"git.solsynth.dev/hypernet/nexus/pkg/nex/cruda"
|
||||||
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
|
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
|
||||||
@ -20,23 +21,39 @@ import (
|
|||||||
"github.com/samber/lo"
|
"github.com/samber/lo"
|
||||||
)
|
)
|
||||||
|
|
||||||
func UniversalPostFilter(c *fiber.Ctx, tx *gorm.DB) (*gorm.DB, error) {
|
type UniversalPostFilterConfig struct {
|
||||||
|
ShowDraft bool
|
||||||
|
ShowReply bool
|
||||||
|
ShowCollapsed bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func UniversalPostFilter(c *fiber.Ctx, tx *gorm.DB, cfg ...UniversalPostFilterConfig) (*gorm.DB, error) {
|
||||||
|
var config UniversalPostFilterConfig
|
||||||
|
if len(cfg) > 0 {
|
||||||
|
config = cfg[0]
|
||||||
|
} else {
|
||||||
|
config = UniversalPostFilterConfig{}
|
||||||
|
}
|
||||||
|
|
||||||
if user, authenticated := c.Locals("user").(authm.Account); authenticated {
|
if user, authenticated := c.Locals("user").(authm.Account); authenticated {
|
||||||
tx = services.FilterPostWithUserContext(c, tx, &user)
|
tx = services.FilterPostWithUserContext(c, tx, &user)
|
||||||
if c.QueryBool("noDraft", true) {
|
if c.QueryBool("noDraft", true) && !config.ShowDraft {
|
||||||
tx = services.FilterPostDraft(tx)
|
tx = services.FilterPostDraft(tx)
|
||||||
|
tx = services.FilterPostWithPublishedAt(tx, time.Now())
|
||||||
} else {
|
} else {
|
||||||
tx = services.FilterPostDraftWithAuthor(database.C, user.ID)
|
tx = services.FilterPostDraftWithAuthor(database.C, user.ID)
|
||||||
|
tx = services.FilterPostWithPublishedAt(tx, time.Now(), user.ID)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
tx = services.FilterPostWithUserContext(c, tx, nil)
|
tx = services.FilterPostWithUserContext(c, tx, nil)
|
||||||
tx = services.FilterPostDraft(tx)
|
tx = services.FilterPostDraft(tx)
|
||||||
|
tx = services.FilterPostWithPublishedAt(tx, time.Now())
|
||||||
}
|
}
|
||||||
|
|
||||||
if c.QueryBool("noReply", true) {
|
if c.QueryBool("noReply", true) && !config.ShowReply {
|
||||||
tx = services.FilterPostReply(tx)
|
tx = services.FilterPostReply(tx)
|
||||||
}
|
}
|
||||||
if c.QueryBool("noCollapse", true) {
|
if c.QueryBool("noCollapse", true) && !config.ShowCollapsed {
|
||||||
tx = tx.Where("is_collapsed = ? OR is_collapsed IS NULL", false)
|
tx = tx.Where("is_collapsed = ? OR is_collapsed IS NULL", false)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -74,7 +91,10 @@ func getPost(c *fiber.Ctx) error {
|
|||||||
|
|
||||||
tx := database.C
|
tx := database.C
|
||||||
|
|
||||||
if tx, err = UniversalPostFilter(c, tx); err != nil {
|
if tx, err = UniversalPostFilter(c, tx, UniversalPostFilterConfig{
|
||||||
|
ShowReply: true,
|
||||||
|
ShowDraft: true,
|
||||||
|
}); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -120,7 +140,9 @@ func searchPost(c *fiber.Ctx) error {
|
|||||||
tx = services.FilterPostWithFuzzySearch(tx, probe)
|
tx = services.FilterPostWithFuzzySearch(tx, probe)
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
if tx, err = UniversalPostFilter(c, tx); err != nil {
|
if tx, err = UniversalPostFilter(c, tx, UniversalPostFilterConfig{
|
||||||
|
ShowReply: true,
|
||||||
|
}); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -276,14 +276,30 @@ func FilterPostReply(tx *gorm.DB, replyTo ...uint) *gorm.DB {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func FilterPostWithPublishedAt(tx *gorm.DB, date time.Time) *gorm.DB {
|
func FilterPostWithPublishedAt(tx *gorm.DB, date time.Time, uid ...uint) *gorm.DB {
|
||||||
|
var publishers []models.Publisher
|
||||||
|
if len(uid) > 0 {
|
||||||
|
if err := database.C.Where("account_id = ?", uid[0]).Find(&publishers).Error; err == nil {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return tx.
|
return tx.
|
||||||
Where("(published_at >= ? OR published_at IS NULL)", date).
|
Where("(published_at < ? OR published_at IS NULL)", date).
|
||||||
Where("(published_until < ? OR published_until IS NULL)", date)
|
Where("(published_until >= ? OR published_until IS NULL)", date)
|
||||||
}
|
}
|
||||||
|
|
||||||
func FilterPostWithAuthorDraft(tx *gorm.DB, uid uint) *gorm.DB {
|
func FilterPostWithAuthorDraft(tx *gorm.DB, uid uint) *gorm.DB {
|
||||||
return tx.Where("publisher_id = ? AND is_draft = ?", uid, true)
|
var publishers []models.Publisher
|
||||||
|
if err := database.C.Where("account_id = ?", uid).Find(&publishers).Error; err != nil {
|
||||||
|
return FilterPostDraft(tx)
|
||||||
|
}
|
||||||
|
if len(publishers) == 0 {
|
||||||
|
return FilterPostDraft(tx)
|
||||||
|
}
|
||||||
|
idSet := lo.Map(publishers, func(item models.Publisher, index int) uint {
|
||||||
|
return item.ID
|
||||||
|
})
|
||||||
|
return tx.Where("publisher_id IN ? AND is_draft = ?", idSet, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
func FilterPostDraft(tx *gorm.DB) *gorm.DB {
|
func FilterPostDraft(tx *gorm.DB) *gorm.DB {
|
||||||
@ -337,11 +353,7 @@ func PreloadGeneral(tx *gorm.DB) *gorm.DB {
|
|||||||
Preload("RepostTo.Categories")
|
Preload("RepostTo.Categories")
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetPost(tx *gorm.DB, id uint, ignoreLimitation ...bool) (models.Post, error) {
|
func GetPost(tx *gorm.DB, id uint) (models.Post, error) {
|
||||||
if len(ignoreLimitation) == 0 || !ignoreLimitation[0] {
|
|
||||||
tx = FilterPostWithPublishedAt(tx, time.Now())
|
|
||||||
}
|
|
||||||
|
|
||||||
var item models.Post
|
var item models.Post
|
||||||
if err := PreloadGeneral(tx).
|
if err := PreloadGeneral(tx).
|
||||||
Where("id = ?", id).
|
Where("id = ?", id).
|
||||||
@ -352,11 +364,7 @@ func GetPost(tx *gorm.DB, id uint, ignoreLimitation ...bool) (models.Post, error
|
|||||||
return item, nil
|
return item, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetPostByAlias(tx *gorm.DB, alias, area string, ignoreLimitation ...bool) (models.Post, error) {
|
func GetPostByAlias(tx *gorm.DB, alias, area string) (models.Post, error) {
|
||||||
if len(ignoreLimitation) == 0 || !ignoreLimitation[0] {
|
|
||||||
tx = FilterPostWithPublishedAt(tx, time.Now())
|
|
||||||
}
|
|
||||||
|
|
||||||
var item models.Post
|
var item models.Post
|
||||||
if err := PreloadGeneral(tx).
|
if err := PreloadGeneral(tx).
|
||||||
Where("alias = ?", alias).
|
Where("alias = ?", alias).
|
||||||
|
Loading…
x
Reference in New Issue
Block a user