Jakub Zych ec1a82ce0a feat: PostSaving carries the rendered post HTML
- CreateThread, Reply and EditPost render the Markdown first and fire
  PostSaving with HTML, the content_html the write stores
- additive: no route, payload, table, migration or config change; the
  parity test expects the new field
- TestPostSavingHTML checks HTML against the stored content_html on a
  thread, a reply and an edit
2026-11-02 18:48:51 +01:00

Forum

A compiled forum plugin for SummerCMS: context-scoped categories, threads, Markdown posts and a generic JSON API.

import _ "git.golem15.com/golem15/sm-forum-plugin"

Name: Forum on the shore; threads stay after the tide.

Module git.golem15.com/golem15/sm-forum-plugin, package forum, plugin ID golem15.forum, requires golem15.user. Display name Forum / Forum.

Overview

The plugin ports the WinterCMS Golem15.Forum plugin. classes.ForumService is the write path: create category, create thread and reply persist inside lagoon.Transaction. New posts call breakwater.Render on Markdown and store content_html; GET returns that stored HTML and never re-renders (D-18). Actor id always comes from the JWT, never from the request body. The only festival event forum fires is the pre-save events.PostSaving; it fires no post-save events.

Tables (PHP v1.1.0 names, one idempotent baseline migration 202610180001_create_golem15_forum_baseline, every statement IF NOT EXISTS):

  • golem15_forum_categories: morph owner context_type (NOT NULL, no FK) + context_id, unique (context_type, context_id, slug) (models.Category);
  • golem15_forum_threads: user_id nullable SET NULL (models.Thread);
  • golem15_forum_posts: Markdown content and stored content_html (models.Post);
  • golem15_forum_thread_reads: unique (user_id, thread_id) (models.ThreadRead).

Access is delegated to contracts.AccessResolver. New installs classes.DenyAll; SetAccessResolver(nil) becomes DenyAll. Boot Warns while the default remains. A blog or acme host must install its own resolver.

Features

  • Generic forum API. forum.APIPrefix (/_forum/api/v1) behind jwt.auth and forum-api 120/min.
  • ForumService. CreateCategory, RenameCategory, ReorderCategories, DeleteCategory, SeedDefaultCategory, CreateThread, Reply, EditPost, DeletePost, DeleteThread, MarkThreadRead, UnreadThreadCount, DeleteContext. Missing and unauthorized collapse to 404.
  • DeleteContext. ForumService.DeleteContext(ctx, type, id) soft-deletes categories (and cascaded threads/posts) for that morph pair. The host wires group-deleted to this method; forum never imports the friends plugin.
  • Morph rewrite. summer forum:rewrite-context-types walks RegisterContext LegacyPHPClass values and UPDATE golem15_forum_categories.context_type to the alias (idempotent).
  • Deny-all ForumAccessResolver. classes.DenyAll CanRead / CanPost / CanManage / CanManageContext are false. Plugin.SetAccessResolver installs the host. Boot Warns while the default remains; the host must SetAccessResolver.
  • Render on write. New and edited posts call breakwater.Render; preview uses the same function. The plugin does not vendor goldmark or bluemonday and does not call seaglass or cabana.RenderMarkdown.
  • Pre-save seam (additive). CreateThread, Reply and EditPost fire events.PostSaving (Op thread, reply or update) after their access and required-field checks and after breakwater.Render, before anything is persisted. The event carries the Markdown Body and the rendered HTML, the same content_html the write stores, so a listener can check what members will read (an entity or an escape in the Markdown can render as a word or link the source does not spell out). A listener error aborts the write and nothing is persisted. A listener that returns contracts.Rejection{Field, Message} gets the 422 validation envelope under errors.title or errors.body (body when Field is empty); any other listener error stays a 500. Without a listener nothing changes.
  • User only. Requires() is golem15.user. The friends plugin is not a dependency.
  • GDPR. Boot Listens user.Deleting (DeletingEvent) and sets threads.user_id and posts.user_id to null; bodies stay.

Usage

Look the service up at request time:

svc, _ := app.Lookup[*classes.ForumService]()
cat, err := svc.CreateCategory(ctx, actorID, "teams", teamID, classes.CreateCategoryInput{Title: "General"})

Install an access resolver from an application plugin after looking the plugin up (forum publishes it at Boot):

func (p *Plugin) Boot(app *backpack.App) error {
	f, _ := app.Lookup[*forum.Plugin]()
	f.SetAccessResolver(p.resolver) // contracts.AccessResolver
	return nil
}

The generic JSON API is forum.APIPrefix (/_forum/api/v1). Routes sit behind jwt.auth and the named bucket forum-api (120/min). The app mounts that guard; this plugin only names it. Published paths:

  • GET /contexts/{type}/{id}/categories
  • POST /contexts/{type}/{id}/categories
  • PUT /contexts/{type}/{id}/categories/{categoryId}
  • DELETE /contexts/{type}/{id}/categories/{categoryId}
  • POST /contexts/{type}/{id}/categories/reorder
  • GET /contexts/{type}/{id}/categories/{categoryId}/threads
  • POST /contexts/{type}/{id}/categories/{categoryId}/threads
  • GET /contexts/{type}/{id}/threads
  • GET /contexts/{type}/{id}/threads/{threadId}/posts
  • POST /contexts/{type}/{id}/threads/{threadId}/posts
  • PUT /contexts/{type}/{id}/posts/{postId}
  • DELETE /contexts/{type}/{id}/posts/{postId}
  • DELETE /contexts/{type}/{id}/threads/{threadId}
  • POST /contexts/{type}/{id}/threads/{threadId}/read
  • GET /contexts/{type}/{id}/unread
  • POST /contexts/{type}/{id}/preview

