mirror of https://github.com/jlelse/GoBlog
Automatically set "Updated" date (Fixes #46)
This commit is contained in:
parent
a71b3cab69
commit
87e10cf68c
|
@ -73,7 +73,7 @@ func (a *goBlog) createMarkdownPreview(w io.Writer, blog string, markdown io.Rea
|
||||||
_, _ = io.WriteString(w, err.Error())
|
_, _ = io.WriteString(w, err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err := a.checkPost(p); err != nil {
|
if err := a.checkPost(p, true); err != nil {
|
||||||
_, _ = io.WriteString(w, err.Error())
|
_, _ = io.WriteString(w, err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
22
postsDb.go
22
postsDb.go
|
@ -14,11 +14,12 @@ import (
|
||||||
"go.goblog.app/app/pkgs/bufferpool"
|
"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 {
|
if p == nil {
|
||||||
return errors.New("no post")
|
return errors.New("no post")
|
||||||
}
|
}
|
||||||
now := time.Now().Local()
|
now := time.Now().Local()
|
||||||
|
nowString := now.Format(time.RFC3339)
|
||||||
// Maybe add blog
|
// Maybe add blog
|
||||||
if p.Blog == "" {
|
if p.Blog == "" {
|
||||||
p.Blog = a.cfg.DefaultBlog
|
p.Blog = a.cfg.DefaultBlog
|
||||||
|
@ -38,11 +39,6 @@ func (a *goBlog) checkPost(p *post) (err error) {
|
||||||
return errors.New("section doesn't exist")
|
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
|
// Fix and check date strings
|
||||||
if p.Published != "" {
|
if p.Published != "" {
|
||||||
p.Published, err = toLocal(p.Published)
|
p.Published, err = toLocal(p.Published)
|
||||||
|
@ -56,6 +52,18 @@ func (a *goBlog) checkPost(p *post) (err error) {
|
||||||
return err
|
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
|
// Fix content
|
||||||
p.Content = strings.TrimSuffix(strings.TrimPrefix(p.Content, "\n"), "\n")
|
p.Content = strings.TrimSuffix(strings.TrimPrefix(p.Content, "\n"), "\n")
|
||||||
// Check status
|
// Check status
|
||||||
|
@ -159,7 +167,7 @@ type postCreationOptions struct {
|
||||||
|
|
||||||
func (a *goBlog) createOrReplacePost(p *post, o *postCreationOptions) error {
|
func (a *goBlog) createOrReplacePost(p *post, o *postCreationOptions) error {
|
||||||
// Check post
|
// Check post
|
||||||
if err := a.checkPost(p); err != nil {
|
if err := a.checkPost(p, o.new); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// Save to db
|
// Save to db
|
||||||
|
|
|
@ -425,3 +425,57 @@ func Test_postDeletesParams(t *testing.T) {
|
||||||
|
|
||||||
assert.Equal(t, 0, count)
|
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)
|
||||||
|
})
|
||||||
|
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue