feat: announce material revisions on materials:{id}

- every revision bump (API writes and /bmadmin saves) enqueues
  updated.jz.bm.material with {material_id, revision} in a savepoint
- the materials namespace authorizer allows a subscribe only to a
  visible material the user may manage
- api.ManagesMaterials and api.CanWatchMaterial
This commit is contained in:
Jakub Zych
2026-11-09 01:12:49 +01:00
parent 40e4a2438b
commit 2a47186a7a
13 changed files with 571 additions and 10 deletions

View File

@@ -50,6 +50,29 @@ func installedMaterialIndex() MaterialIndex {
return h.ix
}
// MaterialRevisionNotifier publishes the live heads-up after an admin save
// bumped a material's revision (classes.MaterialRevisionNotifier). It
// enqueues on tx and never fails the save.
type MaterialRevisionNotifier interface {
Revised(ctx context.Context, tx *gorm.DB, id, revision uint)
}
type materialRevisionNotifierHolder struct{ n MaterialRevisionNotifier }
var materialRevisionNotifier atomic.Value
// UseMaterialRevisionNotifier installs the notifier the materials admin
// calls after a save bumped the revision; nil turns it off. Boot installs
// it, as UseMaterialIndex.
func UseMaterialRevisionNotifier(n MaterialRevisionNotifier) {
materialRevisionNotifier.Store(materialRevisionNotifierHolder{n: n})
}
func installedMaterialRevisionNotifier() MaterialRevisionNotifier {
h, _ := materialRevisionNotifier.Load().(materialRevisionNotifierHolder)
return h.n
}
// FormAfterCreate queues the file text job in the save transaction (the
// form's deferred files commit in the same one) and syncs the new material
// once it commits.
@@ -88,7 +111,8 @@ func (c materialsAdmin) FormBeforeUpdate(ctx context.Context, model any) error {
}
// FormAfterUpdate bumps the revision when the save changed the material's
// content, then queues the file text job in the save transaction and syncs
// content, and then enqueues the live heads-up on the save transaction
// (D-20), then queues the file text job in the save transaction and syncs
// the material once it commits. The compare runs here, not in
// FormBeforeUpdate, because the course, the zjazd and the tags are final
// only after the row write and the pivot sync. A revise error fails the save
@@ -102,9 +126,13 @@ func (materialsAdmin) FormAfterUpdate(ctx context.Context, model any) error {
}
if pending, found := pendingRevisions.LoadAndDelete(m.ID); found {
if tx, ok := cabana.TxFromContext(ctx); ok {
if _, _, err := models.ReviseMaterial(tx, m.ID, pending.(string)); err != nil {
rev, bumped, err := models.ReviseMaterial(tx, m.ID, pending.(string))
if err != nil {
return err
}
if n := installedMaterialRevisionNotifier(); bumped && n != nil {
n.Revised(ctx, tx, m.ID, rev)
}
}
}
return materialAfterCommit(ctx, model, false)