A host plugin can refuse a thread, reply or edit before it is stored (here a neutral acme plugin with its own allowed check on the source and allowedHTML check on the rendered post):

app.Events.Listen[*events.PostSaving]("acme.filter", func(ctx context.Context, ev *events.PostSaving) error {
	if !allowed(ev.Title) {
		return contracts.Rejection{Field: "title", Message: "This title is not allowed."}
	}
	if !allowed(ev.Body) || !allowedHTML(ev.HTML) {
		return contracts.Rejection{Field: "body", Message: "This post is not allowed."}
	}
	return nil
})

API reference

Identifier Description
forum.PluginID golem15.forum
forum.APIPrefix /_forum/api/v1
forum.APIBucket Throttle bucket forum-api (120/min)
contracts.AccessResolver Host access seam; default deny-all
classes.DenyAll Default resolver; every method false
classes.ForumService Forum write path
classes.New Builds the service; refuses a nil DB; DenyAll when Resolver is nil
classes.ForumService.WithDB Shallow copy bound to a festival event transaction
classes.ForumService.SetAccessResolver Host setter; nil becomes DenyAll
classes.ForumService.CreateCategory Persist a category in a morph pair
classes.ForumService.RenameCategory Rename / re-describe a category
classes.ForumService.ReorderCategories Persist sort_order; ignore foreign ids
classes.ForumService.DeleteCategory Soft-delete a category subtree; force purges
classes.ForumService.SeedDefaultCategory Trusted seed; not gated
classes.ForumService.CreateThread Persist a thread and first post; render on write
classes.ForumService.Reply Persist a reply; render on write
classes.ForumService.EditPost Re-render Markdown on write
classes.ForumService.DeletePost Soft-delete a post; stamp self or moderator
classes.ForumService.DeleteThread Soft-delete a thread and its posts
classes.ForumService.MarkThreadRead Idempotent read mark
classes.ForumService.UnreadThreadCount Unread threads of a morph pair
classes.ForumService.DeleteContext Soft-delete categories of a morph pair
classes.ForumService.RegisterContext Record alias + LegacyPHPClass for rewrite
classes.ForumService.RewriteContextTypes UPDATE context_type class strings to aliases
classes.ForumService.NullAuthorship Null thread and post user_id; keep bodies
classes.ForumService.Preview Render Markdown without storing
forum.Plugin.SetAccessResolver Host setter on the published plugin
events.PostSaving Pre-save event: ContextType, ContextID, ActorID, ThreadID, PostID, Title (thread only), Body, HTML (the content_html that would be stored), Op; fired after render and before persistence
events.PostSavingEventName golem15.forum.post.saving (Go-only, no PHP counterpart)
events.OpThread / events.OpReply / events.OpUpdate PostSaving.Op: new thread, reply, edit
contracts.Rejection {Field, Message} error a PostSaving listener returns to refuse content; the API answers 422 errors.{Field} (match with errors.As)
models.Category Row of golem15_forum_categories
models.Thread Row of golem15_forum_threads
models.Post Row of golem15_forum_posts
models.ThreadRead Row of golem15_forum_thread_reads

CLI commands

Command Meaning Example
forum:rewrite-context-types Idempotent UPDATE of golem15_forum_categories.context_type from each RegisterContext LegacyPHPClass to its alias. Contexts must be registered before running it. Does not rewrite system_files.attachment_type. No flags; ends with Rewrote N category context_type row(s). bin/<app> forum:rewrite-context-types

bin/<app> help forum:rewrite-context-types shows the long help and examples. The plugin logs through the component forum (log.components.forum in config/log.yaml).

Dependencies

  • golem15.user (required), git.golem15.com/golem15/sm-user-plugin v0.1.2 or later (classes.DeletingEvent)
  • SummerCMS v0.4.0 modules: backpack, party, pact, lagoon, surf, bouncer, wire, breakwater, bonfire, diary

Testing

go vet ./...
go test -short ./...
go test -run '^TestHTTPTracer$' -v .
go test -run '^TestPostSavingTracer$' -v .
go test -run '^TestPostSaving(NoListener|Rejects|HTML)$' -v .

TestCommandsDocumented (in -short) logs, without failing, each command whose help lacks a description, long help, examples or an option description (bonfire.CheckDocs).

The proof host is golem15.user + golem15.forum + a test-only teams fixture. TestFixtureAbsentFromProduction keeps the fixture id and retired tokens out of production sources. TestHTTPTracer and the TestPostSaving* tests need Docker; TestPostSavingHTML checks that the event's HTML equals the stored content_html on a thread, a reply and an edit. TestNoUserfriendsImport greps production sources for the friends module path.

Changelog

v0.1.2

  • v0.1.2: PostSaving carries HTML, the rendered and sanitised post that is stored as content_html; the event now fires after the render. Additive: no route, payload, table, migration or config key changes, and without a listener behaviour is unchanged.

v0.1.1

  • v0.1.1: adds the pre-save PostSaving seam (package events) and contracts.Rejection; writeForumError maps a Rejection to the 422 validation envelope. Additive: no route, payload, table, migration or config key changes, and without a listener behaviour is unchanged.

v0.1.0

  • First tag; plugin.yaml manifest for the SummerCMS marketplace.
Description
No description provided
Readme 217 KiB
Languages
Go 100%