From c1255658967f8e3f56f93a890e0c7893b7afb4e6 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Tue, 10 Dec 2024 22:27:35 +0800 Subject: [PATCH] :necktie: Prevent user from setting published at before it real published date --- pkg/internal/services/posts.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pkg/internal/services/posts.go b/pkg/internal/services/posts.go index 6823940..7e4c51f 100644 --- a/pkg/internal/services/posts.go +++ b/pkg/internal/services/posts.go @@ -358,6 +358,9 @@ func NewPost(user models.Publisher, item models.Post) (models.Post, error) { if item.Alias != nil && len(*item.Alias) == 0 { item.Alias = nil } + if item.PublishedAt != nil && item.PublishedAt.Unix() < time.Now().Unix() { + return item, fmt.Errorf("post cannot be published before now") + } if item.Alias != nil { re := regexp.MustCompile(`^[a-z0-9.-]+$`) @@ -439,6 +442,9 @@ func EditPost(item models.Post) (models.Post, error) { if item.Alias != nil && len(*item.Alias) == 0 { item.Alias = nil } + if item.PublishedAt != nil && item.PublishedAt.Unix() < item.CreatedAt.Unix() { + return item, fmt.Errorf("post cannot be published before it is created") + } if item.Alias != nil { re := regexp.MustCompile(`^[a-z0-9.-]+$`)