Pre-1.0. Not intended for public use yet. The API is subject to undocumented change, and these pages may already be wrong about it.

Skip to content

Parties & Anonymous Actors ​

Introduction ​

An activity's actor can be a user or a party, such as a payment provider. An anonymous activity has no recorded actor.

MeansIn the Payload
anonymousno recorded actoractor: null; the headline uses the anonymous headline
partya named participant with no model in your appan entity with type: "storyfeed.entity", a label, and an optional external link

Recording a Party ​

A scheduled command cancels unpaid orders. No user is signed in, so name the actor:

app/Console/Commands/CancelUnpaidOrders.php
php
<?php

namespace App\Console\Commands;

use App\Models\Order;
use Illuminate\Console\Command;
use Storyfeed\Facades\Storyfeed;

class CancelUnpaidOrders extends Command
{
    protected $signature = 'orders:cancel-unpaid';

    protected $description = 'Cancel the orders left unpaid at closing time';

    public function handle(): void
    {
        Order::whereNull('paid_at')->whereNull('cancelled_at')->each(function (Order $order) {
            $order->update(['cancelled_at' => now()]);

            Storyfeed::activity()
                ->by('Register') 
                ->action('cancel', $order)
                ->publish();
        });
    }
}
php
<?php

namespace App\Console\Commands;

use App\Models\Order;
use Illuminate\Console\Command;
use Storyfeed\Facades\Storyfeed;

class CancelUnpaidOrders extends Command
{
    protected $signature = 'orders:cancel-unpaid';

    protected $description = 'Cancel the orders left unpaid at closing time';

    public function handle(): void
    {
        Order::whereNull('paid_at')->whereNull('cancelled_at')->each(function (Order $order) {
            $order->update(['cancelled_at' => now()]);

            Storyfeed::record(
                verb: 'cancel',
                object: $order,
                actor: 'Register', 
            );
        });
    }
}
SR
Register cancelled Order #2071

Storyfeed creates the party when its name is first used and reuses it for later activities.

Without by('Register') or another actor default, this command records an anonymous activity.

Linking a Party ​

Give a party a URL with Party::make():

app/Console/Commands/CancelUnpaidOrders.phphandle()
php
use Storyfeed\Facades\Storyfeed;
use Storyfeed\Models\Party;

Party::make('Register', url: 'https://example.com/register');

Storyfeed::activity()
    ->by('Register')
    ->action('cancel', $order)
    ->publish();
php
use Storyfeed\Facades\Storyfeed;
use Storyfeed\Models\Party;

Party::make('Register', url: 'https://example.com/register');

Storyfeed::record(
    verb: 'cancel',
    object: $order,
    actor: 'Register',
);
SR

The URL can appear in any party role, and Activity Streams output includes it as url, including for the actor. Without a URL, the party remains unlinked. Pass url: null to remove the link.

Party::make() creates the party or updates the one with the same key:

ArgumentSetsDefault
namethe party's labelrequired
keythe party's identitythe slug of name
typeits Activity Streams object type, an ObjectType or a stringObjectType::Service
datayour own data stored on the party; replacing it keeps the URL[]
urlits external link; null removes itthe stored link

Renaming a Party ​

A party's name is part of its default key, so a new name creates a new party. Pass the existing key to rename the party instead, and record with the model Party::make() returns:

app/Console/Commands/CancelUnpaidOrders.phphandle()
php
use Storyfeed\Facades\Storyfeed;
use Storyfeed\Models\Party;

$register = Party::make('Front Register', key: 'register');

Storyfeed::activity()
    ->by($register)
    ->action('cancel', $order)
    ->publish();
php
use Storyfeed\Facades\Storyfeed;
use Storyfeed\Models\Party;

$register = Party::make('Front Register', key: 'register');

Storyfeed::record(
    verb: 'cancel',
    object: $order,
    actor: $register,
);

Activities already recorded with the party show the new name. A party name passed as a string goes through Party::make() too, so after the rename by('Front Register') creates a separate party and by('Register') renames it back.

Using Parties in Other Roles ​

A party can fill any role, not only the actor:

app/Http/Controllers/DispatchOrderController.php
php
<?php

namespace App\Http\Controllers;

