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

Feed Media ​

Introduction ​

A model's toFeed() method describes what is stored. Its static feedMedia() method resolves links and media when a feed is retrieved:

app/Models/Photo.php
php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Storyfeed\Concerns\InteractsWithFeed;
use Storyfeed\Contracts\Feedable;
use Storyfeed\FeedContext;
use Storyfeed\FeedMedia;

class Photo extends Model implements Feedable
{
    use InteractsWithFeed;

    public static function feedMedia(FeedContext $context): ?FeedMedia
    {
        return FeedMedia::make()->url(route('photos.show', $context->routeKey()));
    }
}
php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Storyfeed\Concerns\InteractsWithFeed;
use Storyfeed\Contracts\Feedable;
use Storyfeed\FeedContext;
use Storyfeed\FeedMedia;

class Photo extends Model implements Feedable
{
    use InteractsWithFeed;

    public static function feedMedia(FeedContext $context): ?FeedMedia
    {
        return FeedMedia::make(
            url: route('photos.show', $context->routeKey()),
        );
    }
}
Steve Harrington

The method receives a FeedContext with the snapshot's label, route key and data, and returns a FeedMedia or null. Resolving URLs here lets a route or thumbnail change without rewriting stored picture bodies. A composed feed calls feedMedia() too, each time it is read.

Using a Closure ​

With InteractsWithFeed, you may register a closure in booted() instead:

app/Models/Photo.phpbooted()
php
use Storyfeed\FeedContext;
use Storyfeed\FeedMedia;

static::feedMediaUsing(
    fn (FeedContext $context, FeedMedia $media): FeedMedia => $media
        ->url(route('photos.show', $context->routeKey())),
);
Steve Harrington

A model's own feedMedia() method takes precedence over the registered closure. A closure may also return a URL string or null. Without either resolver, the trait returns null.

Linking to the Model ​

Use url() for the destination of the entity's name in the headline. To say more about the link, pass a FeedLink to link(). Its modal() method hints that the renderer should open the link in place, and attributes() adds link attributes:

app/Models/Photo.php
php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Storyfeed\Concerns\InteractsWithFeed;
use Storyfeed\Contracts\Feedable;
use Storyfeed\FeedContext;
use Storyfeed\FeedLink;
use Storyfeed\FeedMedia;

class Photo extends Model implements Feedable
{
    use InteractsWithFeed;

    public static function feedMedia(FeedContext $context): ?FeedMedia
    {
        return FeedMedia::make()
            ->link(
                FeedLink::to(route('photos.show', $context->routeKey()))
                    ->modal()
                    ->attributes(['aria-label' => 'Open photo'])
            );
    }
}
php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Storyfeed\Concerns\InteractsWithFeed;
use Storyfeed\Contracts\Feedable;
use Storyfeed\FeedContext;
use Storyfeed\FeedLink;
use Storyfeed\FeedMedia;

class Photo extends Model implements Feedable
{
    use InteractsWithFeed;

    public static function feedMedia(FeedContext $context): ?FeedMedia
    {
        return FeedMedia::make(
            link: FeedLink::to(route('photos.show', $context->routeKey()))
                ->modal()
                ->attributes(['aria-label' => 'Open photo']),
        );
    }
}
Steve Harrington

A link does not show a picture; see Showing Pictures. A resolver can also choose a different link for each feed.

modal() sets the entity's link.modal to true; the URL stays an ordinary destination. url($href) is the short form of link(FeedLink::to($href)).

Linking Files ​

A Document model describes its PDF with a FileAttachment body in toFeed(). The body stores the file's name, size and media type. Use files() in feedMedia() to supply the link that opens the document and the file to download:

app/Models/Document.php
php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Storyfeed\Body\FileAttachment;
use Storyfeed\Concerns\InteractsWithFeed;
use Storyfeed\Contracts\Feedable;
use Storyfeed\FeedContext;
use Storyfeed\FeedEntity;
use Storyfeed\FeedMedia;
use Storyfeed\FeedResource;

class Document extends Model implements Feedable
{
    use InteractsWithFeed;

    public function toFeed(): FeedEntity
    {
        return FeedEntity::make()
            ->label($this->name)
            ->body(
                FileAttachment::make()
                    ->size($this->bytes)
                    ->mediaType('application/pdf')
                    ->name($this->name)
            );
    }

    public static function feedMedia(FeedContext $context): ?FeedMedia
    {
        return FeedMedia::make()
            ->url(route('documents.show', $context->routeKey()))
            ->files(
                FeedResource::make()
                    ->href(route('documents.download', $context->routeKey()))
                    ->name($context->label())
                    ->mediaType('application/pdf')
            );
    }
}
php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Storyfeed\Body\FileAttachment;
use Storyfeed\Concerns\InteractsWithFeed;
use Storyfeed\Contracts\Feedable;
use Storyfeed\FeedContext;
use Storyfeed\FeedEntity;
use Storyfeed\FeedMedia;
use Storyfeed\FeedResource;

