diff --git a/src/federation.ts b/src/federation.ts index 6b4c6d1..3eaa5ad 100644 --- a/src/federation.ts +++ b/src/federation.ts @@ -1,42 +1,75 @@ -import { createFederation, Follow, Service } from "@fedify/fedify" +import { createFederation, Service, Application, Image } from "@fedify/fedify" import { kv } from "./redis.ts" import { getLogger } from "https://jsr.io/@logtape/logtape/0.6.3/logtape/logger.ts" +import { doQueryPlayer } from "./graphql/stratz.ts" const l = getLogger(["dotino-veloce", "federation"]) l.debug`Creating federation object...` export const federation = createFederation({ kv }) +const userMatcher = /u([0-9]+)/ + l.debug`Creating actor dispatcher...` // deno-lint-ignore require-await async function actorDispatcher(ctx: any, handle: string) { - l.debug`Received request for actor ${handle}, handling...` + l.debug`Determining id of requested actor: ${handle}` + const id = ctx.getActorUri(handle) + l.debug`Requested actor is: ${id.href}` - if (handle !== "service") { - l.debug`No match found for ${handle}, returning null.` - return null + if (handle === "service") { + l.info`Dispatching service account...` + return new Application({ + id, + preferredUsername: id.href, + // Akkoma expects URL to be equal to ID + // https://akkoma.dev/AkkomaGang/akkoma/src/commit/f1018867097e6f293d8b2b5b6935f0a7ebf99bd0/lib/pleroma/object/fetcher.ex#L287 + url: id, + // Akkoma requires inboxes to be setup to display profiles + // https://akkoma.dev/AkkomaGang/akkoma/src/commit/f1018867097e6f293d8b2b5b6935f0a7ebf99bd0/lib/pleroma/web/activity_pub/object_validators/user_validator.ex#L72 + inbox: ctx.getInboxUri(handle), + + name: "[TEST] Dotino Veloce", + summary: "Service account of a Dotino Veloce instance.", + }) } - l.debug`Determining id of actor ${handle}...` - const id = ctx.getActorUri(handle) + const steamId = Number.parseInt(handle.match(userMatcher)?.[1] as any) + if(Number.isFinite(steamId)) { + l.info`Dispatching user account with Steam ID: ${steamId}` + + const player = await doQueryPlayer(steamId) + + if(player === null) { + l.warn`No Steam account was found with ID: ${steamId}` + return null + } + + return new Service({ + id, + preferredUsername: id.href, + // Akkoma expects URL to be equal to ID + // https://akkoma.dev/AkkomaGang/akkoma/src/commit/f1018867097e6f293d8b2b5b6935f0a7ebf99bd0/lib/pleroma/object/fetcher.ex#L287 + url: id, + // Akkoma requires inboxes to be setup to display profiles + // https://akkoma.dev/AkkomaGang/akkoma/src/commit/f1018867097e6f293d8b2b5b6935f0a7ebf99bd0/lib/pleroma/web/activity_pub/object_validators/user_validator.ex#L72 + inbox: ctx.getInboxUri(handle), + + name: `[TEST] ${player.name}`, + icon: new Image({ + url: new URL(player.avatar), + mediaType: "image/jpeg" + }) + }) + } + + l.warn`No dispatcher was found for handle: ${handle}` + return null - l.info`Returning actor: ${id.href}` - return new Service({ - id, - name: "[TEST] Dotino Veloce", - summary: "Core account of a Dotino Veloce instance.", - preferredUsername: id.href, - // Akkoma expects URL to be equal to ID - // https://akkoma.dev/AkkomaGang/akkoma/src/commit/f1018867097e6f293d8b2b5b6935f0a7ebf99bd0/lib/pleroma/object/fetcher.ex#L287 - url: id, - inbox: ctx.getInboxUri(handle), - }) } l.debug`Connecting actor dispatcher to federation object...` federation.setActorDispatcher("/users/{identifier}", actorDispatcher) -// Akkoma requires inboxes to be setup to display profiles -// https://akkoma.dev/AkkomaGang/akkoma/src/commit/f1018867097e6f293d8b2b5b6935f0a7ebf99bd0/lib/pleroma/web/activity_pub/object_validators/user_validator.ex#L72 l.debug`Initializing inbox listener...` federation.setInboxListeners("/inbox/{identifier}") // I don't really care about the shared inbox for this project