From 292d3d3e7d7c5c93ba596e07c9d139395d0c25d1 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Sun, 20 Oct 2024 06:58:51 +0200 Subject: [PATCH] Setup follow handler and actor mapper --- src/dv/dotinoVeloce.ts | 59 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 54 insertions(+), 5 deletions(-) diff --git a/src/dv/dotinoVeloce.ts b/src/dv/dotinoVeloce.ts index f6437a7..0f1acd9 100644 --- a/src/dv/dotinoVeloce.ts +++ b/src/dv/dotinoVeloce.ts @@ -1,5 +1,5 @@ // deno-lint-ignore-file require-await -import { createFederation, Person, Application, Image, PropertyValue, Organization, Federation, KvStore, Context, Actor } from "@fedify/fedify" +import { createFederation, Person, Application, Image, PropertyValue, Organization, Federation, KvStore, Context, Actor, Follow } from "@fedify/fedify" import { getLogger } from "https://jsr.io/@logtape/logtape/0.6.3/logtape/logger.ts" import { escapeHtml } from "@@x/escape" import { StratzAPI } from "../stratz/api.ts" @@ -17,9 +17,16 @@ export class DotinoVeloce { constructor(kv: KvStore, stratz: StratzAPI) { this.stratz = stratz + this.federation = createFederation({ kv }) - this.federation.setInboxListeners("/inbox/{identifier}", /* We don't need shared inboxes here. */) - this.federation.setActorDispatcher("/users/{identifier}", this.#actorHandler) + + this.federation + .setActorDispatcher("/users/{identifier}", this.#actorHandler.bind(this)) + .mapHandle(this.#actorMapper.bind(this)) + + this.federation + .setInboxListeners("/inbox/{identifier}", "/inbox") + .on(Follow, this.#followHandler.bind(this)) } #commonActorProperties(ctx: Context, handle: string): Partial { @@ -187,14 +194,56 @@ export class DotinoVeloce { } async #actorHandler(ctx: Context, handle: string) { - l.debug`Handling actor with handle: ${handle}` + l.info`Handling actor with handle: ${handle}` let actor = null + actor ??= this.#serviceActor(ctx, handle) actor ??= this.#playerActor(ctx, handle) actor ??= this.#guildActor(ctx, handle) - actor ??= this.#serviceActor(ctx, handle) return actor } + + async #actorMapper(_ctx: Context, handle: string) { + return handle + } + + async #followHandler(ctx: Context, follow: Follow) { + l.info`Handling follow request: ${follow}` + + if(!follow.id) { + l.warn`Missing follow ID, skipping.` + return + } + if(!follow.actorId) { + l.warn`Missing actor ID, skipping.` + return + } + if(!follow.objectId) { + l.warn`Missing object ID, skipping.` + return + } + + l.debug`Attempting to determine object of the follow request...` // TODO: ??? + const object = ctx.parseUri(follow.objectId) + l.debug`Object is: ${object}` + + if(!object) { + l.warn`Failed to determine object, skipping.` + return + } + if(object.type !== "actor") { + l.warn`Object type is not actor, skipping.` // TODO: Why? + return + } + + l.debug`Attempting to determine actor of the follow request...` + const actor = await follow.getActor(ctx) + l.debug`Actor is: ${actor}` + + l.debug`Attempting to determine target of the follow request...` + const target = await follow.getTarget(ctx) + l.debug`Target is: ${target}` + } }