class Document extends Model implements Feedable
{
    use InteractsWithFeed;

    public function toFeed(): FeedEntity
    {
        return FeedEntity::make(
            label: $this->name,
            body: FileAttachment::make(
                size: $this->bytes,
                mediaType: 'application/pdf',
                name: $this->name,
            ),
        );
    }

    public static function feedMedia(FeedContext $context): ?FeedMedia
    {
        return FeedMedia::make(
            url: route('documents.show', $context->routeKey()),
            files: [
                FeedResource::make(
                    href: route('documents.download', $context->routeKey()),
                    mediaType: 'application/pdf',
                    name: $context->label(),
                ),
            ],
        );
    }
}
Steve Harrington

Signed Agreement.pdf PDF · 138 KB

The entity link points to the document. The files() list adds resources to entity.media.files; it does not create a body. Each call appends resources. Activity Streams output carries them in its attachment property.

Showing Pictures ​

feedMedia() resolves pictures into the entity's media. To show one in a body, name its slot: declare an Image body in toFeed() with the model's feedMediaPreview(), then supply the preview from feedMedia():

app/Models/Photo.php
php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Storyfeed\Body\Image;
use Storyfeed\Concerns\InteractsWithFeed;
use Storyfeed\Contracts\Feedable;
use Storyfeed\FeedContext;
use Storyfeed\FeedEntity;
use Storyfeed\FeedImage;
use Storyfeed\FeedMedia;

class Photo extends Model implements Feedable
{
    use InteractsWithFeed;

    public function toFeed(): FeedEntity
    {
        return FeedEntity::make()
            ->label($this->name)
            ->body(
                Image::make($this->feedMediaPreview())
                    ->caption($this->subject)
                    ->alt($this->description)
            );
    }

    public static function feedMedia(FeedContext $context): ?FeedMedia
    {
        return FeedMedia::make()
            ->url(route('photos.show', $context->routeKey()))
            ->preview(FeedImage::make()->src(route('photos.thumbnail', $context->routeKey())));
    }
}
php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Storyfeed\Body\Image;
use Storyfeed\Concerns\InteractsWithFeed;
use Storyfeed\Contracts\Feedable;
use Storyfeed\FeedContext;
use Storyfeed\FeedEntity;
use Storyfeed\FeedImage;
use Storyfeed\FeedMedia;

class Photo extends Model implements Feedable
{
    use InteractsWithFeed;

    public function toFeed(): FeedEntity
    {
        return FeedEntity::make(
            label: $this->name,
            body: Image::make(
                image: $this->feedMediaPreview(),
                caption: $this->subject,
                alt: $this->description,
            ),
        );
    }

    public static function feedMedia(FeedContext $context): ?FeedMedia
    {
        return FeedMedia::make(
            url: route('photos.show', $context->routeKey()),
            preview: FeedImage::make(
                src: route('photos.thumbnail', $context->routeKey()),
            ),
        );
    }
}
Steve Harrington
USS Butterscotch
USS Butterscotch

The caption belongs to the body. The body stores the slot's name, and the picture is resolved each time the feed is read, so a changed thumbnail shows on every row. Image::make() with no picture names the preview slot. MediaObject::image() accepts a slot too, or a FeedImage, for a picture alongside its text.

The three slots retain their Activity Streams 2.0 names:

SlotJobModel Method
icona small representation that identifies the entity, such as an avatar or logofeedMediaIcon()
previewa preview of the entity, such as a photograph's thumbnail or a page's link-card imagefeedMediaPreview()
imagea larger picture of a non-image entity, such as a menu itemfeedMediaImage()

Each method is shorthand for getFeedMedia(), such as getFeedMedia('icon').

The AS2 serializer emits these as icon, preview, and image. None is the entity's link.

To retain image dimensions or a media type with the entity, store snapshot data in toFeed().

Adding Your Own Slots ​

For a picture the three slots do not describe, such as a chart of a menu item's orders, name a slot of your own with slot(), and show it in a body with getFeedMedia():

app/Models/MenuItem.php
php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Storyfeed\Body\Image;
use Storyfeed\Concerns\InteractsWithFeed;
use Storyfeed\Contracts\Feedable;
use Storyfeed\FeedContext;
use Storyfeed\FeedEntity;
use Storyfeed\FeedImage;
use Storyfeed\FeedMedia;

class MenuItem extends Model implements Feedable
{
    use InteractsWithFeed;

    public function toFeed(): FeedEntity
    {
        return FeedEntity::make()
            ->label($this->name)
            ->body(Image::make($this->getFeedMedia('sparkline')));
    }

