From 87e10cf68cab69cfb18efddb9f73d692b2578dda Mon Sep 17 00:00:00 2001 From: Jan-Lukas Else Date: Sun, 25 Dec 2022 20:59:43 +0100 Subject: [PATCH] Automatically set "Updated" date (Fixes #46) --- editor.go | 2 +- postsDb.go | 22 +++++++++++++------- postsDb_test.go | 54 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 8 deletions(-) diff --git a/editor.go b/editor.go index bfb3eee..0fe156b 100644 --- a/editor.go +++ b/editor.go @@ -73,7 +73,7 @@ func (a *goBlog) createMarkdownPreview(w io.Writer, blog string, markdown io.Rea _, _ = io.WriteString(w, err.Error()) return } - if err := a.checkPost(p); err != nil { + if err := a.checkPost(p, true); err != nil { _, _ = io.WriteString(w, err.Error()) return } diff --git a/postsDb.go b/postsDb.go index 325dfc1..ecf0287 100644 --- a/postsDb.go +++ b/postsDb.go @@ -14,11 +14,12 @@ import ( "go.goblog.app/app/pkgs/bufferpool" ) -func (a *goBlog) checkPost(p *post) (err error) { +func (a *goBlog) checkPost(p *post, new bool) (err error) { if p == nil { return errors.New("no post") } now := time.Now().Local() + nowString := now.Format(time.RFC3339) // Maybe add blog if p.Blog == "" { p.Blog = a.cfg.DefaultBlog @@ -38,11 +39,6 @@ func (a *goBlog) checkPost(p *post) (err error) { return errors.New("section doesn't exist") } } - // Maybe add published date - if p.Published == "" && p.Section != "" { - // Has no published date, but section -> published now - p.Published = now.Format(time.RFC3339) - } // Fix and check date strings if p.Published != "" { p.Published, err = toLocal(p.Published) @@ -56,6 +52,18 @@ func (a *goBlog) checkPost(p *post) (err error) { return err } } + // Maybe set published date + if new && p.Published == "" && p.Section != "" { + // Has no published date, but section -> published now + p.Published = nowString + } + // Maybe set updated date + if !new && p.Published != "" { + if published, err := dateparse.ParseLocal(p.Published); err == nil && now.After(published) { + // Has published date in the past, so add updated date + p.Updated = nowString + } + } // Fix content p.Content = strings.TrimSuffix(strings.TrimPrefix(p.Content, "\n"), "\n") // Check status @@ -159,7 +167,7 @@ type postCreationOptions struct { func (a *goBlog) createOrReplacePost(p *post, o *postCreationOptions) error { // Check post - if err := a.checkPost(p); err != nil { + if err := a.checkPost(p, o.new); err != nil { return err } // Save to db diff --git a/postsDb_test.go b/postsDb_test.go index 0e11804..7c950d2 100644 --- a/postsDb_test.go +++ b/postsDb_test.go @@ -425,3 +425,57 @@ func Test_postDeletesParams(t *testing.T) { assert.Equal(t, 0, count) } + +func Test_checkPost(t *testing.T) { + app := &goBlog{ + cfg: createDefaultTestConfig(t), + } + _ = app.initConfig(false) + + t.Run("New post should get published date", func(t *testing.T) { + p := &post{} + app.checkPost(p, true) + + assert.NotEmpty(t, p.Published) + }) + + t.Run("New post with path should get no published date", func(t *testing.T) { + p := &post{ + Path: "/abc", + } + app.checkPost(p, true) + + assert.Empty(t, p.Published) + }) + + t.Run("Updated post with past published date should get updated date", func(t *testing.T) { + p := &post{ + Published: time.Now().Local().Add(-1 * time.Hour).Format(time.RFC3339), + } + app.checkPost(p, false) + + assert.NotEmpty(t, p.Updated) + }) + + t.Run("Updated post with future published date should get no updated date", func(t *testing.T) { + p := &post{ + Published: time.Now().Local().Add(time.Hour).Format(time.RFC3339), + } + app.checkPost(p, false) + + assert.Empty(t, p.Updated) + }) + + t.Run("Updated post with updated date should get new updated date", func(t *testing.T) { + oldUpdate := time.Now().Local().Add(-1 * time.Hour).Format(time.RFC3339) + p := &post{ + Published: time.Now().Local().Add(-2 * time.Hour).Format(time.RFC3339), + Updated: oldUpdate, + } + app.checkPost(p, false) + + assert.NotEmpty(t, p.Updated) + assert.NotEqual(t, oldUpdate, p.Updated) + }) + +}