Automatically set "Updated" date (Fixes #46)

This commit is contained in:
Jan-Lukas Else 2022-12-25 20:59:43 +01:00
parent a71b3cab69
commit 87e10cf68c
3 changed files with 70 additions and 8 deletions

View File

@ -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
}

View File

@ -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

View File

@ -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)
})
}