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

View File

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

View File

@ -2,11 +2,14 @@ import renderDocument from './document';
import renderHashtag from './hashtag';
import config from '../../../config';
import DriveFile from '../../../models/drive-file';
import Post from '../../../models/post';
import User from '../../../models/user';
import Post, { IPost } from '../../../models/post';
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;
if (post.replyId) {
@ -39,6 +42,6 @@ export default async (user, post) => {
cc: `${attributedTo}/followers`,
inReplyTo,
attachment: (await promisedFiles).map(renderDocument),
tag: post.tags.map(renderHashtag)
tag: (post.tags || []).map(renderHashtag)
};
};