    public static function feedMedia(FeedContext $context): ?FeedMedia
    {
        return FeedMedia::make()
            ->url(route('menu.show', $context->routeKey()))
            ->slot('sparkline', FeedImage::make()
                ->src(route('menu.sparkline', $context->routeKey()))
                ->width(120)
                ->height(24)
                ->alt('Orders this week'));
    }
}
php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Storyfeed\Body\Image;
use Storyfeed\Concerns\InteractsWithFeed;
use Storyfeed\Contracts\Feedable;
use Storyfeed\FeedContext;
use Storyfeed\FeedEntity;
use Storyfeed\FeedImage;
use Storyfeed\FeedMedia;

class MenuItem extends Model implements Feedable
{
    use InteractsWithFeed;

    public function toFeed(): FeedEntity
    {
        return FeedEntity::make(
            label: $this->name,
            body: Image::make(image: $this->getFeedMedia('sparkline')),
        );
    }

    public static function feedMedia(FeedContext $context): ?FeedMedia
    {
        return FeedMedia::make(
            url: route('menu.show', $context->routeKey()),
        )->slot('sparkline', FeedImage::make(
            src: route('menu.sparkline', $context->routeKey()),
            width: 120,
            height: 24,
            alt: 'Orders this week',
        ));
    }
}
Steve Harrington
Steve Harrington added a photo of something
Orders this week

A slot name is letters, digits, _ and -, starting with a letter. The built-in names icon, preview and image have their own methods. Custom slots appear under media.slots in the payload and are left out of Activity Streams output. Pass a generated SVG as a data: URL in src.

Giving an Actor an Avatar ​

Any feedable model can be an actor. Its icon slot supplies the avatar when it appears in that role, even if it is usually the object of an activity:

app/Models/Photo.php
php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Storyfeed\Concerns\InteractsWithFeed;
use Storyfeed\Contracts\Feedable;
use Storyfeed\FeedContext;
use Storyfeed\FeedImage;
use Storyfeed\FeedMedia;

class Photo extends Model implements Feedable
{
    use InteractsWithFeed;

    public static function feedMedia(FeedContext $context): ?FeedMedia
    {
        return FeedMedia::make()
            ->url(route('photos.show', $context->routeKey()))
            ->icon(FeedImage::make()->src(route('photos.thumbnail', $context->routeKey())));
    }
}
php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Storyfeed\Concerns\InteractsWithFeed;
use Storyfeed\Contracts\Feedable;
use Storyfeed\FeedContext;
use Storyfeed\FeedImage;
use Storyfeed\FeedMedia;

class Photo extends Model implements Feedable
{
    use InteractsWithFeed;

    public static function feedMedia(FeedContext $context): ?FeedMedia
    {
        return FeedMedia::make(
            url: route('photos.show', $context->routeKey()),
            icon: FeedImage::make(
                src: route('photos.thumbnail', $context->routeKey()),
            ),
        );
    }
}
A sundae with whipped cream, sauce and a cherry

An avatar needs no Image body.

Showing Initials Instead of a Picture ​

A model without a picture, such as a team or a project, may give initials and a disc colour instead. Store both values in toFeed() to read them from $context->data():

app/Models/Team.php
php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Storyfeed\Concerns\InteractsWithFeed;
use Storyfeed\Contracts\Feedable;
use Storyfeed\FeedContext;
use Storyfeed\FeedEntity;
use Storyfeed\FeedMedia;

class Team extends Model implements Feedable
{
    use InteractsWithFeed;

    public function toFeed(): FeedEntity
    {
        return FeedEntity::make()
            ->label($this->name)
            ->data(['initials' => $this->initials, 'color' => $this->color]);
    }

    public static function feedMedia(FeedContext $context): ?FeedMedia
    {
        return FeedMedia::make()
            ->url(route('teams.show', $context->routeKey()))
            ->initials($context->data('initials'))
            ->color($context->data('color'));
    }
}
php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Storyfeed\Concerns\InteractsWithFeed;
use Storyfeed\Contracts\Feedable;
use Storyfeed\FeedContext;
use Storyfeed\FeedEntity;
use Storyfeed\FeedMedia;

class Team extends Model implements Feedable
{
    use InteractsWithFeed;

    public function toFeed(): FeedEntity
    {
        return FeedEntity::make(
            label: $this->name,
            data: ['initials' => $this->initials, 'color' => $this->color],
        );
    }

    public static function feedMedia(FeedContext $context): ?FeedMedia
    {
        return FeedMedia::make(
            url: route('teams.show', $context->routeKey()),
            initials: $context->data('initials'),
            color: $context->data('color'),
        );
    }
}
ST
Crack the code

Work out what the Russian message on the tape is saying.

The colour is a hex value, such as #438d98. Without them, Storyfeed derives initials from the label and a colour from the type and key. An entity with an icon gets neither. See Storing Snapshot Data.

See the Feedable API for the complete method list.

For bodies built from current model values, see Resolving Bodies When Retrieved. Link and image resolvers can share the same FeedMedia with those bodies.

Released under the MIT License.