This commit is contained in:
syuilo 2018-04-05 19:19:00 +09:00
parent fd87a63e57
commit 2a80fdeafe
3 changed files with 38 additions and 25 deletions

View file

@ -1,11 +1,13 @@
import { JSDOM } from 'jsdom'; import { JSDOM } from 'jsdom';
import * as debug from 'debug';
import Resolver from '../resolver'; import Resolver from '../resolver';
import DriveFile from '../../../models/drive-file';
import Post from '../../../models/post'; import Post from '../../../models/post';
import uploadFromUrl from '../../../api/drive/upload-from-url'; import uploadFromUrl from '../../../api/drive/upload-from-url';
import createPost from '../../../api/post/create'; import createPost from '../../../api/post/create';
const log = debug('misskey:activitypub');
export default async (actor, activity): Promise<void> => { export default async (actor, activity): Promise<void> => {
if ('actor' in activity && actor.account.uri !== activity.actor) { if ('actor' in activity && actor.account.uri !== activity.actor) {
throw new Error('invalid actor'); throw new Error('invalid actor');
@ -13,26 +15,20 @@ export default async (actor, activity): Promise<void> => {
const uri = activity.id || activity; const uri = activity.id || activity;
try { log(`Create: ${uri}`);
await Promise.all([
DriveFile.findOne({ 'metadata.uri': uri }).then(file => { // TODO: 同じURIをもつものが既に登録されていないかチェック
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}`);
}
const resolver = new Resolver(); const resolver = new Resolver();
const object = await resolver.resolve(activity); let object;
try {
object = await resolver.resolve(activity.object);
} catch (e) {
log(`Resolve failed: ${e}`);
throw e;
}
switch (object.type) { switch (object.type) {
case 'Image': case 'Image':
@ -42,15 +38,22 @@ export default async (actor, activity): Promise<void> => {
case 'Note': case 'Note':
createNote(object); createNote(object);
break; break;
default:
console.warn(`Unknown type: ${object.type}`);
break;
} }
/// ///
async function createImage(image) { async function createImage(image) {
if ('attributedTo' in image && actor.account.uri !== image.attributedTo) { if ('attributedTo' in image && actor.account.uri !== image.attributedTo) {
log(`invalid image: ${JSON.stringify(image, null, 2)}`);
throw new Error('invalid image'); throw new Error('invalid image');
} }
log(`Creating the Image: ${uri}`);
return await uploadFromUrl(image.url, actor); return await uploadFromUrl(image.url, actor);
} }
@ -59,11 +62,14 @@ export default async (actor, activity): Promise<void> => {
('attributedTo' in note && actor.account.uri !== note.attributedTo) || ('attributedTo' in note && actor.account.uri !== note.attributedTo) ||
typeof note.id !== 'string' typeof note.id !== 'string'
) { ) {
log(`invalid note: ${JSON.stringify(note, null, 2)}`);
throw new Error('invalid note'); throw new Error('invalid note');
} }
log(`Creating the Note: ${uri}`);
const media = []; const media = [];
if ('attachment' in note) { if ('attachment' in note && note.attachment != null) {
note.attachment.forEach(async media => { note.attachment.forEach(async media => {
const created = await createImage(media); const created = await createImage(media);
media.push(created); media.push(created);
@ -71,7 +77,7 @@ export default async (actor, activity): Promise<void> => {
} }
let reply = null; let reply = null;
if ('inReplyTo' in note) { if ('inReplyTo' in note && note.inReplyTo != null) {
const inReplyToPost = await Post.findOne({ uri: note.id || note }); const inReplyToPost = await Post.findOne({ uri: note.id || note });
if (inReplyToPost) { if (inReplyToPost) {
reply = inReplyToPost; reply = inReplyToPost;

View file

@ -18,6 +18,10 @@ export default async (actor, activity: IObject): Promise<void> => {
await follow(actor, activity); await follow(actor, activity);
break; break;
case 'Accept':
// noop
break;
case 'Undo': case 'Undo':
await undo(actor, activity); await undo(actor, activity);
break; break;

View file

@ -2,11 +2,14 @@ import renderDocument from './document';
import renderHashtag from './hashtag'; import renderHashtag from './hashtag';
import config from '../../../config'; import config from '../../../config';
import DriveFile from '../../../models/drive-file'; import DriveFile from '../../../models/drive-file';
import Post from '../../../models/post'; import Post, { IPost } from '../../../models/post';
import User from '../../../models/user'; import User, { IUser } from '../../../models/user';
export default async (user: IUser, post: IPost) => {
const promisedFiles = post.mediaIds
? DriveFile.find({ _id: { $in: post.mediaIds } })
: Promise.resolve([]);
export default async (user, post) => {
const promisedFiles = DriveFile.find({ _id: { $in: post.mediaIds } });
let inReplyTo; let inReplyTo;
if (post.replyId) { if (post.replyId) {
@ -39,6 +42,6 @@ export default async (user, post) => {
cc: `${attributedTo}/followers`, cc: `${attributedTo}/followers`,
inReplyTo, inReplyTo,
attachment: (await promisedFiles).map(renderDocument), attachment: (await promisedFiles).map(renderDocument),
tag: post.tags.map(renderHashtag) tag: (post.tags || []).map(renderHashtag)
}; };
}; };