Skip to content

Commit

Permalink
Throw TypeError if activity to send has no actor
Browse files Browse the repository at this point in the history
  • Loading branch information
dahlia committed Apr 8, 2024
1 parent 7006959 commit 21ad740
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ To be released.
- Added `federation()` function.
- Added `ContextDataFactory` type.

- `Context.sendActivity()` method now throws `TypeError` instead of silently
failing when the given `Activity` object lacks the actor property.

[Hono]: https://hono.dev/
[#25]: https://github.com/dahlia/fedify/issues/25
[#27]: https://github.com/dahlia/fedify/issues/27
Expand Down
13 changes: 12 additions & 1 deletion federation/middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,18 @@ Deno.test("Federation.createContext()", async (t) => {
documentUrl: "https://example.com/object",
document: true,
});
await ctx.sendActivity({ handle: "handle" }, [], new Create({}));
assertRejects(
() => ctx.sendActivity({ handle: "handle" }, [], new Create({})),
TypeError,
"The activity to send must have at least one actor property.",
);
await ctx.sendActivity(
{ handle: "handle" },
[],
new Create({
actor: new URL("https://example.com/users/handle"),
}),
);

federation.setInboxListeners("/users/{handle}/inbox", "/inbox");
assertEquals(ctx.getInboxUri(), new URL("https://example.com/inbox"));
Expand Down
6 changes: 6 additions & 0 deletions federation/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -674,13 +674,19 @@ export class Federation<TContextData> {
* @param recipients The recipients of the activity.
* @param activity The activity to send.
* @param options Options for sending the activity.
* @throws {TypeError} If the activity to send does not have an actor.
*/
async sendActivity(
{ keyId, privateKey }: { keyId: URL; privateKey: CryptoKey },
recipients: Actor | Actor[],
activity: Activity,
{ preferSharedInbox, immediate }: SendActivityOptions = {},
): Promise<void> {
if (activity.actorId == null) {
throw new TypeError(
"The activity to send must have at least one actor property.",
);
}
if (activity.id == null) {
activity = activity.clone({
id: new URL(`urn:uuid:${crypto.randomUUID()}`),
Expand Down

0 comments on commit 21ad740

Please sign in to comment.