Some fixes and refactors

This commit is contained in:
syuilo 2018-04-01 21:24:25 +09:00
parent 623f5b0072
commit 79f87b6943
7 changed files with 21 additions and 20 deletions

View file

@ -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);

View file

@ -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<Promise<IRemoteUserObject>[]> {
export default async function create(parentResolver: Resolver, actor, value): Promise<Array<Promise<IRemoteUserObject>>> {
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<Prom
return null;
}));
};
}

View file

@ -12,10 +12,10 @@ async function isCollection(collection) {
export default async (value, usernameLower, hostLower, acctLower) => {
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([

View file

@ -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<string>;
private requesting: Set<string>;
constructor(iterable?: Iterable<string>) {
this.requesting = new Set(iterable);
}
async resolve(value): Promise<Promise<IResult>[]> {
public async resolve(value): Promise<Array<Promise<IResult>>> {
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 => {

View file

@ -1,3 +1,3 @@
export type IObject = {
type: string;
}
};

View file

@ -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);

View file

@ -5,12 +5,12 @@ const webFinger = new WebFinger({});
type ILink = {
href: string;
rel: string;
}
};
type IWebFinger = {
links: Array<ILink>;
links: ILink[];
subject: string;
}
};
export default (query, verifier): Promise<IWebFinger> => new Promise((res, rej) => webFinger.lookup(query, (error, result) => {
if (error) {