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

Send Delete activities for terminated fedify inbox command

parent e51541a8
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -28,12 +28,17 @@ To be released.
     -  Added `attachSignature()` function.
     -  Added `detachSignature()` function.

 -  The `fedify inbox` command now sends `Delete(Application)` activities when
    it's terminated so that the peers can clean up data related to the temporary
    actor.  [[#135]]

 -  Added more log messages using the [LogTape] library.  Currently the below
    logger categories are used:

     -  `["fedify", "sig", "ld"]`

[Linked Data Signatures]: https://web.archive.org/web/20170923124140/https://w3c-dvcg.github.io/ld-signatures/
[#135]: https://github.com/dahlia/fedify/issues/135


Version 0.15.1
+39 −5
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@ import {
  Application,
  type Context,
  createFederation,
  Delete,
  Endpoints,
  Follow,
  generateCryptoKeyPair,
@@ -16,6 +17,7 @@ import {
  isActor,
  lookupObject,
  MemoryKvStore,
  PUBLIC_COLLECTION,
  type Recipient,
} from "@fedify/fedify";
import { getLogger } from "@logtape/logtape";
@@ -28,7 +30,7 @@ import type { ActivityEntry } from "./inbox/entry.ts";
import { ActivityEntryPage, ActivityListPage } from "./inbox/view.tsx";
import { recordingSink } from "./log.ts";
import { tableStyle } from "./table.ts";
import { spawnTemporaryServer } from "./tempserver.ts";
import { spawnTemporaryServer, type TemporaryServer } from "./tempserver.ts";

const logger = getLogger(["fedify", "cli", "inbox"]);

@@ -73,12 +75,20 @@ export const command = new Command()
    );
    Deno.addSignalListener("SIGINT", () => {
      spinner.stop();
      spinner.start("Stopping server...");
      const peersCnt = Object.keys(peers).length;
      spinner.start(
        `Sending Delete(Application) activities to the ${peersCnt} ${
          peersCnt === 1 ? "peer" : "peers"
        }...`,
      );
      sendDeleteToPeers(server).then(() => {
        spinner.text = "Stopping server...";
        server.close().then(() => {
          spinner.succeed("Server stopped.");
          Deno.exit(0);
        });
      });
    });
    spinner.start();
    const fedCtx = federation.createContext(server.url, -1);
    if (options.acceptFollow != null && options.acceptFollow.length > 0) {
@@ -95,6 +105,7 @@ export const command = new Command()
          spinner.start();
          continue;
        }
        if (actor.id != null) peers[actor.id?.href] = actor;
        await fedCtx.sendActivity(
          { handle: "i" },
          actor,
@@ -178,6 +189,23 @@ async function acceptsFollowFrom(actor: Actor): Promise<boolean> {
  return false;
}

const peers: Record<string, Actor> = {};

async function sendDeleteToPeers(server: TemporaryServer): Promise<void> {
  const ctx = federation.createContext(server.url, -1);
  const actorId = ctx.getActorUri("i");
  await ctx.sendActivity(
    { handle: "i" },
    Object.values(peers),
    new Delete({
      id: new URL(`#delete`, actorId),
      actor: actorId,
      to: PUBLIC_COLLECTION,
      object: actorId,
    }),
  );
}

const followers: Record<string, Actor> = {};

federation
@@ -185,6 +213,12 @@ federation
  .setSharedKeyDispatcher((_) => ({ handle: "i" }))
  .on(Activity, async (ctx, activity) => {
    activities[ctx.data].activity = activity;
    for await (const actor of activity.getActors()) {
      if (actor.id != null) peers[actor.id.href] = actor;
    }
    for await (const actor of activity.getAttributions()) {
      if (actor.id != null) peers[actor.id.href] = actor;
    }
    if (activity instanceof Follow) {
      if (acceptFollows.length < 1) return;
      const objectId = activity.objectId;