Sharkey/src/remote/activitypub/create.ts

117 lines
3.2 KiB
TypeScript
Raw Normal View History

2018-03-31 13:55:00 +03:00
import { JSDOM } from 'jsdom';
2018-04-02 07:15:53 +03:00
import config from '../../config';
2018-04-02 11:11:14 +03:00
import { pack as packPost } from '../../models/post';
2018-04-01 22:15:27 +03:00
import RemoteUserObject, { IRemoteUserObject } from '../../models/remote-user-object';
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');
function createRemoteUserObject($ref, $id, { id }) {
const object = { $ref, $id };
if (!id) {
return { object };
}
return RemoteUserObject.insert({ uri: id, object });
}
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-02 11:11:14 +03:00
private async createImage(object) {
if ('attributedTo' in object && this.actor.account.uri !== object.attributedTo) {
throw new Error();
}
2018-03-31 13:55:00 +03:00
2018-04-02 11:11:14 +03:00
const { _id } = await uploadFromUrl(object.url, this.actor);
return createRemoteUserObject('driveFiles.files', _id, object);
2018-03-31 13:55:00 +03:00
}
2018-04-02 11:11:14 +03:00
private async createNote(resolver, object) {
if ('attributedTo' in object && this.actor.account.uri !== object.attributedTo) {
throw new Error();
}
2018-03-31 13:55:00 +03:00
2018-04-02 11:11:14 +03:00
const mediaIds = 'attachment' in object &&
(await Promise.all(await this.create(resolver, object.attachment)))
.filter(media => media !== null && media.object.$ref === 'driveFiles.files')
.map(({ object }) => object.$id);
const { window } = new JSDOM(object.content);
const inserted = await createPost({
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: this.actor._id,
appId: null,
viaMobile: false,
geo: undefined
}, null, null, []);
const promisedRemoteUserObject = createRemoteUserObject('posts', inserted._id, object);
const promises = [];
if (this.distribute) {
promises.push(distributePost(this.actor, inserted.mentions, packPost(inserted)));
}
2018-03-31 13:55:00 +03:00
2018-04-02 11:11:14 +03:00
// Register to search database
if (object.content && config.elasticsearch.enable) {
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-02 11:11:14 +03:00
return promisedRemoteUserObject;
}
public async create(parentResolver, value): Promise<Array<Promise<IRemoteUserObject>>> {
const results = await parentResolver.resolveRemoteUserObjects(value);
return results.map(promisedResult => promisedResult.then(({ resolver, object }) => {
switch (object.type) {
case 'Image':
return this.createImage(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-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);
};