From b42a9e1c4e2191e97ccac0fe115ad7a46421df16 Mon Sep 17 00:00:00 2001 From: mei23 Date: Tue, 21 Aug 2018 13:48:03 +0900 Subject: [PATCH] Set ActivityPub Content-Type --- src/server/activitypub.ts | 13 +++++++++++++ src/server/activitypub/followers.ts | 7 +++++-- src/server/activitypub/following.ts | 7 +++++-- src/server/activitypub/outbox.ts | 7 +++++-- 4 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/server/activitypub.ts b/src/server/activitypub.ts index 26eaa68fe8..d04ffd38f9 100644 --- a/src/server/activitypub.ts +++ b/src/server/activitypub.ts @@ -41,10 +41,20 @@ function inbox(ctx: Router.IRouterContext) { } function isActivityPubReq(ctx: Router.IRouterContext) { + ctx.response.vary('Accept'); const accepted = ctx.accepts('html', 'application/activity+json', 'application/ld+json'); return ['application/activity+json', 'application/ld+json'].includes(accepted as string); } +export function setResponseType(ctx: Router.IRouterContext) { + const accpet = ctx.accepts('application/activity+json', 'application/ld+json'); + if (accpet === 'application/ld+json') { + ctx.response.type = 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"; charset=utf-8'; + } else { + ctx.response.type = 'application/activity+json; charset=utf-8'; + } +} + // inbox router.post('/inbox', json(), inbox); router.post('/users/:user/inbox', json(), inbox); @@ -64,6 +74,7 @@ router.get('/notes/:note', async (ctx, next) => { } ctx.body = pack(await renderNote(note, false)); + setResponseType(ctx); }); // outbox @@ -91,6 +102,7 @@ router.get('/users/:user/publickey', async ctx => { if (isLocalUser(user)) { ctx.body = pack(renderKey(user)); + setResponseType(ctx); } else { ctx.status = 400; } @@ -104,6 +116,7 @@ async function userInfo(ctx: Router.IRouterContext, user: IUser) { } ctx.body = pack(await renderPerson(user as ILocalUser)); + setResponseType(ctx); } router.get('/users/:user', async ctx => { diff --git a/src/server/activitypub/followers.ts b/src/server/activitypub/followers.ts index d51d45b1c7..eb58703443 100644 --- a/src/server/activitypub/followers.ts +++ b/src/server/activitypub/followers.ts @@ -1,5 +1,5 @@ import * as mongo from 'mongodb'; -import * as Koa from 'koa'; +import * as Router from 'koa-router'; import config from '../../config'; import $ from 'cafy'; import ID from '../../misc/cafy-id'; import User from '../../models/user'; @@ -8,8 +8,9 @@ import pack from '../../remote/activitypub/renderer'; import renderOrderedCollection from '../../remote/activitypub/renderer/ordered-collection'; import renderOrderedCollectionPage from '../../remote/activitypub/renderer/ordered-collection-page'; import renderFollowUser from '../../remote/activitypub/renderer/follow-user'; +import { setResponseType } from '../activitypub'; -export default async (ctx: Koa.Context) => { +export default async (ctx: Router.IRouterContext) => { const userId = new mongo.ObjectID(ctx.params.user); // Get 'cursor' parameter @@ -72,9 +73,11 @@ export default async (ctx: Koa.Context) => { ); ctx.body = pack(rendered); + setResponseType(ctx); } else { // index page const rendered = renderOrderedCollection(partOf, user.followersCount, `${partOf}?page=true`, null); ctx.body = pack(rendered); + setResponseType(ctx); } }; diff --git a/src/server/activitypub/following.ts b/src/server/activitypub/following.ts index 7e496f590d..80878fd4ca 100644 --- a/src/server/activitypub/following.ts +++ b/src/server/activitypub/following.ts @@ -1,5 +1,5 @@ import * as mongo from 'mongodb'; -import * as Koa from 'koa'; +import * as Router from 'koa-router'; import config from '../../config'; import $ from 'cafy'; import ID from '../../misc/cafy-id'; import User from '../../models/user'; @@ -8,8 +8,9 @@ import pack from '../../remote/activitypub/renderer'; import renderOrderedCollection from '../../remote/activitypub/renderer/ordered-collection'; import renderOrderedCollectionPage from '../../remote/activitypub/renderer/ordered-collection-page'; import renderFollowUser from '../../remote/activitypub/renderer/follow-user'; +import { setResponseType } from '../activitypub'; -export default async (ctx: Koa.Context) => { +export default async (ctx: Router.IRouterContext) => { const userId = new mongo.ObjectID(ctx.params.user); // Get 'cursor' parameter @@ -72,9 +73,11 @@ export default async (ctx: Koa.Context) => { ); ctx.body = pack(rendered); + setResponseType(ctx); } else { // index page const rendered = renderOrderedCollection(partOf, user.followingCount, `${partOf}?page=true`, null); ctx.body = pack(rendered); + setResponseType(ctx); } }; diff --git a/src/server/activitypub/outbox.ts b/src/server/activitypub/outbox.ts index 71d879e215..37df190880 100644 --- a/src/server/activitypub/outbox.ts +++ b/src/server/activitypub/outbox.ts @@ -1,16 +1,17 @@ import * as mongo from 'mongodb'; -import * as Koa from 'koa'; +import * as Router from 'koa-router'; import config from '../../config'; import $ from 'cafy'; import ID from '../../misc/cafy-id'; import User from '../../models/user'; import pack from '../../remote/activitypub/renderer'; import renderOrderedCollection from '../../remote/activitypub/renderer/ordered-collection'; import renderOrderedCollectionPage from '../../remote/activitypub/renderer/ordered-collection-page'; +import { setResponseType } from '../activitypub'; import Note from '../../models/note'; import renderNote from '../../remote/activitypub/renderer/note'; -export default async (ctx: Koa.Context) => { +export default async (ctx: Router.IRouterContext) => { const userId = new mongo.ObjectID(ctx.params.user); // Get 'sinceId' parameter @@ -92,6 +93,7 @@ export default async (ctx: Koa.Context) => { ); ctx.body = pack(rendered); + setResponseType(ctx); } else { // index page const rendered = renderOrderedCollection(partOf, user.notesCount, @@ -99,5 +101,6 @@ export default async (ctx: Koa.Context) => { `${partOf}?page=true&since_id=000000000000000000000000` ); ctx.body = pack(rendered); + setResponseType(ctx); } };