Unverified Commit dfa53a4b authored by Hong Minhee's avatar Hong Minhee
Browse files

Add immediate option to sendActivity()

parent a021cee9
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -33,6 +33,11 @@ To be released.
     -  `Context.sendActivity()` method
     -  `Federation.sendActivity()` method

 -  Added `immediate` option to `Context.sendActivity()` and
    `Federation.sendActivity()` methods.

 -  Added `SendActivityOptions` interface.


Version 0.2.0
-------------
+19 −1
Original line number Diff line number Diff line
@@ -95,7 +95,7 @@ export interface Context<TContextData> {
    sender: { keyId: URL; privateKey: CryptoKey } | { handle: string },
    recipients: Actor | Actor[],
    activity: Activity,
    options?: { preferSharedInbox?: boolean },
    options?: SendActivityOptions,
  ): Promise<void>;
}

@@ -113,3 +113,21 @@ export interface RequestContext<TContextData> extends Context<TContextData> {
   */
  readonly url: URL;
}

/**
 * Options for {@link Context.sendActivity} method and
 * {@link Federation.sendActivity} method.
 */
export interface SendActivityOptions {
  /**
   * Whether to prefer the shared inbox for the recipients.
   */
  preferSharedInbox?: boolean;

  /**
   * Whether to send the activity immediately, without enqueuing it.
   * If `true`, the activity will be sent immediately and the retrial
   * policy will not be applied.
   */
  immediate?: boolean;
}
+19 −3
Original line number Diff line number Diff line
@@ -19,7 +19,7 @@ import {
  InboxListener,
  NodeInfoDispatcher,
} from "./callback.ts";
import { Context, RequestContext } from "./context.ts";
import { Context, RequestContext, SendActivityOptions } from "./context.ts";
import {
  CollectionCallbacks,
  handleActor,
@@ -282,7 +282,7 @@ export class Federation<TContextData> {
        sender: { keyId: URL; privateKey: CryptoKey } | { handle: string },
        recipients: Actor | Actor[],
        activity: Activity,
        options: { preferSharedInbox?: boolean } = {},
        options: SendActivityOptions = {},
      ): Promise<void> => {
        let senderPair: { keyId: URL; privateKey: CryptoKey };
        if ("handle" in sender) {
@@ -630,7 +630,7 @@ export class Federation<TContextData> {
    { keyId, privateKey }: { keyId: URL; privateKey: CryptoKey },
    recipients: Actor | Actor[],
    activity: Activity,
    { preferSharedInbox }: { preferSharedInbox?: boolean } = {},
    { preferSharedInbox, immediate }: SendActivityOptions = {},
  ): Promise<void> {
    if (activity.id == null) {
      activity = activity.clone({
@@ -642,6 +642,22 @@ export class Federation<TContextData> {
      recipients: Array.isArray(recipients) ? recipients : [recipients],
      preferSharedInbox,
    });
    if (immediate) {
      const promises: Promise<void>[] = [];
      for (const inbox of inboxes) {
        promises.push(
          sendActivity({
            keyId,
            privateKey,
            activity,
            inbox,
            documentLoader: this.#documentLoader,
          }),
        );
      }
      await Promise.all(promises);
      return;
    }
    for (const inbox of inboxes) {
      const message: OutboxMessage = {
        type: "outbox",