Trigger post instead of update hook when scheduled post get's published

This commit is contained in:
Jan-Lukas Else 2022-12-30 19:25:31 +01:00
parent 2bb3f760a0
commit 4b9440443b
2 changed files with 15 additions and 1 deletions

View File

@ -180,7 +180,7 @@ func (a *goBlog) createOrReplacePost(p *post, o *postCreationOptions) error {
}
// Trigger hooks
if p.Status == statusPublished && (p.Visibility == visibilityPublic || p.Visibility == visibilityUnlisted) {
if o.new || (o.oldStatus != statusPublished && o.oldVisibility != visibilityPublic && o.oldVisibility != visibilityUnlisted) {
if o.new || o.oldStatus == statusScheduled || (o.oldStatus != statusPublished && o.oldVisibility != visibilityPublic && o.oldVisibility != visibilityUnlisted) {
defer a.postPostHooks(p)
} else {
defer a.postUpdateHooks(p)

View File

@ -10,6 +10,8 @@ import (
func Test_postsScheduler(t *testing.T) {
updateHook, postHook := 0, 0
app := &goBlog{
cfg: createDefaultTestConfig(t),
}
@ -21,6 +23,12 @@ func Test_postsScheduler(t *testing.T) {
Lang: "en",
},
}
app.pPostHooks = append(app.pPostHooks, func(p *post) {
postHook++
})
app.pUpdateHooks = append(app.pUpdateHooks, func(p *post) {
updateHook++
})
_ = app.initConfig(false)
_ = app.initCache()
@ -40,10 +48,16 @@ func Test_postsScheduler(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, 1, count)
assert.Equal(t, 0, postHook)
assert.Equal(t, 0, updateHook)
app.checkScheduledPosts()
count, err = app.db.countPosts(&postsRequestConfig{status: []postStatus{statusScheduled}})
require.NoError(t, err)
assert.Equal(t, 0, count)
assert.Equal(t, 1, postHook)
assert.Equal(t, 0, updateHook)
}