monkeeShark/src/remote/activitypub/act/create.ts

103 lines
2.3 KiB
TypeScript
Raw Normal View History

2018-04-04 14:12:35 +00:00
import { JSDOM } from 'jsdom';
2018-04-05 10:19:00 +00:00
import * as debug from 'debug';
2018-04-04 14:12:35 +00:00
2018-04-03 11:09:26 +00:00
import Resolver from '../resolver';
2018-04-04 14:12:35 +00:00
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-05 10:19:00 +00:00
const log = debug('misskey:activitypub');
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;
2018-04-05 10:19:00 +00:00
log(`Create: ${uri}`);
// TODO: 同じURIをもつものが既に登録されていないかチェック
2018-04-04 14:12:35 +00:00
2018-04-04 14:59:38 +00:00
const resolver = new Resolver();
2018-04-05 10:19:00 +00:00
let object;
try {
object = await resolver.resolve(activity.object);
} catch (e) {
log(`Resolve failed: ${e}`);
throw e;
}
2018-04-04 14:12:35 +00:00
switch (object.type) {
case 'Image':
2018-04-04 18:21:11 +00:00
createImage(object);
2018-04-04 14:12:35 +00:00
break;
case 'Note':
2018-04-04 18:21:11 +00:00
createNote(object);
2018-04-04 14:12:35 +00:00
break;
2018-04-05 10:19:00 +00:00
default:
console.warn(`Unknown type: ${object.type}`);
break;
2018-04-04 14:12:35 +00:00
}
///
2018-04-04 18:21:11 +00:00
async function createImage(image) {
2018-04-04 14:12:35 +00:00
if ('attributedTo' in image && actor.account.uri !== image.attributedTo) {
2018-04-05 10:19:00 +00:00
log(`invalid image: ${JSON.stringify(image, null, 2)}`);
2018-04-04 14:12:35 +00:00
throw new Error('invalid image');
}
2018-04-05 10:19:00 +00:00
log(`Creating the Image: ${uri}`);
2018-04-04 14:12:35 +00:00
return await uploadFromUrl(image.url, actor);
}
2018-04-04 18:21:11 +00:00
async function createNote(note) {
2018-04-04 14:12:35 +00:00
if (
('attributedTo' in note && actor.account.uri !== note.attributedTo) ||
typeof note.id !== 'string'
) {
2018-04-05 10:19:00 +00:00
log(`invalid note: ${JSON.stringify(note, null, 2)}`);
2018-04-04 14:12:35 +00:00
throw new Error('invalid note');
}
2018-04-05 10:19:00 +00:00
log(`Creating the Note: ${uri}`);
2018-04-04 15:50:57 +00:00
const media = [];
2018-04-05 10:19:00 +00:00
if ('attachment' in note && note.attachment != null) {
2018-04-04 14:12:35 +00:00
note.attachment.forEach(async media => {
2018-04-04 18:21:11 +00:00
const created = await createImage(media);
2018-04-04 15:50:57 +00:00
media.push(created);
2018-04-04 14:12:35 +00:00
});
}
2018-04-04 18:21:11 +00:00
let reply = null;
2018-04-05 10:19:00 +00:00
if ('inReplyTo' in note && note.inReplyTo != null) {
2018-04-04 18:21:11 +00:00
const inReplyToPost = await Post.findOne({ uri: note.id || note });
if (inReplyToPost) {
reply = inReplyToPost;
} else {
reply = await createNote(await resolver.resolve(note));
}
}
2018-04-04 14:12:35 +00:00
const { window } = new JSDOM(note.content);
2018-04-04 18:21:11 +00:00
return await createPost(actor, {
2018-04-04 14:12:35 +00:00
createdAt: new Date(note.published),
2018-04-04 15:50:57 +00:00
media,
2018-04-04 18:21:11 +00:00
reply,
2018-04-04 15:50:57 +00:00
repost: undefined,
2018-04-04 14:12:35 +00:00
text: window.document.body.textContent,
viaMobile: false,
geo: undefined,
uri: note.id
2018-04-04 15:50:57 +00:00
});
2018-04-04 14:12:35 +00:00
}
2018-03-31 10:55:00 +00:00
};