Unverified Commit 89074a8d authored by Hong Minhee's avatar Hong Minhee
Browse files

Let `autoIdAssigner` generate an http/https URI

parent 7b586abe
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -23,6 +23,10 @@ To be released.
        `DocumentLoaderFactoryOptions` interface.
     -  Added a type parameter `TContextData` to `CreateFederationOptions`
        interface.
     -  Fedify now assigns a random-generated *http:*/*https:* URI to
        activities if these do not have explicit `id` properties.  This behavior
        can be disabled by excluding `autoIdAssigner()` from
        the `CreateFederationOptions.activityTransformers` option.

 -  Introduced `ActivityTransformer`s for adjusting outgoing activities
    before sending them so that some ActivityPub implementations with quirks
+13 −3
Original line number Diff line number Diff line
import { assert } from "@std/assert/assert";
import { assertEquals } from "@std/assert/assert-equals";
import { assertNotEquals } from "@std/assert/assert-not-equals";
import { assertInstanceOf } from "@std/assert/assert-instance-of";
import { MemoryKvStore } from "../federation/kv.ts";
import { FederationImpl } from "../federation/middleware.ts";
import { test } from "../testing/mod.ts";
@@ -21,11 +22,20 @@ test("autoIdAssigner", async () => {
    }),
  });
  const result = autoIdAssigner(activity, context);
  assertNotEquals(result.id, null);
  const { id } = result;
  assertInstanceOf(id, URL);
  assertEquals(id.origin, "http://example.com");
  assertEquals(id.pathname, "/");
  assertEquals(id.search, "");
  assert(
    id.hash.match(
      /^#Follow\/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/,
    ),
  );
  assertEquals(
    await result.toJsonLd(),
    await new Follow({
      id: result.id,
      id,
      actor: new URL("http://example.com/actors/1"),
      object: new Person({
        id: new URL("http://example.com/actors/2"),
+9 −4
Original line number Diff line number Diff line
@@ -10,11 +10,13 @@ const logger = getLogger(["fedify", "compat", "transformers"]);
 * does not already have one.  This is useful for ensuring that activities
 * have an ID before they are sent to other servers.
 *
 * The generated ID is a URN UUID like:
 * The generated ID is an origin URI with a fragment which contains an activity
 * type name with a random UUID:
 *
 * ```
 * urn:uuid:12345678-1234-5678-1234-567812345678
 * https://example.com/#Follow/12345678-1234-5678-1234-567812345678
 * ```
 *
 * @typeParam TContextData The type of the context data.
 * @param activity The activity to assign an ID to.
 * @param context The context of the activity.
@@ -23,10 +25,13 @@ const logger = getLogger(["fedify", "compat", "transformers"]);
 */
export function autoIdAssigner<TContextData>(
  activity: Activity,
  _context: Context<TContextData>,
  context: Context<TContextData>,
): Activity {
  if (activity.id != null) return activity;
  const id = new URL(`urn:uuid:${crypto.randomUUID()}`);
  const id = new URL(
    `/#${activity.constructor.name}/${crypto.randomUUID()}`,
    context.origin,
  );
  logger.warn(
    "As the activity to send does not have an id, a new id {id} has " +
      "been generated for it.  However, it is recommended to explicitly " +