From a27a29b3719588fb14b17e3fd1dadd8e84681b19 Mon Sep 17 00:00:00 2001 From: Johann150 Date: Fri, 11 Nov 2022 17:54:11 +0100 Subject: [PATCH] server: redirect browsers to human readable page Also added/translated more comments. --- packages/backend/src/server/activitypub.ts | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/packages/backend/src/server/activitypub.ts b/packages/backend/src/server/activitypub.ts index f1a8f4914..6cc767496 100644 --- a/packages/backend/src/server/activitypub.ts +++ b/packages/backend/src/server/activitypub.ts @@ -23,8 +23,6 @@ import Featured from './activitypub/featured.js'; // Init router const router = new Router(); -//#region Routing - function inbox(ctx: Router.RouterContext) { let signature; @@ -45,6 +43,8 @@ const LD_JSON = 'application/ld+json; profile="https://www.w3.org/ns/activitystr function isActivityPubReq(ctx: Router.RouterContext) { ctx.response.vary('Accept'); + // if no accept header is supplied, koa returns the 1st, so html is used as a dummy + // i.e. activitypub requests must be explicit const accepted = ctx.accepts('html', ACTIVITY_JSON, LD_JSON); return typeof accepted === 'string' && !accepted.match(/html/); } @@ -77,7 +77,7 @@ router.get('/notes/:note', async (ctx, next) => { return; } - // リモートだったらリダイレクト + // redirect if remote if (note.userHost != null) { if (note.uri == null || isSelfHost(note.userHost)) { ctx.status = 500; @@ -94,6 +94,15 @@ router.get('/notes/:note', async (ctx, next) => { // note activity router.get('/notes/:note/activity', async ctx => { + if (!isActivityPubReq(ctx)) { + /* + Redirect to the human readable page. in this case using next is not possible, + since there is no human readable page explicitly for the activity. + */ + ctx.redirect(`/notes/${ctx.params.note}`); + return; + } + const note = await Notes.findOneBy({ id: ctx.params.note, userHost: IsNull(), @@ -185,7 +194,6 @@ router.get('/@:user', async (ctx, next) => { await userInfo(ctx, user); }); -//#endregion // emoji router.get('/emojis/:emoji', async ctx => {