Sharkey/src/remote/activitypub/create.ts

126 lines
3.3 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-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-03 17:14:56 +03:00
private async createImage(image) {
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:14:56 +03:00
const { _id } = await uploadFromUrl(image.url, this.actor);
return createRemoteUserObject('driveFiles.files', _id, image);
2018-03-31 13:55:00 +03:00
}
2018-04-03 17:14:56 +03:00
private async createNote(resolver: Resolver, note) {
if ('attributedTo' in note && this.actor.account.uri !== note.attributedTo) {
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,
geo: undefined
}, null, null, []);
2018-04-03 17:14:56 +03:00
const promisedRemoteUserObject = createRemoteUserObject('posts', inserted._id, note);
2018-04-02 11:11:14 +03:00
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-02 11:11:14 +03:00
return promisedRemoteUserObject;
}
2018-04-03 17:01:48 +03:00
public async create(parentResolver: Resolver, value): Promise<Array<Promise<IRemoteUserObject>>> {
const collection = await parentResolver.resolveCollection(value);
return collection.object.map(async element => {
if (typeof element === 'string') {
const object = RemoteUserObject.findOne({ uri: element });
if (object !== null) {
return object;
}
}
const { resolver, object } = await collection.resolver.resolveOne(element);
2018-04-02 11:11:14 +03:00
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-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);
};