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) => { export default (resolver, actor, activity) => {
if ('actor' in activity && actor.account.uri !== activity.actor) { if ('actor' in activity && actor.account.uri !== activity.actor) {
throw new Error; throw new Error();
} }
return create(resolver, actor, activity.object); return create(resolver, actor, activity.object);

View file

@ -3,6 +3,7 @@ import config from '../../../conf';
import Post from '../../../models/post'; import Post from '../../../models/post';
import RemoteUserObject, { IRemoteUserObject } from '../../../models/remote-user-object'; import RemoteUserObject, { IRemoteUserObject } from '../../../models/remote-user-object';
import uploadFromUrl from '../../drive/upload_from_url'; import uploadFromUrl from '../../drive/upload_from_url';
import Resolver from './resolver';
const createDOMPurify = require('dompurify'); const createDOMPurify = require('dompurify');
function createRemoteUserObject($ref, $id, { id }) { function createRemoteUserObject($ref, $id, { id }) {
@ -17,7 +18,7 @@ function createRemoteUserObject($ref, $id, { id }) {
async function createImage(actor, object) { async function createImage(actor, object) {
if ('attributedTo' in object && actor.account.uri !== object.attributedTo) { if ('attributedTo' in object && actor.account.uri !== object.attributedTo) {
throw new Error; throw new Error();
} }
const { _id } = await uploadFromUrl(object.url, actor); const { _id } = await uploadFromUrl(object.url, actor);
@ -26,7 +27,7 @@ async function createImage(actor, object) {
async function createNote(resolver, actor, object) { async function createNote(resolver, actor, object) {
if ('attributedTo' in object && actor.account.uri !== object.attributedTo) { if ('attributedTo' in object && actor.account.uri !== object.attributedTo) {
throw new Error; throw new Error();
} }
const mediaIds = 'attachment' in object && const mediaIds = 'attachment' in object &&
@ -69,10 +70,10 @@ async function createNote(resolver, actor, object) {
return createRemoteUserObject('posts', _id, 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); 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) { switch (object.type) {
case 'Image': case 'Image':
return createImage(actor, object); return createImage(actor, object);
@ -83,4 +84,4 @@ export default async function create(parentResolver, actor, value): Promise<Prom
return null; return null;
})); }));
}; }

View file

@ -12,10 +12,10 @@ async function isCollection(collection) {
export default async (value, usernameLower, hostLower, acctLower) => { export default async (value, usernameLower, hostLower, acctLower) => {
if (!validateUsername(usernameLower)) { 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 ( if (
object === null || object === null ||
@ -25,7 +25,7 @@ export default async (value, usernameLower, hostLower, acctLower) => {
!isValidName(object.name) || !isValidName(object.name) ||
!isValidDescription(object.summary) !isValidDescription(object.summary)
) { ) {
throw new Error; throw new Error();
} }
const [followers, following, outbox, finger] = await Promise.all([ 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'].includes('https://www.w3.org/ns/activitystreams') :
object['@context'] !== 'https://www.w3.org/ns/activitystreams' object['@context'] !== 'https://www.w3.org/ns/activitystreams'
)) { )) {
throw new Error; throw new Error();
} }
return { resolver, object }; return { resolver, object };
@ -57,13 +57,13 @@ async function resolveCollection(this: Resolver, value) {
} }
export default class Resolver { export default class Resolver {
requesting: Set<string>; private requesting: Set<string>;
constructor(iterable?: Iterable<string>) { constructor(iterable?: Iterable<string>) {
this.requesting = new Set(iterable); 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); const collection = await resolveCollection.call(this, value);
return collection return collection
@ -71,15 +71,15 @@ export default class Resolver {
.map(resolveUnrequestedOne.bind(this)); .map(resolveUnrequestedOne.bind(this));
} }
resolveOne(value) { public resolveOne(value) {
if (this.requesting.has(value)) { if (this.requesting.has(value)) {
throw new Error; throw new Error();
} }
return resolveUnrequestedOne.call(this, value); return resolveUnrequestedOne.call(this, value);
} }
async resolveRemoteUserObjects(value) { public async resolveRemoteUserObjects(value) {
const collection = await resolveCollection.call(this, value); const collection = await resolveCollection.call(this, value);
return collection.filter(element => !this.requesting.has(element)).map(element => { return collection.filter(element => !this.requesting.has(element)).map(element => {

View file

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

View file

@ -16,7 +16,7 @@ export default async (username, host, option) => {
const finger = await webFinger(acctLower, acctLower); const finger = await webFinger(acctLower, acctLower);
const self = finger.links.find(link => link.rel && link.rel.toLowerCase() === 'self'); const self = finger.links.find(link => link.rel && link.rel.toLowerCase() === 'self');
if (!self) { if (!self) {
throw new Error; throw new Error();
} }
user = await resolvePerson(self.href, usernameLower, hostLower, acctLower); user = await resolvePerson(self.href, usernameLower, hostLower, acctLower);

View file

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