This commit is contained in:
syuilo 2018-04-05 22:49:41 +09:00
parent a6abcd1aa5
commit 5f8ab58446
4 changed files with 62 additions and 53 deletions

View File

@ -5,10 +5,12 @@ import Resolver from '../resolver';
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';
import { IRemoteUser, isRemoteUser } from '../../../models/user';
import resolvePerson from '../resolve-person';
const log = debug('misskey:activitypub'); const log = debug('misskey:activitypub');
export default async (actor, activity): Promise<void> => { export default async (actor: IRemoteUser, 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');
} }
@ -32,32 +34,31 @@ export default async (actor, activity): Promise<void> => {
switch (object.type) { switch (object.type) {
case 'Image': case 'Image':
createImage(object); createImage(resolver, actor, object);
break; break;
case 'Note': case 'Note':
createNote(object); createNote(resolver, actor, object);
break; break;
default: default:
console.warn(`Unknown type: ${object.type}`); console.warn(`Unknown type: ${object.type}`);
break; break;
} }
};
/// async function createImage(resolver: Resolver, actor: IRemoteUser, 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)}`); log(`invalid image: ${JSON.stringify(image, null, 2)}`);
throw new Error('invalid image'); throw new Error('invalid image');
} }
log(`Creating the Image: ${uri}`); log(`Creating the Image: ${image.id}`);
return await uploadFromUrl(image.url, actor); return await uploadFromUrl(image.url, actor);
} }
async function createNote(note) { async function createNote(resolver: Resolver, actor: IRemoteUser, note) {
if ( if (
('attributedTo' in note && actor.account.uri !== note.attributedTo) || ('attributedTo' in note && actor.account.uri !== note.attributedTo) ||
typeof note.id !== 'string' typeof note.id !== 'string'
@ -66,12 +67,12 @@ export default async (actor, activity): Promise<void> => {
throw new Error('invalid note'); throw new Error('invalid note');
} }
log(`Creating the Note: ${uri}`); log(`Creating the Note: ${note.id}`);
const media = []; const media = [];
if ('attachment' in note && note.attachment != null) { 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(resolver, note.actor, media);
media.push(created); media.push(created);
}); });
} }
@ -82,7 +83,11 @@ export default async (actor, activity): Promise<void> => {
if (inReplyToPost) { if (inReplyToPost) {
reply = inReplyToPost; reply = inReplyToPost;
} else { } else {
reply = await createNote(await resolver.resolve(note)); const inReplyTo = await resolver.resolve(note.inReplyTo) as any;
const actor = await resolvePerson(inReplyTo.attributedTo);
if (isRemoteUser(actor)) {
reply = await createNote(resolver, actor, inReplyTo);
}
} }
} }
@ -99,4 +104,3 @@ export default async (actor, activity): Promise<void> => {
uri: note.id uri: note.id
}); });
} }
};

View File

@ -3,8 +3,9 @@ import performDeleteActivity from './delete';
import follow from './follow'; import follow from './follow';
import undo from './undo'; import undo from './undo';
import { IObject } from '../type'; import { IObject } from '../type';
import { IUser } from '../../../models/user';
export default async (actor, activity: IObject): Promise<void> => { export default async (actor: IUser, activity: IObject): Promise<void> => {
switch (activity.type) { switch (activity.type) {
case 'Create': case 'Create':
await create(actor, activity); await create(actor, activity);

View File

@ -11,7 +11,7 @@ export default async (value, verifier?: string) => {
const object = await resolver.resolve(value) as any; const object = await resolver.resolve(value) as any;
if ( if (
object === null || object == null ||
object.type !== 'Person' || object.type !== 'Person' ||
typeof object.preferredUsername !== 'string' || typeof object.preferredUsername !== 'string' ||
!validateUsername(object.preferredUsername) || !validateUsername(object.preferredUsername) ||

View File

@ -33,6 +33,10 @@ export default class Resolver {
} }
public async resolve(value): Promise<IObject> { public async resolve(value): Promise<IObject> {
if (value == null) {
throw new Error('resolvee is null (or undefined)');
}
if (typeof value !== 'string') { if (typeof value !== 'string') {
return value; return value;
} }