Allow to update post-status using Micropub update

This commit is contained in:
Jan-Lukas Else 2024-01-23 08:25:21 +01:00
parent 10616e24a5
commit 24cd6dbdac
2 changed files with 37 additions and 3 deletions

View File

@ -465,7 +465,6 @@ func (s *micropubImplementation) updatePostPropertiesFromMf(p *post, properties
// Ignore the following properties
delete(properties, "url")
delete(properties, "post-status")
delete(properties, "photo")
delete(properties, "photo-alt")
@ -492,6 +491,12 @@ func (s *micropubImplementation) updatePostPropertiesFromMf(p *post, properties
delete(properties, "mp-channel")
p.Visibility = postVisibility(defaultIfEmpty(getFirstStringFromArray(properties["visibility"]), string(p.Visibility)))
delete(properties, "visibility")
if newStatusString := getFirstStringFromArray(properties["post-status"]); newStatusString != "" {
if newStatus := postStatus(newStatusString); newStatus == statusPublished || newStatus == statusDraft {
p.Status = newStatus
}
}
delete(properties, "post-status")
for key, value := range properties {
p.Parameters[s.mapToParameterName(key)] = cast.ToStringSlice(value)

View File

@ -279,7 +279,7 @@ func Test_micropubUpdate(t *testing.T) {
"category": {"test"},
"random": {"def"},
"content": {"New test"},
"post-status": {"published-deleted"},
"post-status": {"draft"},
"visibility": {"unlisted"},
},
},
@ -292,11 +292,40 @@ func Test_micropubUpdate(t *testing.T) {
assert.Equal(t, []string{"test"}, p.Parameters["tags"])
assert.Equal(t, []string{"def"}, p.Parameters["random"])
assert.Equal(t, "New test", p.Content)
assert.Equal(t, statusPublished, p.Status)
assert.Equal(t, statusDraft, p.Status)
assert.Nil(t, p.Parameters["post-status"])
assert.Equal(t, visibilityUnlisted, p.Visibility)
})
t.Run("Replace wrong status", func(t *testing.T) {
postPath := "/replace2"
err := app.createPost(&post{
Path: postPath,
Content: "",
Status: statusPublished,
Visibility: visibilityPublic,
Parameters: map[string][]string{},
})
require.NoError(t, err)
_, err = app.getMicropubImplementation().Update(&micropub.Request{
URL: app.cfg.Server.PublicAddress + postPath,
Updates: micropub.RequestUpdate{
Replace: map[string][]any{
"post-status": {"published-deleted"},
},
},
})
require.NoError(t, err)
p, err := app.getPost(postPath)
require.NoError(t, err)
assert.Equal(t, statusPublished, p.Status)
assert.Nil(t, p.Parameters["post-status"])
})
t.Run("Add", func(t *testing.T) {
postPath := "/add1"