Unverified Commit 21ad740a authored by Hong Minhee's avatar Hong Minhee
Browse files

Throw TypeError if activity to send has no actor

parent 70069598
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -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
+12 −1
Original line number Diff line number Diff line
@@ -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"));
+6 −0
Original line number Diff line number Diff line
@@ -674,6 +674,7 @@ 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 },
@@ -681,6 +682,11 @@ export class Federation<TContextData> {
    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()}`),