From 79f87b69433cf10746580cf2a8144375dafb7e37 Mon Sep 17 00:00:00 2001 From: syuilo Date: Sun, 1 Apr 2018 21:24:25 +0900 Subject: [PATCH] Some fixes and refactors --- src/common/remote/activitypub/act/create.ts | 2 +- src/common/remote/activitypub/create.ts | 11 ++++++----- src/common/remote/activitypub/resolve-person.ts | 6 +++--- src/common/remote/activitypub/resolver.ts | 12 ++++++------ src/common/remote/activitypub/type.ts | 2 +- src/common/remote/resolve-user.ts | 2 +- src/common/remote/webfinger.ts | 6 +++--- 7 files changed, 21 insertions(+), 20 deletions(-) diff --git a/src/common/remote/activitypub/act/create.ts b/src/common/remote/activitypub/act/create.ts index 6c62f7ab9e..9eb74800ea 100644 --- a/src/common/remote/activitypub/act/create.ts +++ b/src/common/remote/activitypub/act/create.ts @@ -2,7 +2,7 @@ import create from '../create'; export default (resolver, actor, activity) => { if ('actor' in activity && actor.account.uri !== activity.actor) { - throw new Error; + throw new Error(); } return create(resolver, actor, activity.object); diff --git a/src/common/remote/activitypub/create.ts b/src/common/remote/activitypub/create.ts index 4aaaeb3060..ea780f01ea 100644 --- a/src/common/remote/activitypub/create.ts +++ b/src/common/remote/activitypub/create.ts @@ -3,6 +3,7 @@ import config from '../../../conf'; import Post from '../../../models/post'; import RemoteUserObject, { IRemoteUserObject } from '../../../models/remote-user-object'; import uploadFromUrl from '../../drive/upload_from_url'; +import Resolver from './resolver'; const createDOMPurify = require('dompurify'); function createRemoteUserObject($ref, $id, { id }) { @@ -17,7 +18,7 @@ function createRemoteUserObject($ref, $id, { id }) { async function createImage(actor, object) { if ('attributedTo' in object && actor.account.uri !== object.attributedTo) { - throw new Error; + throw new Error(); } const { _id } = await uploadFromUrl(object.url, actor); @@ -26,7 +27,7 @@ async function createImage(actor, object) { async function createNote(resolver, actor, object) { if ('attributedTo' in object && actor.account.uri !== object.attributedTo) { - throw new Error; + throw new Error(); } const mediaIds = 'attachment' in object && @@ -69,10 +70,10 @@ async function createNote(resolver, actor, object) { return createRemoteUserObject('posts', _id, object); } -export default async function create(parentResolver, actor, value): Promise[]> { +export default async function create(parentResolver: Resolver, actor, value): Promise>> { const results = await parentResolver.resolveRemoteUserObjects(value); - return results.map(asyncResult => asyncResult.then(({ resolver, object }) => { + return results.map(promisedResult => promisedResult.then(({ resolver, object }) => { switch (object.type) { case 'Image': return createImage(actor, object); @@ -83,4 +84,4 @@ export default async function create(parentResolver, actor, value): Promise { if (!validateUsername(usernameLower)) { - throw new Error; + throw new Error(); } - const { resolver, object } = await (new Resolver).resolveOne(value); + const { resolver, object } = await new Resolver().resolveOne(value); if ( object === null || @@ -25,7 +25,7 @@ export default async (value, usernameLower, hostLower, acctLower) => { !isValidName(object.name) || !isValidDescription(object.summary) ) { - throw new Error; + throw new Error(); } const [followers, following, outbox, finger] = await Promise.all([ diff --git a/src/common/remote/activitypub/resolver.ts b/src/common/remote/activitypub/resolver.ts index 50ac1b0b19..43f0d63cb3 100644 --- a/src/common/remote/activitypub/resolver.ts +++ b/src/common/remote/activitypub/resolver.ts @@ -29,7 +29,7 @@ async function resolveUnrequestedOne(this: Resolver, value) { !object['@context'].includes('https://www.w3.org/ns/activitystreams') : object['@context'] !== 'https://www.w3.org/ns/activitystreams' )) { - throw new Error; + throw new Error(); } return { resolver, object }; @@ -57,13 +57,13 @@ async function resolveCollection(this: Resolver, value) { } export default class Resolver { - requesting: Set; + private requesting: Set; constructor(iterable?: Iterable) { this.requesting = new Set(iterable); } - async resolve(value): Promise[]> { + public async resolve(value): Promise>> { const collection = await resolveCollection.call(this, value); return collection @@ -71,15 +71,15 @@ export default class Resolver { .map(resolveUnrequestedOne.bind(this)); } - resolveOne(value) { + public resolveOne(value) { if (this.requesting.has(value)) { - throw new Error; + throw new Error(); } return resolveUnrequestedOne.call(this, value); } - async resolveRemoteUserObjects(value) { + public async resolveRemoteUserObjects(value) { const collection = await resolveCollection.call(this, value); return collection.filter(element => !this.requesting.has(element)).map(element => { diff --git a/src/common/remote/activitypub/type.ts b/src/common/remote/activitypub/type.ts index 5c4750e140..94e2c350a2 100644 --- a/src/common/remote/activitypub/type.ts +++ b/src/common/remote/activitypub/type.ts @@ -1,3 +1,3 @@ export type IObject = { type: string; -} +}; diff --git a/src/common/remote/resolve-user.ts b/src/common/remote/resolve-user.ts index 13d155830e..4959539da6 100644 --- a/src/common/remote/resolve-user.ts +++ b/src/common/remote/resolve-user.ts @@ -16,7 +16,7 @@ export default async (username, host, option) => { const finger = await webFinger(acctLower, acctLower); const self = finger.links.find(link => link.rel && link.rel.toLowerCase() === 'self'); if (!self) { - throw new Error; + throw new Error(); } user = await resolvePerson(self.href, usernameLower, hostLower, acctLower); diff --git a/src/common/remote/webfinger.ts b/src/common/remote/webfinger.ts index 23f0aaa55f..9f1b916c9f 100644 --- a/src/common/remote/webfinger.ts +++ b/src/common/remote/webfinger.ts @@ -5,12 +5,12 @@ const webFinger = new WebFinger({}); type ILink = { href: string; rel: string; -} +}; type IWebFinger = { - links: Array; + links: ILink[]; subject: string; -} +}; export default (query, verifier): Promise => new Promise((res, rej) => webFinger.lookup(query, (error, result) => { if (error) {