Set ActivityPub Content-Type

This commit is contained in:
mei23 2018-08-21 13:48:03 +09:00
parent 4495525705
commit b42a9e1c4e
4 changed files with 28 additions and 6 deletions

View File

@ -41,10 +41,20 @@ function inbox(ctx: Router.IRouterContext) {
} }
function isActivityPubReq(ctx: Router.IRouterContext) { function isActivityPubReq(ctx: Router.IRouterContext) {
ctx.response.vary('Accept');
const accepted = ctx.accepts('html', 'application/activity+json', 'application/ld+json'); const accepted = ctx.accepts('html', 'application/activity+json', 'application/ld+json');
return ['application/activity+json', 'application/ld+json'].includes(accepted as string); 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 // inbox
router.post('/inbox', json(), inbox); router.post('/inbox', json(), inbox);
router.post('/users/:user/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)); ctx.body = pack(await renderNote(note, false));
setResponseType(ctx);
}); });
// outbox // outbox
@ -91,6 +102,7 @@ router.get('/users/:user/publickey', async ctx => {
if (isLocalUser(user)) { if (isLocalUser(user)) {
ctx.body = pack(renderKey(user)); ctx.body = pack(renderKey(user));
setResponseType(ctx);
} else { } else {
ctx.status = 400; ctx.status = 400;
} }
@ -104,6 +116,7 @@ async function userInfo(ctx: Router.IRouterContext, user: IUser) {
} }
ctx.body = pack(await renderPerson(user as ILocalUser)); ctx.body = pack(await renderPerson(user as ILocalUser));
setResponseType(ctx);
} }
router.get('/users/:user', async ctx => { router.get('/users/:user', async ctx => {

View File

@ -1,5 +1,5 @@
import * as mongo from 'mongodb'; import * as mongo from 'mongodb';
import * as Koa from 'koa'; import * as Router from 'koa-router';
import config from '../../config'; import config from '../../config';
import $ from 'cafy'; import ID from '../../misc/cafy-id'; import $ from 'cafy'; import ID from '../../misc/cafy-id';
import User from '../../models/user'; import User from '../../models/user';
@ -8,8 +8,9 @@ import pack from '../../remote/activitypub/renderer';
import renderOrderedCollection from '../../remote/activitypub/renderer/ordered-collection'; import renderOrderedCollection from '../../remote/activitypub/renderer/ordered-collection';
import renderOrderedCollectionPage from '../../remote/activitypub/renderer/ordered-collection-page'; import renderOrderedCollectionPage from '../../remote/activitypub/renderer/ordered-collection-page';
import renderFollowUser from '../../remote/activitypub/renderer/follow-user'; 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); const userId = new mongo.ObjectID(ctx.params.user);
// Get 'cursor' parameter // Get 'cursor' parameter
@ -72,9 +73,11 @@ export default async (ctx: Koa.Context) => {
); );
ctx.body = pack(rendered); ctx.body = pack(rendered);
setResponseType(ctx);
} else { } else {
// index page // index page
const rendered = renderOrderedCollection(partOf, user.followersCount, `${partOf}?page=true`, null); const rendered = renderOrderedCollection(partOf, user.followersCount, `${partOf}?page=true`, null);
ctx.body = pack(rendered); ctx.body = pack(rendered);
setResponseType(ctx);
} }
}; };

View File

@ -1,5 +1,5 @@
import * as mongo from 'mongodb'; import * as mongo from 'mongodb';
import * as Koa from 'koa'; import * as Router from 'koa-router';
import config from '../../config'; import config from '../../config';
import $ from 'cafy'; import ID from '../../misc/cafy-id'; import $ from 'cafy'; import ID from '../../misc/cafy-id';
import User from '../../models/user'; import User from '../../models/user';
@ -8,8 +8,9 @@ import pack from '../../remote/activitypub/renderer';
import renderOrderedCollection from '../../remote/activitypub/renderer/ordered-collection'; import renderOrderedCollection from '../../remote/activitypub/renderer/ordered-collection';
import renderOrderedCollectionPage from '../../remote/activitypub/renderer/ordered-collection-page'; import renderOrderedCollectionPage from '../../remote/activitypub/renderer/ordered-collection-page';
import renderFollowUser from '../../remote/activitypub/renderer/follow-user'; 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); const userId = new mongo.ObjectID(ctx.params.user);
// Get 'cursor' parameter // Get 'cursor' parameter
@ -72,9 +73,11 @@ export default async (ctx: Koa.Context) => {
); );
ctx.body = pack(rendered); ctx.body = pack(rendered);
setResponseType(ctx);
} else { } else {
// index page // index page
const rendered = renderOrderedCollection(partOf, user.followingCount, `${partOf}?page=true`, null); const rendered = renderOrderedCollection(partOf, user.followingCount, `${partOf}?page=true`, null);
ctx.body = pack(rendered); ctx.body = pack(rendered);
setResponseType(ctx);
} }
}; };

View File

@ -1,16 +1,17 @@
import * as mongo from 'mongodb'; import * as mongo from 'mongodb';
import * as Koa from 'koa'; import * as Router from 'koa-router';
import config from '../../config'; import config from '../../config';
import $ from 'cafy'; import ID from '../../misc/cafy-id'; import $ from 'cafy'; import ID from '../../misc/cafy-id';
import User from '../../models/user'; import User from '../../models/user';
import pack from '../../remote/activitypub/renderer'; import pack from '../../remote/activitypub/renderer';
import renderOrderedCollection from '../../remote/activitypub/renderer/ordered-collection'; import renderOrderedCollection from '../../remote/activitypub/renderer/ordered-collection';
import renderOrderedCollectionPage from '../../remote/activitypub/renderer/ordered-collection-page'; import renderOrderedCollectionPage from '../../remote/activitypub/renderer/ordered-collection-page';
import { setResponseType } from '../activitypub';
import Note from '../../models/note'; import Note from '../../models/note';
import renderNote from '../../remote/activitypub/renderer/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); const userId = new mongo.ObjectID(ctx.params.user);
// Get 'sinceId' parameter // Get 'sinceId' parameter
@ -92,6 +93,7 @@ export default async (ctx: Koa.Context) => {
); );
ctx.body = pack(rendered); ctx.body = pack(rendered);
setResponseType(ctx);
} else { } else {
// index page // index page
const rendered = renderOrderedCollection(partOf, user.notesCount, const rendered = renderOrderedCollection(partOf, user.notesCount,
@ -99,5 +101,6 @@ export default async (ctx: Koa.Context) => {
`${partOf}?page=true&since_id=000000000000000000000000` `${partOf}?page=true&since_id=000000000000000000000000`
); );
ctx.body = pack(rendered); ctx.body = pack(rendered);
setResponseType(ctx);
} }
}; };