|
| 1 | +--- |
| 2 | +description: >- |
| 3 | + Fedify provides a way to register outbox listeners so that you can handle |
| 4 | + client-to-server `POST` requests to actor outboxes. This section explains |
| 5 | + how to register an outbox listener and how to federate posted activities. |
| 6 | +--- |
| 7 | + |
| 8 | +Outbox listeners |
| 9 | +================ |
| 10 | + |
| 11 | +Fedify can route `POST` requests to an actor's outbox through typed listeners. |
| 12 | +This is useful when you want to accept ActivityPub client-to-server activities |
| 13 | +from your own clients without exposing a separate non-standard API. |
| 14 | + |
| 15 | +This guide covers `POST /outbox`. To serve `GET /outbox`, use the |
| 16 | +[*Collections*][collections-outbox] guide. |
| 17 | + |
| 18 | +[collections-outbox]: ./collections.md#outbox |
| 19 | + |
| 20 | + |
| 21 | +Registering an outbox listener |
| 22 | +------------------------------ |
| 23 | + |
| 24 | +With Fedify, you can register outbox listeners per activity type, just like |
| 25 | +inbox listeners. The following shows how to register a listener for `Create` |
| 26 | +activities: |
| 27 | + |
| 28 | +~~~~ typescript twoslash |
| 29 | +import { type Federation } from "@fedify/fedify"; |
| 30 | +import { Activity, Create, Person } from "@fedify/vocab"; |
| 31 | +const federation = null as unknown as Federation<void>; |
| 32 | +const myKnownRecipients: Person[] = []; |
| 33 | +async function verifyAccessToken( |
| 34 | + authorization: string | null, |
| 35 | +): Promise<{ identifier: string } | null> { |
| 36 | + authorization; |
| 37 | + return null; |
| 38 | +} |
| 39 | +async function savePostedActivity( |
| 40 | + identifier: string, |
| 41 | + activity: Activity, |
| 42 | +): Promise<void> { |
| 43 | + identifier; |
| 44 | + activity; |
| 45 | +} |
| 46 | +// ---cut-before--- |
| 47 | +federation |
| 48 | + .setOutboxListeners("/users/{identifier}/outbox") |
| 49 | + .on(Create, async (ctx, activity) => { |
| 50 | + await savePostedActivity(ctx.identifier, activity); |
| 51 | + await ctx.sendActivity( |
| 52 | + { identifier: ctx.identifier }, |
| 53 | + myKnownRecipients, |
| 54 | + activity, |
| 55 | + ); |
| 56 | + }) |
| 57 | + .authorize(async (ctx, identifier) => { |
| 58 | + const session = await verifyAccessToken( |
| 59 | + ctx.request.headers.get("authorization"), |
| 60 | + ); |
| 61 | + return session?.identifier === identifier; |
| 62 | + }); |
| 63 | +~~~~ |
| 64 | + |
| 65 | +The `~Federatable.setOutboxListeners()` method registers the outbox path, and |
| 66 | +the `~OutboxListenerSetters.on()` method registers a listener for a specific |
| 67 | +activity type. The `~OutboxListenerSetters.authorize()` hook runs before the |
| 68 | +listener and can reject unauthorized requests with `401 Unauthorized`. |
| 69 | + |
| 70 | +Fedify also rejects a posted activity if its `actor` does not match the local |
| 71 | +actor who owns the addressed outbox. |
| 72 | + |
| 73 | +> [!TIP] |
| 74 | +> If you need to handle every activity type, register a listener for the |
| 75 | +> `Activity` class. Unsupported activity types can also be left unhandled, |
| 76 | +> in which case Fedify responds with `202 Accepted` without dispatching a |
| 77 | +> listener. |
| 78 | +
|
| 79 | +> [!NOTE] |
| 80 | +> The URI Template syntax supports different expansion types like |
| 81 | +> `{identifier}` (simple expansion) and `{+identifier}` (reserved expansion). |
| 82 | +> If your identifiers contain URIs or special characters, you may need to use |
| 83 | +> `{+identifier}` to avoid double-encoding issues. See the |
| 84 | +> [*URI Template* guide][uri-template-guide] for details. |
| 85 | +
|
| 86 | +[uri-template-guide]: ./uri-template.md |
| 87 | + |
| 88 | + |
| 89 | +Looking at `OutboxContext.identifier` |
| 90 | +------------------------------------- |
| 91 | + |
| 92 | +The `~OutboxContext.identifier` property contains the identifier from the |
| 93 | +matched outbox route. Fedify does not infer anything more specific than that. |
| 94 | + |
| 95 | +~~~~ typescript twoslash |
| 96 | +import { type OutboxListenerSetters } from "@fedify/fedify"; |
| 97 | +import { Create } from "@fedify/vocab"; |
| 98 | +(0 as unknown as OutboxListenerSetters<void>) |
| 99 | +// ---cut-before--- |
| 100 | +.on(Create, async (ctx, activity) => { |
| 101 | + console.log(ctx.identifier); |
| 102 | + console.log(activity.id?.href); |
| 103 | +}); |
| 104 | +~~~~ |
| 105 | + |
| 106 | + |
| 107 | +Federating posted activities |
| 108 | +---------------------------- |
| 109 | + |
| 110 | +Fedify does not federate client-posted activities automatically. If you want |
| 111 | +to deliver a posted activity, call `~Context.sendActivity()` or |
| 112 | +`~OutboxContext.forwardActivity()` explicitly inside your outbox listener. |
| 113 | + |
| 114 | +~~~~ typescript twoslash |
| 115 | +import { type Federation } from "@fedify/fedify"; |
| 116 | +import { Create, Person } from "@fedify/vocab"; |
| 117 | +const federation = null as unknown as Federation<void>; |
| 118 | +const recipients: Person[] = []; |
| 119 | +// ---cut-before--- |
| 120 | +federation |
| 121 | + .setOutboxListeners("/users/{identifier}/outbox") |
| 122 | + .on(Create, async (ctx, activity) => { |
| 123 | + await ctx.sendActivity( |
| 124 | + { identifier: ctx.identifier }, |
| 125 | + recipients, |
| 126 | + activity, |
| 127 | + ); |
| 128 | + }); |
| 129 | +~~~~ |
| 130 | + |
| 131 | +If the client already signed the posted JSON-LD with Linked Data Signatures or |
| 132 | +Object Integrity Proofs and you want to preserve that payload verbatim, use |
| 133 | +`~OutboxContext.forwardActivity()` instead of round-tripping through Fedify's |
| 134 | +vocabulary objects: |
| 135 | + |
| 136 | +~~~~ typescript twoslash |
| 137 | +import { type Federation } from "@fedify/fedify"; |
| 138 | +import { Activity, Person } from "@fedify/vocab"; |
| 139 | +const federation = null as unknown as Federation<void>; |
| 140 | +const recipients: Person[] = []; |
| 141 | +// ---cut-before--- |
| 142 | +federation |
| 143 | + .setOutboxListeners("/users/{identifier}/outbox") |
| 144 | + .on(Activity, async (ctx) => { |
| 145 | + await ctx.forwardActivity( |
| 146 | + { identifier: ctx.identifier }, |
| 147 | + recipients, |
| 148 | + { skipIfUnsigned: true }, |
| 149 | + ); |
| 150 | + }); |
| 151 | +~~~~ |
| 152 | + |
| 153 | +If a listener returns without calling one of these delivery methods, Fedify |
| 154 | +logs a runtime warning. The `@fedify/lint` package also provides a lint rule |
| 155 | +for the same mistake; see [*Linting*][linting-guide] for details. |
| 156 | + |
| 157 | +> [!TIP] |
| 158 | +> Explicit delivery keeps outbox listeners symmetric with inbox listeners: |
| 159 | +> Fedify never guesses the recipient list for you, so applications can reuse |
| 160 | +> their own caches and delivery policies. |
| 161 | +
|
| 162 | +[linting-guide]: ./lint.md |
| 163 | + |
| 164 | + |
| 165 | +Handling errors |
| 166 | +--------------- |
| 167 | + |
| 168 | +You can attach an error handler to outbox listeners. It receives the outbox |
| 169 | +context along with the thrown error: |
| 170 | + |
| 171 | +~~~~ typescript twoslash |
| 172 | +import { type Federation } from "@fedify/fedify"; |
| 173 | +import { Activity } from "@fedify/vocab"; |
| 174 | +const federation = null as unknown as Federation<void>; |
| 175 | +// ---cut-before--- |
| 176 | +federation |
| 177 | + .setOutboxListeners("/users/{identifier}/outbox") |
| 178 | + .on(Activity, async () => { |
| 179 | + throw new Error("Something went wrong."); |
| 180 | + }) |
| 181 | + .onError(async (ctx, error) => { |
| 182 | + console.error(ctx.identifier, error); |
| 183 | + }); |
| 184 | +~~~~ |
| 185 | + |
| 186 | + |
| 187 | +Current scope |
| 188 | +------------- |
| 189 | + |
| 190 | +Outbox listeners currently provide the routing and authorization surface for |
| 191 | +client-to-server posting, but the rest of the server-side behavior remains |
| 192 | +application-defined. |
| 193 | + |
| 194 | +In particular, Fedify does not currently do the following for you: |
| 195 | + |
| 196 | + - Persist the posted activity in your outbox collection |
| 197 | + - Generate IDs or `Location` headers for newly posted activities |
| 198 | + - Wrap non-`Activity` objects in `Create` automatically |
| 199 | + - Federate anything unless your listener calls `ctx.sendActivity()` or |
| 200 | + `ctx.forwardActivity()` |
| 201 | + |
| 202 | +If you need full `GET /outbox` support as well, combine this guide with the |
| 203 | +[*Collections*][collections-outbox] guide. |
0 commit comments