Story Middleware
Introduction
Use story middleware to add shared data, supply roles, or decide whether to publish an activity. The built-in batch middleware collects one actor's activities into a batch; see Batching Activities.
Defining Story Middleware
<?php
namespace App\StoryMiddleware;
use Closure;
use Storyfeed\PendingActivity;
class MarkReviewed
{
public function handle(PendingActivity $activity, Closure $next): mixed
{
$activity->data([
...($activity->activity->data ?? []),
'reviewed' => true,
]);
return $next($activity);
}
}Middleware receives a PendingActivity and passes it to $next. Code after $next($activity) can inspect the returned activity. Check its exists property before performing work that requires a stored activity.
Return null without calling $next to skip publishing. The builder's publish method then returns an unsaved activity (exists === false). To continue publishing, return the result of $next($activity). Middleware cannot change the verb or object type.
Closure Middleware
You may also define middleware as a closure:
use App\Models\Order;
use Closure;
use Storyfeed\Facades\Story;
use Storyfeed\PendingActivity;
Story::for(Order::class)->verb('place')
->headline(':actor placed :object with :target')
->icon('shopping-bag')
->middleware(
static function (PendingActivity $activity, Closure $next) {
return $next($activity->data([
...($activity->activity->data ?? []),
'reviewed' => true,
]));
},
);[
{
"kind": "activity",
"id": "j84",
"verb": "place",
"published_at": "1985-07-02T12:00:00.000000Z",
"starts_at": null,
"ends_at": null,
"headline_template": ":actor placed :object with :target",
"headline": null,
"bindings": null,
"glyph": "shopping-bag",
"glyph_intent": "pending",
"actor": {
"type": "user",
"id": "113",
"label": "Erica Sinclair",
"link": {
"href": "/users/113",
"modal": false,
"attributes": []
},
"data": [],
"media": {
"icon": {
"src": "/media/worlds/stranger-things/avatars/scout.jpg",
"mediaType": "image/jpeg",
"width": 256,
"height": 256,
"alt": null
},
"image": null,
"preview": null,
"initials": null,
"color": null,
"files": [],
"slots": []
},
"media_ref": "maa7c0cfb07af83e2",
"entity_ref": "ebe52ab4d5e63692e",
"body": null,
"tombstone": null
},
"object": {
"type": "order",
"id": "1035",
"label": "Order #1035",
"link": {
"href": "/orders/1035",
"modal": false,
"attributes": []
},
"data": [],
"media": {
"icon": null,
"image": null,
"preview": null,
"initials": "O1",
"color": "#4d7c0f",
"files": [],
"slots": []
},
"media_ref": "ma9527581042280f7",
"entity_ref": "e722f0b114fd1ec13",
"body": null,
"tombstone": null
},
"target": {
"type": "venue",
"id": "101",
"label": "Scoops Ahoy",
"link": {
"href": "/venues/scoops",
"modal": false,
"attributes": []
},
"data": [],
"media": {
"icon": null,
"image": null,
"preview": {
"src": "/media/worlds/stranger-things/parlour.jpg",
"mediaType": "image/jpeg",
"width": 960,
"height": 720,
"alt": "An ice cream counter"
},
"initials": "SA",
"color": "#9f1239",
"files": [],
"slots": []
},
"media_ref": "m1bcad7c79ddfc05d",
"entity_ref": "e352bb6f37b7d8bfe",
"body": [
{
"$body": "Storyfeed/Body/Image",
"$v": 3,
"alt": "Scoops Ahoy",
"caption": "Scoops Ahoy",
"image": "preview"
}
],
"tombstone": null
},
"context": null,
"origin": null,
"result": null,
"instrument": null,
"location": null,
"generator": null,
"featured": "object",
"data": {
"reviewed": true
},
"tombstoned": [],
"redundant": false,
"missing_headline_template": null,
"missing_headline": null
}
]Storyfeed::fake() runs the same middleware pipeline.
Registering Middleware
Register aliases and named groups in a service provider, because cached stories do not load routes/feed.php:
use App\StoryMiddleware\MarkReviewed;
use Storyfeed\Facades\Story;
Story::aliasMiddleware('reviewed', MarkReviewed::class);
Story::middlewareGroup('review', ['reviewed']);aliasMiddleware gives a class a short name. middlewareGroup registers a list of middleware under one name: the review group contains the reviewed alias, and either may be assigned to a story.
Assigning Middleware to Stories
Attach the class to the verb:
use App\Models\Order;
use App\StoryMiddleware\MarkReviewed;
use Storyfeed\Facades\Story;
Story::for(Order::class)->verb('place')
->headline(':actor placed :object with :target')
->icon('shopping-bag')
->middleware(MarkReviewed::class);[
{
"kind": "activity",
"id": "j84",
"verb": "place",
"published_at": "1985-07-02T12:00:00.000000Z",
"starts_at": null,
"ends_at": null,
"headline_template": ":actor placed :object with :target",
"headline": null,
"bindings": null,
"glyph": "shopping-bag",
"glyph_intent": "pending",
"actor": {
"type": "user",
"id": "113",
"label": "Erica Sinclair",
"link": {
"href": "/users/113",
"modal": false,
"attributes": []
},
"data": [],
"media": {
"icon": {
"src": "/media/worlds/stranger-things/avatars/scout.jpg",
"mediaType": "image/jpeg",
"width": 256,
"height": 256,
"alt": null
},
"image": null,
"preview": null,
"initials": null,
"color": null,
"files": [],
"slots": []
},
"media_ref": "maa7c0cfb07af83e2",
"entity_ref": "ebe52ab4d5e63692e",
"body": null,
"tombstone": null
},
"object": {
"type": "order",
"id": "1035",
"label": "Order #1035",
"link": {
"href": "/orders/1035",
"modal": false,
"attributes": []
},
"data": [],
"media": {
"icon": null,
"image": null,
"preview": null,
"initials": "O1",
"color": "#4d7c0f",
"files": [],
"slots": []
},
"media_ref": "ma9527581042280f7",
"entity_ref": "e722f0b114fd1ec13",
"body": null,
"tombstone": null
},
"target": {
"type": "venue",
"id": "101",
"label": "Scoops Ahoy",
"link": {
"href": "/venues/scoops",
"modal": false,
"attributes": []
},
"data": [],
"media": {
"icon": null,
"image": null,
"preview": {
"src": "/media/worlds/stranger-things/parlour.jpg",
"mediaType": "image/jpeg",
"width": 960,
"height": 720,
"alt": "An ice cream counter"
},
"initials": "SA",
"color": "#9f1239",
"files": [],
"slots": []
},
"media_ref": "m1bcad7c79ddfc05d",
"entity_ref": "e352bb6f37b7d8bfe",
"body": [
{
"$body": "Storyfeed/Body/Image",
"$v": 3,
"alt": "Scoops Ahoy",
"caption": "Scoops Ahoy",
"image": "preview"
}
],
"tombstone": null
},
"context": null,
"origin": null,
"result": null,
"instrument": null,
"location": null,
"generator": null,
"featured": "object",
"data": {
"reviewed": true
},
"tombstoned": [],
"redundant": false,
"missing_headline_template": null,
"missing_headline": null
}
]Use the group in the feed file:
use App\Models\Order;
use Storyfeed\Facades\Story;
Story::middleware('review')->group(function () {
Story::for(Order::class)->verb('place')
->headline(':actor placed :object with :target')
->icon('shopping-bag');
Story::for(Order::class)->verb('complete')
->headline(':actor completed :object')
->withoutMiddleware('reviewed');
});The place activity receives the review data; complete skips that middleware:
[
{
"kind": "activity",
"id": "j84",
"verb": "place",
"published_at": "1985-07-02T12:00:00.000000Z",
"starts_at": null,
"ends_at": null,
"headline_template": ":actor placed :object with :target",
"headline": null,
"bindings": null,
"glyph": "shopping-bag",
"glyph_intent": "pending",
"actor": {
"type": "user",
"id": "113",
"label": "Erica Sinclair",
"link": {
"href": "/users/113",
"modal": false,
"attributes": []
},
"data": [],
"media": {
"icon": {
"src": "/media/worlds/stranger-things/avatars/scout.jpg",
"mediaType": "image/jpeg",
"width": 256,
"height": 256,
"alt": null
},
"image": null,
"preview": null,
"initials": null,
"color": null,
"files": [],
"slots": []
},
"media_ref": "maa7c0cfb07af83e2",
"entity_ref": "ebe52ab4d5e63692e",
"body": null,
"tombstone": null
},
"object": {
"type": "order",
"id": "1035",
"label": "Order #1035",
"link": {
"href": "/orders/1035",
"modal": false,
"attributes": []
},
"data": [],
"media": {
"icon": null,
"image": null,
"preview": null,
"initials": "O1",
"color": "#4d7c0f",
"files": [],
"slots": []
},
"media_ref": "ma9527581042280f7",
"entity_ref": "e722f0b114fd1ec13",
"body": null,
"tombstone": null
},
"target": {
"type": "venue",
"id": "101",
"label": "Scoops Ahoy",
"link": {
"href": "/venues/scoops",
"modal": false,
"attributes": []
},
"data": [],
"media": {
"icon": null,
"image": null,
"preview": {
"src": "/media/worlds/stranger-things/parlour.jpg",
"mediaType": "image/jpeg",
"width": 960,
"height": 720,
"alt": "An ice cream counter"
},
"initials": "SA",
"color": "#9f1239",
"files": [],
"slots": []
},
"media_ref": "m1bcad7c79ddfc05d",
"entity_ref": "e352bb6f37b7d8bfe",
"body": [
{
"$body": "Storyfeed/Body/Image",
"$v": 3,
"alt": "Scoops Ahoy",
"caption": "Scoops Ahoy",
"image": "preview"
}
],
"tombstone": null
},
"context": null,
"origin": null,
"result": null,
"instrument": null,
"location": null,
"generator": null,
"featured": "object",
"data": {
"reviewed": true
},
"tombstoned": [],
"redundant": false,
"missing_headline_template": null,
"missing_headline": null
}
]Execution Order
The built-in default group runs first, followed by enclosing groups and the verb's middleware. Identical resolved middleware strings run once. The default group contains batch. To run middleware for every verb, push it onto the default group:
use Storyfeed\Facades\Story;
Story::pushMiddlewareToGroup('default', 'reviewed');prependMiddlewareToGroup adds middleware to the start of a group instead. Neither adds middleware a group already contains. To replace the whole group, redefine it with Story::middlewareGroup('default', [...]).
A queued activity runs its story middleware on the worker.
Excluding Middleware
Exclusions match resolved strings exactly: withoutMiddleware('batch') leaves batch:5 minutes in place. Use unbatched() to remove batching altogether.
Parameters
A middleware string may name a class, alias, or group. Append arguments after a colon, as in batch:5 minutes. A custom class receives them after $next in its handle method. A Story may declare middleware in middleware(): array, but that method cannot use constructor data.
Supplying Default Roles
<?php
namespace App\StoryMiddleware;
use Closure;
use Storyfeed\PendingActivity;
class UseServiceActor
{
public function handle(PendingActivity $activity, Closure $next): mixed
{
if (! $activity->hasActor()) {
$activity->by('System'); // A declared party.
}
return $next($activity);
}
}Call hasActor() before supplying an actor; it also returns true for explicit anonymity. Call has('context') before supplying context. These checks preserve actor and context precedence. Declare party names in the party list, because the by method does not check that list.
A role supplied by story middleware gives way to the call site and any scope; see Role Precedence for the full order.
Caching and Inspecting Middleware
The storyfeed:cache command caches middleware declarations, including closures. Register aliases and named groups in a service provider so they remain available when the feed file is cached. Use storyfeed:list -v to inspect each verb's resolved middleware and arguments.