import Router from "@koa/router" ; import { readAsset } from "../util/fs" ; import { AS_CONTEXT , AS_MIME , KOA_JSON_ACCEPTS , SEC_CONTEXT , } from "../util/consts" ; export type ActorCtrl = void ; export default async ( { actorUrl , domain , origin , publicKeyPem , publicKeyUrl , router , } ) : Promise < ActorCtrl > => { const kingIcon = await readAsset ( "img/bk.png" ) ; const accountUrl = `acct:king@${ domain }` ; const validQueries = new Set ( [ "king" , `king@${ domain }` , "acct:king" , accountUrl , ] ) ; router . get ( "/.well-known/webfinger" , ( ctx ) => { const { resource } = ctx . query ; if ( typeof resource === "string" && validQueries . has ( resource ) ) { ctx . body = { subject : accountUrl , links : [ { rel : "self" , type : AS_MIME , href : actorUrl , } , ] , } ; ctx . type = "application/jrd+json" ; } else { ctx . status = 404 ; } } ) ; router . get ( "/@king" , ( ctx ) => { const actor = { "@context" : [ AS_CONTEXT , SEC_CONTEXT ] , id : actorUrl , type : "Service" , name : "King" , summary : `<p>I'm a bot, hosting games of chess!</p>` , preferredUsername : "king" , inbox : `${ origin }/inbox` , icon : { type : "Image" , mediaType : "image/png" , url : `${ actorUrl }/icon` , } , attachment : [ { type : "PropertyValue" , name : "Website" , value : `<a href="${ origin }/">${ domain }</a>` , } , ] , publicKey : { id : publicKeyUrl , owner : actorUrl , publicKeyPem , } , } ; ctx . response . vary ( "accept" ) ; if ( ctx . accepts ( "html" ) ) { ctx . redirect ( "/" ) ; } else if ( ctx . accepts ( KOA_JSON_ACCEPTS ) ) { ctx . body = actor ; ctx . type = AS_MIME ; } else { ctx . status = 406 ; } } ) ; router . get ( "/@king/icon" , ( ctx ) => { ctx . body = kingIcon ; ctx . type = "png" ; } ) ; } ;