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 // Trigger hooks
if p.Status == statusPublished && (p.Visibility == visibilityPublic || p.Visibility == visibilityUnlisted) { 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) defer a.postPostHooks(p)
} else { } else {
defer a.postUpdateHooks(p) defer a.postUpdateHooks(p)

View File

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