diff --git a/pkg/internal/http/api/posts_api.go b/pkg/internal/http/api/posts_api.go index 963934a..967e2ad 100644 --- a/pkg/internal/http/api/posts_api.go +++ b/pkg/internal/http/api/posts_api.go @@ -68,7 +68,13 @@ func getPost(c *fiber.Ctx) error { var item models.Post var err error - tx := services.FilterPostDraft(database.C) + tx := database.C + + if user, authenticated := c.Locals("user").(authm.Account); authenticated { + tx = services.FilterPostDraftWithAuthor(database.C, user.ID) + } else { + tx = services.FilterPostDraft(database.C) + } if user, authenticated := c.Locals("user").(authm.Account); authenticated { tx = services.FilterPostWithUserContext(c, tx, &user) diff --git a/pkg/internal/services/posts.go b/pkg/internal/services/posts.go index 592e3d4..d8fb923 100644 --- a/pkg/internal/services/posts.go +++ b/pkg/internal/services/posts.go @@ -290,6 +290,20 @@ func FilterPostDraft(tx *gorm.DB) *gorm.DB { return tx.Where("is_draft = ? OR is_draft IS NULL", false) } +func FilterPostDraftWithAuthor(tx *gorm.DB, uid uint) *gorm.DB { + 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("(is_draft = ? OR is_draft IS NULL) OR publisher_id IN ?", false, idSet) +} + func FilterPostWithFuzzySearch(tx *gorm.DB, probe string) *gorm.DB { if len(probe) == 0 { return tx