From 555699635f5604e91b006f90d3a2d1441d773ecf Mon Sep 17 00:00:00 2001 From: Chloe Kudryavtsev Date: Wed, 17 Aug 2022 19:47:09 -0400 Subject: [PATCH] chore: fix backend activitypub.ts lints and logic lints: mostly function signatures logic: remote user logic is incorrect and overly lengthy, public key checks are leaky TODO: default export lint --- packages/backend/src/server/activitypub.ts | 31 ++++++++-------------- 1 file changed, 11 insertions(+), 20 deletions(-) diff --git a/packages/backend/src/server/activitypub.ts b/packages/backend/src/server/activitypub.ts index 36bc4caf2..3f6ec3a36 100644 --- a/packages/backend/src/server/activitypub.ts +++ b/packages/backend/src/server/activitypub.ts @@ -25,7 +25,7 @@ const router = new Router(); //#region Routing -function inbox(ctx: Router.RouterContext) { +function inbox(ctx: Router.RouterContext): void { let signature; try { @@ -43,13 +43,13 @@ function inbox(ctx: Router.RouterContext) { const ACTIVITY_JSON = 'application/activity+json; charset=utf-8'; const LD_JSON = 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"; charset=utf-8'; -function isActivityPubReq(ctx: Router.RouterContext) { +function isActivityPubReq(ctx: Router.RouterContext): boolean { ctx.response.vary('Accept'); const accepted = ctx.accepts('html', ACTIVITY_JSON, LD_JSON); return typeof accepted === 'string' && !accepted.match(/html/); } -export function setResponseType(ctx: Router.RouterContext) { +export function setResponseType(ctx: Router.RouterContext): void { const accept = ctx.accepts(ACTIVITY_JSON, LD_JSON); if (accept === LD_JSON) { ctx.response.type = LD_JSON; @@ -77,13 +77,9 @@ router.get('/notes/:note', async (ctx, next) => { return; } - // リモートだったらリダイレクト - if (note.userHost != null) { - if (note.uri == null || isSelfHost(note.userHost)) { - ctx.status = 500; - return; - } - ctx.redirect(note.uri); + if (!isSelfHost(note.userHost)) { + if (note.uri != null) ctx.redirect(note.uri); + else ctx.status = 500; return; } @@ -132,24 +128,19 @@ router.get('/users/:user/publickey', async ctx => { host: IsNull(), }); - if (user == null) { + if (user == null || !Users.isLocalUser(user)) { ctx.status = 404; return; } const keypair = await getUserKeypair(user.id); - - if (Users.isLocalUser(user)) { - ctx.body = renderActivity(renderKey(user, keypair)); - ctx.set('Cache-Control', 'public, max-age=180'); - setResponseType(ctx); - } else { - ctx.status = 400; - } + ctx.body = renderActivity(renderKey(user, keypair)); + ctx.set('Cache-Control', 'public, max-age=180'); + setResponseType(ctx); }); // user -async function userInfo(ctx: Router.RouterContext, user: User | null) { +async function userInfo(ctx: Router.RouterContext, user: User | null): void { if (user == null) { ctx.status = 404; return;