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

Choosing When to Publish ​

Introduction ​

Storyfeed is a side effect, never the source. Your own domain events are the source of truth, and the feed is one subscriber to them: dispatch an app event where the transition succeeds, have it implement Storyfeed\Contracts\PublishesToFeed, and return the activity from toFeedActivity(). Business listeners subscribe to your event, not to Storyfeed's. See Publishing From Events.

An order is saved many times while it is being prepared: a note is added, the table changes, a line is corrected. Only a few of those saves matter to someone reading the feed, such as when the order was placed, confirmed or cancelled. Publishing from every save fills the feed with near-identical rows. Publishing from the status changes records what happened.

Publish meaningful status changes so routine saves do not create duplicate activities.

Choosing a Publish Site ​

Publish each transition from one site, so it is recorded once:

Call SiteUse For
domain event via PublishesToFeedmeaningful application events; create an event when the transition needs one. See Publishing From Events
model observermodel transitions while the application has no domain event
action or service classdispatching the domain event where the transition succeeds
inline Storyfeed::activity()->publish()imports, backfills, console commands and one-offs, where there is no domain transition
none: compose it when reada record's own page, from timestamps it already stores; nothing reaches other feeds

Publishing From an Observer ​

Use a model observer when the application has no domain event for a transition.

Create an observer for the model whose status changes:

shell
php artisan make:observer OrderObserver --model=Order

In the observer's updated method, publish only the transitions the feed should show:

app/Observers/OrderObserver.php
php
<?php

namespace App\Observers;

use App\Models\Order;
use Storyfeed\Facades\Storyfeed;

class OrderObserver
{
    public function updated(Order $order): void
    {
        if (! $order->wasChanged('status')) {
            return;                                  // no status change
        }

        $verb = match ($order->status) {
            'confirmed' => 'confirm',
            'ready' => 'prepare',
            'completed' => 'complete',
            default => null,                         // drafts are not recorded
        };

        if ($verb === null) {
            return;
        }

        Storyfeed::activity()
            ->action($verb, $order)
            ->publish();
    }
}
php
<?php

namespace App\Observers;

use App\Models\Order;
use Storyfeed\Facades\Storyfeed;

class OrderObserver
{
    public function updated(Order $order): void
    {
        if (! $order->wasChanged('status')) {
            return;                                  // no status change
        }

        $verb = match ($order->status) {
            'confirmed' => 'confirm',
            'ready' => 'prepare',
            'completed' => 'complete',
            default => null,                         // drafts are not recorded
        };

        if ($verb === null) {
            return;
        }

        Storyfeed::record(
            verb: $verb,
            object: $order,
        );
    }
}

Register the observer in your service provider:

app/Providers/AppServiceProvider.phpboot()
php
use App\Models\Order;
use App\Observers\OrderObserver;

Order::observe(OrderObserver::class);

Define a headline for each verb:

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

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

Story::for(Order::class)->verb('prepare')
    ->headline(':actor prepared :object');

Story::for(Order::class)->verb('complete')
    ->headline(':actor completed :object');

Without by(), Storyfeed records the signed-in user as the actor:

Steve Harrington

A save in a console command or a scheduled task has no signed-in user. The activity then uses the default actor, or has no actor.

Using One Verb per Transition ​

Use a separate verb for each transition so each has its own headline and keepLatest() can keep its latest activity. A single status verb with the state in data does not distinguish transitions this way.

Choosing Events to Record ​

Record what the reader of this particular feed would want to follow. The same event can belong on an admin feed and not on a public one. For a customer feed:

EventRecord It?Reason
model created as a draftnoit is not ready for others to act on
save with no status changenono visible change occurred
note text editednothe note's body already displays its current text
background indexing, cache rebuilding, or setting a dirty flagnointernal maintenance is not useful feed content
someone typing or coming onlinenothe state may change within seconds
field-level audit recordnokeep detailed change history in an audit log
order placed → confirmedyes, as confirmeach status transition describes an event; give each its own verb
order confirmed → readyyes, as prepareeach status transition describes an event
order ready → completedyes, as completeeach status transition describes an event
question asked about a menu itemyesthe activity identifies the subject and can quote the question
order placedyesothers can follow the order's progress
order viewedtemporarilyuse the verb's retention period to limit how long it remains

To leave a draft out, return null from the event's toFeedActivity(); see Skipping Publication.

Released under the MIT License.