Activity Scopes
Introduction
Storyfeed::actor() and Storyfeed::context() supply a default actor or context to every activity published inside a callback. HTTP middleware can supply either role for a whole request.
Sharing Roles Within a Callback
Sharing Context
<?php
namespace App\Http\Controllers;
use App\Models\Order;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Storyfeed\Facades\Storyfeed;
class PlaceOrderController extends Controller
{
public function __invoke(Request $request, Order $order): RedirectResponse
{
$order->update(['status' => 'placed']);
Storyfeed::context($order->shop, function () use ($request, $order) {
Storyfeed::activity()
->by($request->user())
->action('place', $order)
->publish();
});
return to_route('orders.show', $order);
}
}<?php
namespace App\Http\Controllers;
use App\Models\Order;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Storyfeed\Facades\Storyfeed;
class PlaceOrderController extends Controller
{
public function __invoke(Request $request, Order $order): RedirectResponse
{
$order->update(['status' => 'placed']);
Storyfeed::context($order->shop, function () use ($request, $order) {
Storyfeed::record(
verb: 'place',
object: $order,
actor: $request->user(),
);
});
return to_route('orders.show', $order);
}
}With :actor placed :object in :context declared as the headline:
[
{
"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 in :context",
"headline": null,
"bindings": null,
"glyph": "shopping-bag",
"glyph_intent": null,
"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": null,
"context": {
"type": "venue",
"id": "101",
"label": "Scoops Ahoy",
"link": {
"href": "/venues/scoops",
"modal": false,
"attributes": []
},
"data": [],
"media": {
"preview": {
"src": "/media/worlds/stranger-things/parlour.jpg",
"mediaType": "image/jpeg",
"width": 960,
"height": 720,
"alt": "An ice cream counter"
}
},
"body": [
{
"$body": "Storyfeed/Body/Image",
"$v": 3,
"alt": "Scoops Ahoy",
"caption": "Scoops Ahoy",
"image": "preview"
}
],
"tombstone": null
},
"origin": null,
"result": null,
"instrument": null,
"location": null,
"generator": null,
"featured": "object",
"data": null,
"tombstoned": [],
"redundant": false,
"missing_headline_template": null,
"missing_headline": null
}
]Every activity published inside the callback inherits the context, including activities published by methods the callback calls.
The scope accepts an Eloquent model or a party name and returns the callback's result. Without a callback, Storyfeed::context($model) returns an activity builder with that context set.
Sharing an Actor
use Storyfeed\Facades\Storyfeed;
Storyfeed::actor($request->user(), function () use ($order) {
Storyfeed::activity()
->action('place', $order)
->context($order->shop)
->publish();
});use Storyfeed\Facades\Storyfeed;
Storyfeed::actor($request->user(), function () use ($order) {
Storyfeed::record(
verb: 'place',
object: $order,
context: $order->shop,
);
});[
{
"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 in :context",
"headline": null,
"bindings": null,
"glyph": "shopping-bag",
"glyph_intent": null,
"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": null,
"context": {
"type": "venue",
"id": "101",
"label": "Scoops Ahoy",
"link": {
"href": "/venues/scoops",
"modal": false,
"attributes": []
},
"data": [],
"media": {
"preview": {
"src": "/media/worlds/stranger-things/parlour.jpg",
"mediaType": "image/jpeg",
"width": 960,
"height": 720,
"alt": "An ice cream counter"
}
},
"body": [
{
"$body": "Storyfeed/Body/Image",
"$v": 3,
"alt": "Scoops Ahoy",
"caption": "Scoops Ahoy",
"image": "preview"
}
],
"tombstone": null
},
"origin": null,
"result": null,
"instrument": null,
"location": null,
"generator": null,
"featured": "object",
"data": null,
"tombstoned": [],
"redundant": false,
"missing_headline_template": null,
"missing_headline": null
}
]The actor method accepts an Eloquent model or a party name, checked against the declared party names when you declare them, and returns the callback's result. Without a callback, Storyfeed::actor($user) returns an activity builder with that actor set.
Nested Scopes
Nested callbacks use the innermost actor or context. The previous scope is restored when the callback ends, including when it throws an exception.
Sharing Roles Within an HTTP Request
Context From Route Parameters
use App\Http\Controllers\PlaceOrderController;
use Illuminate\Support\Facades\Route;
Route::post('/shops/{shop}/orders/{order}/place', PlaceOrderController::class)
->middleware(['auth', 'storyfeed.context:shop']);The storyfeed.context:shop middleware uses the bound shop route parameter. It must be an Eloquent model; missing or unbound values throw an exception. For implicit binding, type-hint the parameter in the controller:
<?php
namespace App\Http\Controllers;
use App\Models\Order;
use App\Models\Shop;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Storyfeed\Facades\Storyfeed;
class PlaceOrderController extends Controller
{
public function __invoke(Request $request, Shop $shop, Order $order): RedirectResponse
{
$order->update(['status' => 'placed']);
Storyfeed::activity()->by($request->user())->action('place', $order)->publish();
return to_route('orders.show', $order);
}
}<?php
namespace App\Http\Controllers;
use App\Models\Order;
use App\Models\Shop;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Storyfeed\Facades\Storyfeed;
class PlaceOrderController extends Controller
{
public function __invoke(Request $request, Shop $shop, Order $order): RedirectResponse
{
$order->update(['status' => 'placed']);
Storyfeed::record(
verb: 'place',
object: $order,
actor: $request->user(),
);
return to_route('orders.show', $order);
}
}[
{
"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 in :context",
"headline": null,
"bindings": null,
"glyph": "shopping-bag",
"glyph_intent": null,
"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": null,
"context": {
"type": "venue",
"id": "101",
"label": "Scoops Ahoy",
"link": {
"href": "/venues/scoops",
"modal": false,
"attributes": []
},
"data": [],
"media": {
"preview": {
"src": "/media/worlds/stranger-things/parlour.jpg",
"mediaType": "image/jpeg",
"width": 960,
"height": 720,
"alt": "An ice cream counter"
}
},
"body": [
{
"$body": "Storyfeed/Body/Image",
"$v": 3,
"alt": "Scoops Ahoy",
"caption": "Scoops Ahoy",
"image": "preview"
}
],
"tombstone": null
},
"origin": null,
"result": null,
"instrument": null,
"location": null,
"generator": null,
"featured": "object",
"data": null,
"tombstoned": [],
"redundant": false,
"missing_headline_template": null,
"missing_headline": null
}
]The middleware supplies the context to every activity published in this request.
Declared Party Actors
use App\Http\Controllers\PlaceOrderController;
use Illuminate\Support\Facades\Route;
Route::post('/shops/{shop}/orders/{order}/place', PlaceOrderController::class)
->middleware(['auth', 'storyfeed.context:shop', 'storyfeed.actor:System']);The storyfeed.actor:System middleware wraps the request in Storyfeed::actor('System', ...). Declare the party in your service provider. An explicit ->by($request->user()) takes precedence; activities without an explicit actor use System.
Request-Based Actors
A Story class verb method can choose the actor from the request:
use Illuminate\Http\Request;
use Storyfeed\Stories\Verb;
public function confirmPayment(Verb $verb, Request $request): Verb
{
return $verb
->headline(':actor confirmed payment for :object')
->icon('credit-card')
->actor($request->hasHeader('Paddle-Signature') ? 'Paddle' : 'Stripe');
}The same webhook can choose between two declared parties. Its controller publishes confirm_payment without naming an actor:
use Storyfeed\Facades\Storyfeed;
Storyfeed::activity('confirm_payment', $order)->publish();use Storyfeed\Facades\Storyfeed;
Storyfeed::record(verb: 'confirm_payment', object: $order);For a request without the signature header:
The verb's actor applies when no actor is assigned explicitly or through a Storyfeed::actor() scope. Only the actor setting may depend on the request; headlines, icons, intents, grouping, and other settings must remain consistent. A request-dependent headline throws an exception when grammar.strict is enabled, as it is by default in local and testing environments.
Role Precedence
Storyfeed resolves each role from the first applicable source:
| Priority | Actor | Context |
|---|---|---|
| Call site | ->by($user) or explicit anonymity | ->context($model) |
| Scope | Storyfeed::actor() or storyfeed.actor:{Party}, including a scope carried into a queued job | Storyfeed::context() or storyfeed.context:{param}, including a scope carried into a queued job |
| Story middleware | supplies an actor when hasActor() is false | supplies context when has('context') is false |
| Verb | the verb's actor | none |
| Resolver or user | a custom actor_resolver; without one, the authenticated user, or in a queued job the user authenticated at dispatch | none |
| Fallback | parties.fallback | none |
An actor or context set on the activity takes precedence over the corresponding scope. An explicit by(null) records no actor even inside an actor scope. A nested scope applies only for its callback.
A custom resolver replaces the authenticated user as a source. If it returns null, the fallback party applies. Explicit anonymity records no actor. Without a resolved actor, the activity is anonymous and cannot join a batch. If no context is supplied, that role remains empty.
Carrying Roles Into Queued Jobs
The Authenticated User
A job dispatched during a request publishes as the request's authenticated user, even though the worker has no logged-in user. Jobs dispatched from that job inherit the same user.
Actor Scopes
A job dispatched inside Storyfeed::actor() runs as that actor on the worker:
use App\Jobs\SyncMenu;
use Storyfeed\Facades\Storyfeed;
Storyfeed::actor('System', fn () => SyncMenu::dispatch());Activities published by the job use the System party unless they specify an actor. Jobs it dispatches inherit the scope, which ends when the job completes or throws an exception. A job dispatched with ->afterResponse() runs after the scope closes, so it does not carry the actor.
Context Scopes
Jobs dispatched inside Storyfeed::context() run inside that context on the worker, and jobs they dispatch inherit it. The scope ends with the job, even when the job throws.
Carrying Request-Based Actors
Jobs dispatched during a request carry the actor selected by the request-based verb actor. The worker retains that selection without needing the original HTTP request.