2018-03-31 12:55:00 +02:00
|
|
|
import { JSDOM } from 'jsdom';
|
2018-04-02 06:15:53 +02:00
|
|
|
import config from '../../config';
|
2018-04-01 21:15:27 +02:00
|
|
|
import Post from '../../models/post';
|
|
|
|
import RemoteUserObject, { IRemoteUserObject } from '../../models/remote-user-object';
|
2018-04-02 05:58:53 +02:00
|
|
|
import uploadFromUrl from '../../drive/upload-from-url';
|
2018-04-01 14:24:25 +02:00
|
|
|
import Resolver from './resolver';
|
2018-03-31 12:55:00 +02:00
|
|
|
const createDOMPurify = require('dompurify');
|
|
|
|
|
|
|
|
function createRemoteUserObject($ref, $id, { id }) {
|
|
|
|
const object = { $ref, $id };
|
|
|
|
|
|
|
|
if (!id) {
|
|
|
|
return { object };
|
|
|
|
}
|
|
|
|
|
|
|
|
return RemoteUserObject.insert({ uri: id, object });
|
|
|
|
}
|
|
|
|
|
|
|
|
async function createImage(actor, object) {
|
|
|
|
if ('attributedTo' in object && actor.account.uri !== object.attributedTo) {
|
2018-04-01 14:24:25 +02:00
|
|
|
throw new Error();
|
2018-03-31 12:55:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const { _id } = await uploadFromUrl(object.url, actor);
|
|
|
|
return createRemoteUserObject('driveFiles.files', _id, object);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function createNote(resolver, actor, object) {
|
|
|
|
if ('attributedTo' in object && actor.account.uri !== object.attributedTo) {
|
2018-04-01 14:24:25 +02:00
|
|
|
throw new Error();
|
2018-03-31 12:55:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const mediaIds = 'attachment' in object &&
|
|
|
|
(await Promise.all(await create(resolver, actor, object.attachment)))
|
|
|
|
.filter(media => media !== null && media.object.$ref === 'driveFiles.files')
|
|
|
|
.map(({ object }) => object.$id);
|
|
|
|
|
|
|
|
const { window } = new JSDOM(object.content);
|
|
|
|
|
|
|
|
const { _id } = await Post.insert({
|
|
|
|
channelId: undefined,
|
|
|
|
index: undefined,
|
|
|
|
createdAt: new Date(object.published),
|
|
|
|
mediaIds,
|
|
|
|
replyId: undefined,
|
|
|
|
repostId: undefined,
|
|
|
|
poll: undefined,
|
|
|
|
text: window.document.body.textContent,
|
|
|
|
textHtml: object.content && createDOMPurify(window).sanitize(object.content),
|
|
|
|
userId: actor._id,
|
|
|
|
appId: null,
|
|
|
|
viaMobile: false,
|
|
|
|
geo: undefined
|
|
|
|
});
|
|
|
|
|
|
|
|
// Register to search database
|
|
|
|
if (object.content && config.elasticsearch.enable) {
|
|
|
|
const es = require('../../db/elasticsearch');
|
|
|
|
|
|
|
|
es.index({
|
|
|
|
index: 'misskey',
|
|
|
|
type: 'post',
|
|
|
|
id: _id.toString(),
|
|
|
|
body: {
|
|
|
|
text: window.document.body.textContent
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return createRemoteUserObject('posts', _id, object);
|
|
|
|
}
|
|
|
|
|
2018-04-01 14:24:25 +02:00
|
|
|
export default async function create(parentResolver: Resolver, actor, value): Promise<Array<Promise<IRemoteUserObject>>> {
|
2018-03-31 12:55:00 +02:00
|
|
|
const results = await parentResolver.resolveRemoteUserObjects(value);
|
|
|
|
|
2018-04-01 14:24:25 +02:00
|
|
|
return results.map(promisedResult => promisedResult.then(({ resolver, object }) => {
|
2018-03-31 12:55:00 +02:00
|
|
|
switch (object.type) {
|
|
|
|
case 'Image':
|
|
|
|
return createImage(actor, object);
|
|
|
|
|
|
|
|
case 'Note':
|
|
|
|
return createNote(resolver, actor, object);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}));
|
2018-04-01 14:24:25 +02:00
|
|
|
}
|