feat(16.2-07): purge the host site cache on every admin post and category write

- posts: FormAfterCreate/Update purge after the content_html translations,
  new FormAfterDelete; categories: FormAfterCreate and FormAfterDelete
- NewSiteCachePurger refuses half configuration, non-http(s) URLs, user
  info, fragments, http to a non-loopback host and tokens under 32 chars;
  Boot logs the endpoint host and path once
- TestPostsAdminPurgesSiteCache smoke test
- README: Configuration section (keys, contract, timing, scope), API rows
This commit is contained in:
2026-10-10 18:27:12 +02:00
parent f3b223f131
commit 2a3afb5107
8 changed files with 192 additions and 25 deletions

View File

@@ -6,6 +6,7 @@ import (
"fmt"
"strconv"
"git.golem15.com/golem15/sm-journal-plugin/classes"
"git.golem15.com/golem15/sm-journal-plugin/console"
"git.golem15.com/golem15/sm-journal-plugin/models"
"git.golem15.com/golem15/summercms/modules/backpack"
@@ -226,16 +227,41 @@ func preparePostWrite(ctx context.Context, post *models.Post) error {
}
// FormAfterCreate writes content_html for every non-default locale whose
// content the save wrote (D-02).
// content the save wrote (D-02), then registers the host site cache purge
// for after the commit.
func (c postsAdminController) FormAfterCreate(ctx context.Context, model any) error {
return c.writeContentHTMLTranslations(ctx, model)
return c.afterPostWrite(ctx, model)
}
// FormAfterUpdate writes content_html for every non-default locale whose
// content the save wrote (D-02). Untouched locales are not listed and keep
// their content_html.
// their content_html. It then registers the host site cache purge for after
// the commit; an untouched save purges too, which is harmless.
func (c postsAdminController) FormAfterUpdate(ctx context.Context, model any) error {
return c.writeContentHTMLTranslations(ctx, model)
return c.afterPostWrite(ctx, model)
}
// FormAfterDelete registers the host site cache purge for after the delete
// commits.
func (c postsAdminController) FormAfterDelete(ctx context.Context, model any) error {
post, ok := model.(*models.Post)
if !ok || post == nil {
return errors.New("posts admin: form model is not a post")
}
purger, tx := siteCachePurger(ctx, c.app)
classes.PurgeSiteCacheAfterCommit(ctx, tx, purger, "post", post.Slug)
return nil
}
func (c postsAdminController) afterPostWrite(ctx context.Context, model any) error {
if err := c.writeContentHTMLTranslations(ctx, model); err != nil {
return err
}
if post, ok := model.(*models.Post); ok && post != nil {
purger, tx := siteCachePurger(ctx, c.app)
classes.PurgeSiteCacheAfterCommit(ctx, tx, purger, "post", post.Slug)
}
return nil
}
// writeContentHTMLTranslations stores each written non-default content