2018-03-31 13:55:00 +03:00
|
|
|
import { JSDOM } from 'jsdom';
|
2018-04-03 17:45:13 +03:00
|
|
|
import { ObjectID } from 'mongodb';
|
2018-04-02 07:15:53 +03:00
|
|
|
import config from '../../config';
|
2018-04-03 17:45:13 +03:00
|
|
|
import DriveFile from '../../models/drive-file';
|
|
|
|
import Post from '../../models/post';
|
2018-04-02 11:11:14 +03:00
|
|
|
import { IRemoteUser } from '../../models/user';
|
2018-04-02 06:58:53 +03:00
|
|
|
import uploadFromUrl from '../../drive/upload-from-url';
|
2018-04-02 11:11:14 +03:00
|
|
|
import createPost from '../../post/create';
|
|
|
|
import distributePost from '../../post/distribute';
|
2018-04-01 15:24:25 +03:00
|
|
|
import Resolver from './resolver';
|
2018-03-31 13:55:00 +03:00
|
|
|
const createDOMPurify = require('dompurify');
|
|
|
|
|
2018-04-03 17:45:13 +03:00
|
|
|
type IResult = {
|
|
|
|
resolver: Resolver;
|
|
|
|
object: {
|
|
|
|
$ref: string;
|
|
|
|
$id: ObjectID;
|
|
|
|
};
|
|
|
|
};
|
2018-03-31 13:55:00 +03:00
|
|
|
|
2018-04-02 11:11:14 +03:00
|
|
|
class Creator {
|
|
|
|
private actor: IRemoteUser;
|
|
|
|
private distribute: boolean;
|
|
|
|
|
|
|
|
constructor(actor, distribute) {
|
|
|
|
this.actor = actor;
|
|
|
|
this.distribute = distribute;
|
2018-03-31 13:55:00 +03:00
|
|
|
}
|
|
|
|
|
2018-04-03 17:45:13 +03:00
|
|
|
private async createImage(resolver: Resolver, image) {
|
2018-04-03 17:14:56 +03:00
|
|
|
if ('attributedTo' in image && this.actor.account.uri !== image.attributedTo) {
|
2018-04-02 11:11:14 +03:00
|
|
|
throw new Error();
|
|
|
|
}
|
2018-03-31 13:55:00 +03:00
|
|
|
|
2018-04-03 17:45:13 +03:00
|
|
|
const { _id } = await uploadFromUrl(image.url, this.actor, image.id || null);
|
|
|
|
return {
|
|
|
|
resolver,
|
|
|
|
object: { $ref: 'driveFiles.files', $id: _id }
|
|
|
|
};
|
2018-03-31 13:55:00 +03:00
|
|
|
}
|
|
|
|
|
2018-04-03 17:14:56 +03:00
|
|
|
private async createNote(resolver: Resolver, note) {
|
2018-04-03 17:45:13 +03:00
|
|
|
if (
|
|
|
|
('attributedTo' in note && this.actor.account.uri !== note.attributedTo) ||
|
|
|
|
typeof note.id !== 'string'
|
|
|
|
) {
|
2018-04-02 11:11:14 +03:00
|
|
|
throw new Error();
|
|
|
|
}
|
2018-03-31 13:55:00 +03:00
|
|
|
|
2018-04-03 17:14:56 +03:00
|
|
|
const mediaIds = 'attachment' in note &&
|
|
|
|
(await Promise.all(await this.create(resolver, note.attachment)))
|
2018-04-02 11:11:14 +03:00
|
|
|
.filter(media => media !== null && media.object.$ref === 'driveFiles.files')
|
|
|
|
.map(({ object }) => object.$id);
|
|
|
|
|
2018-04-03 17:14:56 +03:00
|
|
|
const { window } = new JSDOM(note.content);
|
2018-04-02 11:11:14 +03:00
|
|
|
|
|
|
|
const inserted = await createPost({
|
|
|
|
channelId: undefined,
|
|
|
|
index: undefined,
|
2018-04-03 17:14:56 +03:00
|
|
|
createdAt: new Date(note.published),
|
2018-04-02 11:11:14 +03:00
|
|
|
mediaIds,
|
|
|
|
replyId: undefined,
|
|
|
|
repostId: undefined,
|
|
|
|
poll: undefined,
|
|
|
|
text: window.document.body.textContent,
|
2018-04-03 17:14:56 +03:00
|
|
|
textHtml: note.content && createDOMPurify(window).sanitize(note.content),
|
2018-04-02 11:11:14 +03:00
|
|
|
userId: this.actor._id,
|
|
|
|
appId: null,
|
|
|
|
viaMobile: false,
|
2018-04-03 17:45:13 +03:00
|
|
|
geo: undefined,
|
|
|
|
uri: note.id
|
2018-04-02 11:11:14 +03:00
|
|
|
}, null, null, []);
|
|
|
|
|
|
|
|
const promises = [];
|
|
|
|
|
|
|
|
if (this.distribute) {
|
2018-04-02 14:16:13 +03:00
|
|
|
promises.push(distributePost(this.actor, inserted.mentions, inserted));
|
2018-04-02 11:11:14 +03:00
|
|
|
}
|
2018-03-31 13:55:00 +03:00
|
|
|
|
2018-04-02 11:11:14 +03:00
|
|
|
// Register to search database
|
2018-04-03 17:14:56 +03:00
|
|
|
if (note.content && config.elasticsearch.enable) {
|
2018-04-02 11:11:14 +03:00
|
|
|
const es = require('../../db/elasticsearch');
|
|
|
|
|
|
|
|
promises.push(new Promise((resolve, reject) => {
|
|
|
|
es.index({
|
|
|
|
index: 'misskey',
|
|
|
|
type: 'post',
|
|
|
|
id: inserted._id.toString(),
|
|
|
|
body: {
|
|
|
|
text: window.document.body.textContent
|
|
|
|
}
|
|
|
|
}, resolve);
|
|
|
|
}));
|
|
|
|
}
|
2018-03-31 13:55:00 +03:00
|
|
|
|
2018-04-02 11:11:14 +03:00
|
|
|
await Promise.all(promises);
|
2018-03-31 13:55:00 +03:00
|
|
|
|
2018-04-03 17:45:13 +03:00
|
|
|
return {
|
|
|
|
resolver,
|
|
|
|
object: { $ref: 'posts', id: inserted._id }
|
|
|
|
};
|
2018-04-02 11:11:14 +03:00
|
|
|
}
|
|
|
|
|
2018-04-03 17:45:13 +03:00
|
|
|
public async create(parentResolver: Resolver, value): Promise<Array<Promise<IResult>>> {
|
2018-04-03 14:39:27 +03:00
|
|
|
const collection = await parentResolver.resolveCollection(value);
|
|
|
|
|
|
|
|
return collection.object.map(async element => {
|
2018-04-03 18:14:18 +03:00
|
|
|
const uri = element.id || element;
|
|
|
|
|
|
|
|
try {
|
|
|
|
await Promise.all([
|
|
|
|
DriveFile.findOne({ 'metadata.uri': uri }).then(file => {
|
|
|
|
if (file === null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw {
|
|
|
|
$ref: 'driveFile.files',
|
|
|
|
$id: file._id
|
|
|
|
};
|
|
|
|
}, () => {}),
|
|
|
|
Post.findOne({ uri }).then(post => {
|
|
|
|
if (post === null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw {
|
|
|
|
$ref: 'posts',
|
|
|
|
$id: post._id
|
|
|
|
};
|
|
|
|
}, () => {})
|
|
|
|
]);
|
|
|
|
} catch (object) {
|
|
|
|
return {
|
|
|
|
resolver: collection.resolver,
|
|
|
|
object
|
|
|
|
};
|
2018-04-03 14:39:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const { resolver, object } = await collection.resolver.resolveOne(element);
|
2018-04-02 11:11:14 +03:00
|
|
|
|
|
|
|
switch (object.type) {
|
|
|
|
case 'Image':
|
2018-04-03 17:45:13 +03:00
|
|
|
return this.createImage(resolver, object);
|
2018-03-31 13:55:00 +03:00
|
|
|
|
2018-04-02 11:11:14 +03:00
|
|
|
case 'Note':
|
|
|
|
return this.createNote(resolver, object);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
2018-04-03 14:39:27 +03:00
|
|
|
});
|
2018-04-02 11:11:14 +03:00
|
|
|
}
|
2018-04-01 15:24:25 +03:00
|
|
|
}
|
2018-04-02 11:11:14 +03:00
|
|
|
|
|
|
|
export default (resolver: Resolver, actor, value, distribute?: boolean) => {
|
|
|
|
const creator = new Creator(actor, distribute);
|
|
|
|
return creator.create(resolver, value);
|
|
|
|
};
|