Recording Connected App Writes
Introduction
An operator asks an AI assistant, connected to the app, to update a supplier's prices. The assistant makes the change through the app's API. The feed should say who asked for the change and which app made it. An audit later needs who authorised the app to act.
When an operator uses a connected app to create or change a record, record the operator as the actor and the app as the instrument. Keep who authorised the write in a companion audit entry, and retrieve everything done through the app with the instrument filter.
Choosing the Actor and Instrument
For a deliberate operator command, record the operator as actor and the connected app as instrument when your app treats the operator as performing the requested action. Choose roles for the fact the activity records:
| Execution | Actor | Instrument | Authorization |
|---|---|---|---|
| An operator commands a connected app to create a record | operator | connected app | retain the authorizer and token owner separately |
| An app independently creates a record | connected app | a separate tool, if used | retain the authorizer and token owner separately |
| A person approves another person's work | person who performed the work | tool used, if any | the approval record identifies the approver; see Recording an Authoriser |
A token owner is not automatically the performer. Storyfeed has no built-in authorizer role. Store authorization in your app's audit record and copy relevant event-time values into activity data. A connected app with its own model can be feedable; a service without a model can be a party.
Use create when the record is created and update or edit when it changes. The transport belongs in the instrument or activity data. See Choosing a Verb.
Recording a Companion Audit Entry
Define different headlines for the business record and the audit entry:
use App\Models\AuditEntry;
use App\Models\MenuItem;
use Storyfeed\Facades\Story;
Story::for(MenuItem::class)->verb('create')
->headline(':actor created :object using :instrument');
Story::for(AuditEntry::class)->verb('create')
->headline(':actor recorded :object for :target using :instrument');The action creates a menu item and a mandatory audit entry. MenuItem, ConnectedApp and AuditEntry implement Feedable; the audit entry labels its own record. The audit model casts before and after to arrays and allows the attributes shown below:
<?php
namespace App\Actions;
use App\Models\AuditEntry;
use App\Models\ConnectedApp;
use App\Models\MenuItem;
use App\Models\User;
use Illuminate\Support\Facades\DB;
use Storyfeed\Facades\Storyfeed;
class CreateMenuItem
{
public function handle(
User $operator,
User $authoriser,
User $tokenOwner,
ConnectedApp $app,
array $attributes,
): MenuItem {
// The caller has authenticated the app and authorized this command.
return DB::transaction(function () use (
$operator, $authoriser, $tokenOwner, $app, $attributes,
) {
$item = MenuItem::create($attributes);
$facts = [
'operation' => 'create',
'before' => null,
'after' => $item->only('name'),
'tool' => 'create_menu_item',
'authoriser_id' => $authoriser->getKey(),
'token_owner_id' => $tokenOwner->getKey(),
];
$entry = AuditEntry::create([
...$facts,
'subject_type' => $item->getMorphClass(),
'subject_id' => $item->getKey(),
]);
// If a domain event publishes this fact, keep that as its only site.
Storyfeed::activity()
->by($operator)
->action('create', $item)
->using($app)
->publish();
Storyfeed::activity()
->by($operator)
->action('create', $entry)
->to($item)
->using($app)
->data($facts)
->publish();
return $item;
});
}
}<?php
namespace App\Actions;
use App\Models\AuditEntry;
use App\Models\ConnectedApp;
use App\Models\MenuItem;
use App\Models\User;
use Illuminate\Support\Facades\DB;
use Storyfeed\Facades\Storyfeed;
class CreateMenuItem
{
public function handle(
User $operator,
User $authoriser,
User $tokenOwner,
ConnectedApp $app,
array $attributes,
): MenuItem {
// The caller has authenticated the app and authorized this command.
return DB::transaction(function () use (
$operator, $authoriser, $tokenOwner, $app, $attributes,
) {
$item = MenuItem::create($attributes);
$facts = [
'operation' => 'create',
'before' => null,
'after' => $item->only('name'),
'tool' => 'create_menu_item',
'authoriser_id' => $authoriser->getKey(),
'token_owner_id' => $tokenOwner->getKey(),
];
$entry = AuditEntry::create([
...$facts,
'subject_type' => $item->getMorphClass(),
'subject_id' => $item->getKey(),
]);
// If a domain event publishes this fact, keep that as its only site.
Storyfeed::record(
verb: 'create',
object: $item,
actor: $operator,
instrument: $app,
);
Storyfeed::record(
verb: 'create',
object: $entry,
actor: $operator,
target: $item,
data: $facts,
instrument: $app,
);
return $item;
});
}
}- Price
- $2.95
- Section
- Sundaes
- Available
- At the counter
- Orders
- 12
[
{
"kind": "activity",
"id": "delegated-business",
"verb": "create",
"published_at": "1985-07-04T18:10:00.000000Z",
"starts_at": null,
"ends_at": null,
"headline_template": ":actor created :object using :instrument",
"headline": null,
"bindings": null,
"glyph": "git-merge",
"glyph_intent": null,
"actor": {
"type": "user",
"id": "101",
"label": "Steve Harrington",
"link": {
"href": "/users/101",
"modal": false,
"attributes": []
},
"data": [],
"media": {
"icon": {
"src": "/media/worlds/stranger-things/avatars/scooper.jpg",
"mediaType": "image/jpeg",
"width": 256,
"height": 256,
"alt": null
},
"image": null,
"preview": null,
"initials": null,
"color": null,
"files": [],
"slots": []
},
"media_ref": "md08aeb0f13f95241",
"entity_ref": "e6b153676d8dbc853",
"body": null,
"tombstone": null
},
"object": {
"type": "menu_item",
"id": "101",
"label": "USS Butterscotch",
"link": {
"href": "/menu/butterscotch",
"modal": false,
"attributes": []
},
"data": [],
"media": {
"icon": null,
"image": null,
"preview": null,
"initials": "UB",
"color": "#0f766e",
"files": [],
"slots": []
},
"media_ref": "mf5acd255ffaa7539",
"entity_ref": "e8f1e747924c2a5dc",
"body": [
{
"$body": "Storyfeed/Body/KeyValue",
"$v": 3,
"title": "USS Butterscotch",
"items": [
{
"key": "Price",
"value": "$2.95"
},
{
"key": "Section",
"value": "Sundaes"
},
{
"key": "Available",
"value": "At the counter"
},
{
"key": "Orders",
"value": 12
}
]
}
],
"tombstone": null
},
"target": null,
"context": null,
"origin": null,
"result": null,
"instrument": {
"type": "connected_app",
"id": "306",
"label": "Menu Desk",
"link": {
"href": "/connected-apps/306",
"modal": false,
"attributes": []
},
"data": [],
"media": {
"icon": null,
"image": null,
"preview": null,
"initials": "MD",
"color": "#9f1239",
"files": [],
"slots": []
},
"media_ref": "ma9d42e68a89ae77f",
"entity_ref": "e618c03f5ba12c684",
"body": null,
"tombstone": null
},
"location": null,
"generator": null,
"featured": "object",
"data": null,
"tombstoned": [],
"redundant": false,
"missing_headline_template": null,
"missing_headline": null
},
{
"kind": "activity",
"id": "delegated-audit",
"verb": "create",
"published_at": "1985-07-04T18:10:00.000000Z",
"starts_at": null,
"ends_at": null,
"headline_template": ":actor recorded :object for :target using :instrument",
"headline": null,
"bindings": null,
"glyph": "git-merge",
"glyph_intent": null,
"actor": {
"type": "user",
"id": "101",
"label": "Steve Harrington",
"link": {
"href": "/users/101",
"modal": false,
"attributes": []
},
"data": [],
"media": {
"icon": {
"src": "/media/worlds/stranger-things/avatars/scooper.jpg",
"mediaType": "image/jpeg",
"width": 256,
"height": 256,
"alt": null
},
"image": null,
"preview": null,
"initials": null,
"color": null,
"files": [],
"slots": []
},
"media_ref": "md08aeb0f13f95241",
"entity_ref": "e6b153676d8dbc853",
"body": null,
"tombstone": null
},
"object": {
"type": "audit_entry",
"id": "307",
"label": "Creation audit",
"link": {
"href": "/audit-entries/307",
"modal": false,
"attributes": []
},
"data": [],
"media": {
"icon": null,
"image": null,
"preview": null,
"initials": "CA",
"color": "#be123c",
"files": [],
"slots": []
},
"media_ref": "mc2bcb7b446113975",
"entity_ref": "e2cb7e8639d18636c",
"body": null,
"tombstone": null
},
"target": {
"type": "menu_item",
"id": "101",
"label": "USS Butterscotch",
"link": {
"href": "/menu/butterscotch",
"modal": false,
"attributes": []
},
"data": [],
"media": {
"icon": null,
"image": null,
"preview": null,
"initials": "UB",
"color": "#0f766e",
"files": [],
"slots": []
},
"media_ref": "mf5acd255ffaa7539",
"entity_ref": "e8f1e747924c2a5dc",
"body": [
{
"$body": "Storyfeed/Body/KeyValue",
"$v": 3,
"title": "USS Butterscotch",
"items": [
{
"key": "Price",
"value": "$2.95"
},
{
"key": "Section",
"value": "Sundaes"
},
{
"key": "Available",
"value": "At the counter"
},
{
"key": "Orders",
"value": 12
}
]
}
],
"tombstone": null
},
"context": null,
"origin": null,
"result": null,
"instrument": {
"type": "connected_app",
"id": "306",
"label": "Menu Desk",
"link": {
"href": "/connected-apps/306",
"modal": false,
"attributes": []
},
"data": [],
"media": {
"icon": null,
"image": null,
"preview": null,
"initials": "MD",
"color": "#9f1239",
"files": [],
"slots": []
},
"media_ref": "ma9d42e68a89ae77f",
"entity_ref": "e618c03f5ba12c684",
"body": null,
"tombstone": null
},
"location": null,
"generator": null,
"featured": "object",
"data": {
"operation": "create",
"before": null,
"after": {
"name": "USS Butterscotch"
},
"tool": "create_menu_item",
"authoriser_id": "101",
"token_owner_id": "101"
},
"tombstoned": [],
"redundant": false,
"missing_headline_template": null,
"missing_headline": null
}
]For an edit, capture the item's values before the update and its values afterward in the audit entry.
Keep one publication site for the business activity. If a domain event already publishes the menu item's creation, pass the operator and app to that event and leave only the audit publication in this action. See Choosing When to Publish.
The application owns atomic persistence of its business record and mandatory audit ledger. This transaction requires those models to share a database connection. Queued or after-commit feed publication is separate from that ledger requirement. Filtering a display feed does not make the ledger atomic.
Filtering by App Instrument
To retrieve activities performed using one connected app, filter by the instrument role:
use App\Models\ConnectedApp;
use Illuminate\Support\Facades\Route;
use Storyfeed\Facades\Storyfeed;
// Apply the app's authorization middleware to this route.
Route::get('/connected-apps/{app}/activity', function (ConnectedApp $app) {
return Storyfeed::feed()
->instrument($app)
->log()
->get();
});- Price
- $2.95
- Section
- Sundaes
- Available
- At the counter
- Orders
- 12
The instrument filter retrieves both records, even when their actors differ. An app_name value in data does not fill the instrument role. involving($app) is broader: it also matches the app in any other role. To display only business records while retaining audit activities, filter by the object type:
use App\Models\ConnectedApp;
use App\Models\MenuItem;
use Illuminate\Support\Facades\Route;
use Storyfeed\Facades\Storyfeed;
// Apply the app's authorization middleware to this route.
Route::get('/connected-apps/{app}/activity', function (ConnectedApp $app) {
return Storyfeed::feed()
->instrument($app)
->objectType(MenuItem::class)
->log()
->get();
});- Price
- $2.95
- Section
- Sundaes
- Available
- At the counter
- Orders
- 12