From c67ff442071175f20d704a14e06f8cbaf535d9ef Mon Sep 17 00:00:00 2001 From: Johann150 Date: Sat, 16 Dec 2023 09:59:51 +0100 Subject: [PATCH] make webfinger server stuff more readable --- packages/backend/src/server/well-known.ts | 45 +++++++++++++---------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/packages/backend/src/server/well-known.ts b/packages/backend/src/server/well-known.ts index fea4c4ad8..80e89419d 100644 --- a/packages/backend/src/server/well-known.ts +++ b/packages/backend/src/server/well-known.ts @@ -80,26 +80,33 @@ router.get('/.well-known/oauth-authorization-server', oauth); router.get('/.well-known/openid-configuration', oauth); router.get(webFingerPath, async ctx => { - const fromId = (id: User['id']): FindOptionsWhere => ({ - id, - host: IsNull(), - isSuspended: false, - }); + const fromAcct = (acct_str: string): FindOptionsWhere | number => { + const acct = Acct.parse(acct_str); + if (!acct.host || acct.host === config.host.toLowerCase()) { + return { + usernameLower: acct.username, + host: IsNull(), + isSuspended: false, + }; + } else { + return 422; + } + }; - const generateQuery = (resource: string): FindOptionsWhere | number => - resource.startsWith(`${config.url.toLowerCase()}/users/`) ? - fromId(resource.split('/').pop()!) : - fromAcct(Acct.parse( - resource.startsWith(`${config.url.toLowerCase()}/@`) ? resource.split('/').pop()! : - resource.startsWith('acct:') ? resource.slice('acct:'.length) : - resource)); - - const fromAcct = (acct: Acct.Acct): FindOptionsWhere | number => - !acct.host || acct.host === config.host.toLowerCase() ? { - usernameLower: acct.username, - host: IsNull(), - isSuspended: false, - } : 422; + const generateQuery = (resource: string): FindOptionsWhere | number => { + if (resource.startsWith(`${config.url.toLowerCase()}/users/`)) { + return { + id: resource.split('/').pop()!, + host: IsNull(), + isSuspended: false, + }; + } else if (resource.startsWith(`${config.url.toLowerCase()}/@`)) { + return fromAcct(resource.split('/').pop()!); + } else if (resource.startsWith("acct:")) { + return fromAcct(resource.slice('acct:'.length)); + } + return fromAcct(resource); + }; let resource = ctx.query.resource;