use App\Models\Order;
use Illuminate\Http\RedirectResponse;
use Storyfeed\Facades\Storyfeed;

class DispatchOrderController extends Controller
{
    public function __invoke(Order $order): RedirectResponse
    {
        $order->update(['dispatched_at' => now()]);

        Storyfeed::activity()
            ->action('dispatch', $order)
            ->to('Front desk')
            ->publish();

        return back();
    }
}
php
<?php

namespace App\Http\Controllers;

use App\Models\Order;
use Illuminate\Http\RedirectResponse;
use Storyfeed\Facades\Storyfeed;

class DispatchOrderController extends Controller
{
    public function __invoke(Order $order): RedirectResponse
    {
        $order->update(['dispatched_at' => now()]);

        Storyfeed::record(
            verb: 'dispatch',
            object: $order,
            target: 'Front desk',
        );

        return back();
    }
}

To retrieve a party model by name, call Storyfeed::party('Front desk'). Storyfeed creates it if it does not exist.

Declaring Party Names ​

A misspelling such as 'Strpie' creates a separate party from 'Stripe'. Declare allowed actor names to detect these mistakes:

app/Providers/AppServiceProvider.phpboot()
php
use Storyfeed\Facades\Storyfeed;

Storyfeed::parties(['Stripe', 'Register']);

Undeclared names are handled according to the environment:

EnvironmentAn Undeclared Name
local, testingthrows UndeclaredParty with the call and allowed names
everywhere elseis ignored; the activity retains its default actor and storyfeed:doctor reports the name

The list applies to Storyfeed::actor() and a verb's actor method. The by method does not check it. Without a list, any name is allowed. Names match by slug, so 'Stripe' and 'stripe' identify the same party.

Set parties.strict in config/storyfeed.php to control whether undeclared names throw an exception. The default, null, throws only in local and testing.

Setting a Default Actor ​

config/storyfeed.php
php
'parties' => [
    // e.g. 'System' — a name for otherwise-anonymous publishes
    'fallback' => null,
],

Without a fallback or another resolved actor, the activity is anonymous.

Resolving the Default Actor ​

By default, Storyfeed records the authenticated user when you omit the actor. To use another authentication guard, set actor_resolver to an invokable class that returns the actor:

app/Support/ResolveFeedActor.php
php
<?php

namespace App\Support;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;

class ResolveFeedActor
{
    public function __invoke(): ?Model
    {
        return Auth::guard('admin')->user() ?? Auth::user();
    }
}
config/storyfeed.php
php
'actor_resolver' => App\Support\ResolveFeedActor::class,

When the resolver returns null, the fallback party applies.

Recording Anonymous Activities ​

If you omit the actor, Storyfeed uses the authenticated user or a configured default. To record no actor, even during an authenticated request, pass null to the by method:

Where the activity happens: a controller, an action, a listener
php
use Storyfeed\Facades\Storyfeed;

Storyfeed::activity()
    ->by(null)
    ->action('place', $order)
    ->to($shop)
    ->publish();
Someone placed Order #1035 with Scoops Ahoy
MethodActor
omit by()resolved from the request or configured defaults
->by(null) or ->actor(null)anonymous
->anonymously()anonymous, on an existing builder
Storyfeed::anonymous()anonymous, on a new builder
Storyfeed::record(..., anonymous: true)anonymous; also supplying a non-null actor: throws an exception

Passing actor: null to Storyfeed::record() still allows the default actor to apply. Use anonymous: true to record no actor with named arguments.

Anonymous Headlines ​

routes/feed.php
php
use App\Models\Order;
use Storyfeed\Facades\Story;

Story::for(Order::class)->verb('confirm')
    ->headline(':actor confirmed :object')
    ->anonymousHeadline(':object was confirmed');

An anonymous activity uses the anonymous headline. A party uses the ordinary headline. Anonymous templates cannot contain :actor. You may also use a closure, as described in The Feed File.

If a verb never records an actor, you may omit :actor from its headline:

routes/feed.php
php
use App\Models\Order;
use Storyfeed\Facades\Story;

Story::for(Order::class)->verb('expire')
    ->headline(':object expired at :target');

Omitting :actor from a headline does not remove the recorded actor.

Released under the MIT License.