Compare commits

...

1 commit

Author SHA1 Message Date
555699635f 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
2022-08-17 19:47:09 -04:00

View file

@ -25,7 +25,7 @@ const router = new Router();
//#region Routing //#region Routing
function inbox(ctx: Router.RouterContext) { function inbox(ctx: Router.RouterContext): void {
let signature; let signature;
try { try {
@ -43,13 +43,13 @@ function inbox(ctx: Router.RouterContext) {
const ACTIVITY_JSON = 'application/activity+json; charset=utf-8'; 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'; 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'); ctx.response.vary('Accept');
const accepted = ctx.accepts('html', ACTIVITY_JSON, LD_JSON); const accepted = ctx.accepts('html', ACTIVITY_JSON, LD_JSON);
return typeof accepted === 'string' && !accepted.match(/html/); 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); const accept = ctx.accepts(ACTIVITY_JSON, LD_JSON);
if (accept === LD_JSON) { if (accept === LD_JSON) {
ctx.response.type = LD_JSON; ctx.response.type = LD_JSON;
@ -77,13 +77,9 @@ router.get('/notes/:note', async (ctx, next) => {
return; return;
} }
// リモートだったらリダイレクト if (!isSelfHost(note.userHost)) {
if (note.userHost != null) { if (note.uri != null) ctx.redirect(note.uri);
if (note.uri == null || isSelfHost(note.userHost)) { else ctx.status = 500;
ctx.status = 500;
return;
}
ctx.redirect(note.uri);
return; return;
} }
@ -132,24 +128,19 @@ router.get('/users/:user/publickey', async ctx => {
host: IsNull(), host: IsNull(),
}); });
if (user == null) { if (user == null || !Users.isLocalUser(user)) {
ctx.status = 404; ctx.status = 404;
return; return;
} }
const keypair = await getUserKeypair(user.id); const keypair = await getUserKeypair(user.id);
ctx.body = renderActivity(renderKey(user, keypair));
if (Users.isLocalUser(user)) { ctx.set('Cache-Control', 'public, max-age=180');
ctx.body = renderActivity(renderKey(user, keypair)); setResponseType(ctx);
ctx.set('Cache-Control', 'public, max-age=180');
setResponseType(ctx);
} else {
ctx.status = 400;
}
}); });
// user // user
async function userInfo(ctx: Router.RouterContext, user: User | null) { async function userInfo(ctx: Router.RouterContext, user: User | null): void {
if (user == null) { if (user == null) {
ctx.status = 404; ctx.status = 404;
return; return;