2018-04-04 14:12:35 +00:00
|
|
|
import { JSDOM } from 'jsdom';
|
|
|
|
const createDOMPurify = require('dompurify');
|
|
|
|
|
2018-04-03 11:09:26 +00:00
|
|
|
import Resolver from '../resolver';
|
2018-04-04 14:12:35 +00:00
|
|
|
import DriveFile from '../../../models/drive-file';
|
|
|
|
import Post from '../../../models/post';
|
2018-04-04 14:59:38 +00:00
|
|
|
import uploadFromUrl from '../../../api/drive/upload-from-url';
|
|
|
|
import createPost from '../../../api/post/create';
|
2018-03-31 10:55:00 +00:00
|
|
|
|
2018-04-04 14:59:38 +00:00
|
|
|
export default async (actor, activity): Promise<void> => {
|
2018-03-31 10:55:00 +00:00
|
|
|
if ('actor' in activity && actor.account.uri !== activity.actor) {
|
2018-04-04 14:12:35 +00:00
|
|
|
throw new Error('invalid actor');
|
2018-03-31 10:55:00 +00:00
|
|
|
}
|
|
|
|
|
2018-04-04 14:12:35 +00:00
|
|
|
const uri = activity.id || activity;
|
|
|
|
|
|
|
|
try {
|
|
|
|
await Promise.all([
|
|
|
|
DriveFile.findOne({ 'metadata.uri': uri }).then(file => {
|
|
|
|
if (file !== null) {
|
|
|
|
throw new Error();
|
|
|
|
}
|
|
|
|
}, () => {}),
|
|
|
|
Post.findOne({ uri }).then(post => {
|
|
|
|
if (post !== null) {
|
|
|
|
throw new Error();
|
|
|
|
}
|
|
|
|
}, () => {})
|
|
|
|
]);
|
|
|
|
} catch (object) {
|
|
|
|
throw new Error(`already registered: ${uri}`);
|
|
|
|
}
|
|
|
|
|
2018-04-04 14:59:38 +00:00
|
|
|
const resolver = new Resolver();
|
|
|
|
|
2018-04-04 14:12:35 +00:00
|
|
|
const object = await resolver.resolve(activity);
|
|
|
|
|
|
|
|
switch (object.type) {
|
|
|
|
case 'Image':
|
|
|
|
createImage(resolver, object);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'Note':
|
|
|
|
createNote(resolver, object);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
async function createImage(resolver: Resolver, image) {
|
|
|
|
if ('attributedTo' in image && actor.account.uri !== image.attributedTo) {
|
|
|
|
throw new Error('invalid image');
|
|
|
|
}
|
|
|
|
|
|
|
|
return await uploadFromUrl(image.url, actor);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function createNote(resolver: Resolver, note) {
|
|
|
|
if (
|
|
|
|
('attributedTo' in note && actor.account.uri !== note.attributedTo) ||
|
|
|
|
typeof note.id !== 'string'
|
|
|
|
) {
|
|
|
|
throw new Error('invalid note');
|
|
|
|
}
|
|
|
|
|
|
|
|
const mediaIds = [];
|
|
|
|
|
|
|
|
if ('attachment' in note) {
|
|
|
|
note.attachment.forEach(async media => {
|
|
|
|
const created = await createImage(resolver, media);
|
|
|
|
mediaIds.push(created._id);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const { window } = new JSDOM(note.content);
|
|
|
|
|
|
|
|
await createPost(actor, {
|
|
|
|
channelId: undefined,
|
|
|
|
index: undefined,
|
|
|
|
createdAt: new Date(note.published),
|
|
|
|
mediaIds,
|
|
|
|
replyId: undefined,
|
|
|
|
repostId: undefined,
|
|
|
|
poll: undefined,
|
|
|
|
text: window.document.body.textContent,
|
|
|
|
textHtml: note.content && createDOMPurify(window).sanitize(note.content),
|
|
|
|
userId: actor._id,
|
|
|
|
appId: null,
|
|
|
|
viaMobile: false,
|
|
|
|
geo: undefined,
|
|
|
|
uri: note.id
|
|
|
|
}, null, null, []);
|
|
|
|
}
|
2018-03-31 10:55:00 +00:00
|
|
|
};
|