Split code
This commit is contained in:
parent
574e3b0bfd
commit
4e9ae8e8d5
@ -1,108 +0,0 @@
|
|||||||
import { JSDOM } from 'jsdom';
|
|
||||||
import * as debug from 'debug';
|
|
||||||
|
|
||||||
import Resolver from '../resolver';
|
|
||||||
import Post from '../../../models/post';
|
|
||||||
import uploadFromUrl from '../../../services/drive/upload-from-url';
|
|
||||||
import createPost from '../../../services/post/create';
|
|
||||||
import { IRemoteUser, isRemoteUser } from '../../../models/user';
|
|
||||||
import resolvePerson from '../resolve-person';
|
|
||||||
|
|
||||||
const log = debug('misskey:activitypub');
|
|
||||||
|
|
||||||
export default async (actor: IRemoteUser, activity): Promise<void> => {
|
|
||||||
if ('actor' in activity && actor.account.uri !== activity.actor) {
|
|
||||||
throw new Error('invalid actor');
|
|
||||||
}
|
|
||||||
|
|
||||||
const uri = activity.id || activity;
|
|
||||||
|
|
||||||
log(`Create: ${uri}`);
|
|
||||||
|
|
||||||
// TODO: 同じURIをもつものが既に登録されていないかチェック
|
|
||||||
|
|
||||||
const resolver = new Resolver();
|
|
||||||
|
|
||||||
let object;
|
|
||||||
|
|
||||||
try {
|
|
||||||
object = await resolver.resolve(activity.object);
|
|
||||||
} catch (e) {
|
|
||||||
log(`Resolution failed: ${e}`);
|
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (object.type) {
|
|
||||||
case 'Image':
|
|
||||||
createImage(resolver, actor, object);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'Note':
|
|
||||||
createNote(resolver, actor, object);
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
console.warn(`Unknown type: ${object.type}`);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
async function createImage(resolver: Resolver, actor: IRemoteUser, 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: ${image.id}`);
|
|
||||||
|
|
||||||
return await uploadFromUrl(image.url, actor);
|
|
||||||
}
|
|
||||||
|
|
||||||
async function createNote(resolver: Resolver, actor: IRemoteUser, note) {
|
|
||||||
if (
|
|
||||||
('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: ${note.id}`);
|
|
||||||
|
|
||||||
const media = [];
|
|
||||||
if ('attachment' in note && note.attachment != null) {
|
|
||||||
// TODO: attachmentは必ずしもImageではない
|
|
||||||
// TODO: ループの中でawaitはすべきでない
|
|
||||||
note.attachment.forEach(async media => {
|
|
||||||
const created = await createImage(resolver, note.actor, media);
|
|
||||||
media.push(created);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
let reply = null;
|
|
||||||
if ('inReplyTo' in note && note.inReplyTo != null) {
|
|
||||||
const inReplyToPost = await Post.findOne({ uri: note.inReplyTo.id || note.inReplyTo });
|
|
||||||
if (inReplyToPost) {
|
|
||||||
reply = inReplyToPost;
|
|
||||||
} else {
|
|
||||||
const inReplyTo = await resolver.resolve(note.inReplyTo) as any;
|
|
||||||
const actor = await resolvePerson(inReplyTo.attributedTo);
|
|
||||||
if (isRemoteUser(actor)) {
|
|
||||||
reply = await createNote(resolver, actor, inReplyTo);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const { window } = new JSDOM(note.content);
|
|
||||||
|
|
||||||
return await createPost(actor, {
|
|
||||||
createdAt: new Date(note.published),
|
|
||||||
media,
|
|
||||||
reply,
|
|
||||||
repost: undefined,
|
|
||||||
text: window.document.body.textContent,
|
|
||||||
viaMobile: false,
|
|
||||||
geo: undefined,
|
|
||||||
uri: note.id
|
|
||||||
});
|
|
||||||
}
|
|
19
src/remote/activitypub/act/create/image.ts
Normal file
19
src/remote/activitypub/act/create/image.ts
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
import * as debug from 'debug';
|
||||||
|
|
||||||
|
import Resolver from '../../resolver';
|
||||||
|
import uploadFromUrl from '../../../../services/drive/upload-from-url';
|
||||||
|
import { IRemoteUser } from '../../../../models/user';
|
||||||
|
import { IDriveFile } from '../../../../models/drive-file';
|
||||||
|
|
||||||
|
const log = debug('misskey:activitypub');
|
||||||
|
|
||||||
|
export default async function(resolver: Resolver, actor: IRemoteUser, image): Promise<IDriveFile> {
|
||||||
|
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: ${image.id}`);
|
||||||
|
|
||||||
|
return await uploadFromUrl(image.url, actor);
|
||||||
|
}
|
45
src/remote/activitypub/act/create/index.ts
Normal file
45
src/remote/activitypub/act/create/index.ts
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
import * as debug from 'debug';
|
||||||
|
|
||||||
|
import Resolver from '../../resolver';
|
||||||
|
import { IRemoteUser } from '../../../../models/user';
|
||||||
|
import createNote from './note';
|
||||||
|
import createImage from './image';
|
||||||
|
|
||||||
|
const log = debug('misskey:activitypub');
|
||||||
|
|
||||||
|
export default async (actor: IRemoteUser, activity): Promise<void> => {
|
||||||
|
if ('actor' in activity && actor.account.uri !== activity.actor) {
|
||||||
|
throw new Error('invalid actor');
|
||||||
|
}
|
||||||
|
|
||||||
|
const uri = activity.id || activity;
|
||||||
|
|
||||||
|
log(`Create: ${uri}`);
|
||||||
|
|
||||||
|
// TODO: 同じURIをもつものが既に登録されていないかチェック
|
||||||
|
|
||||||
|
const resolver = new Resolver();
|
||||||
|
|
||||||
|
let object;
|
||||||
|
|
||||||
|
try {
|
||||||
|
object = await resolver.resolve(activity.object);
|
||||||
|
} catch (e) {
|
||||||
|
log(`Resolution failed: ${e}`);
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (object.type) {
|
||||||
|
case 'Image':
|
||||||
|
createImage(resolver, actor, object);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'Note':
|
||||||
|
createNote(resolver, actor, object);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
console.warn(`Unknown type: ${object.type}`);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
};
|
60
src/remote/activitypub/act/create/note.ts
Normal file
60
src/remote/activitypub/act/create/note.ts
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
import { JSDOM } from 'jsdom';
|
||||||
|
import * as debug from 'debug';
|
||||||
|
|
||||||
|
import Resolver from '../../resolver';
|
||||||
|
import Post, { IPost } from '../../../../models/post';
|
||||||
|
import createPost from '../../../../services/post/create';
|
||||||
|
import { IRemoteUser, isRemoteUser } from '../../../../models/user';
|
||||||
|
import resolvePerson from '../../resolve-person';
|
||||||
|
import createImage from './image';
|
||||||
|
|
||||||
|
const log = debug('misskey:activitypub');
|
||||||
|
|
||||||
|
export default async function createNote(resolver: Resolver, actor: IRemoteUser, note): Promise<IPost> {
|
||||||
|
if (
|
||||||
|
('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: ${note.id}`);
|
||||||
|
|
||||||
|
const media = [];
|
||||||
|
if ('attachment' in note && note.attachment != null) {
|
||||||
|
// TODO: attachmentは必ずしもImageではない
|
||||||
|
// TODO: ループの中でawaitはすべきでない
|
||||||
|
note.attachment.forEach(async media => {
|
||||||
|
const created = await createImage(resolver, note.actor, media);
|
||||||
|
media.push(created);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
let reply = null;
|
||||||
|
if ('inReplyTo' in note && note.inReplyTo != null) {
|
||||||
|
const inReplyToPost = await Post.findOne({ uri: note.inReplyTo.id || note.inReplyTo });
|
||||||
|
if (inReplyToPost) {
|
||||||
|
reply = inReplyToPost;
|
||||||
|
} else {
|
||||||
|
const inReplyTo = await resolver.resolve(note.inReplyTo) as any;
|
||||||
|
const actor = await resolvePerson(inReplyTo.attributedTo);
|
||||||
|
if (isRemoteUser(actor)) {
|
||||||
|
reply = await createNote(resolver, actor, inReplyTo);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const { window } = new JSDOM(note.content);
|
||||||
|
|
||||||
|
return await createPost(actor, {
|
||||||
|
createdAt: new Date(note.published),
|
||||||
|
media,
|
||||||
|
reply,
|
||||||
|
repost: undefined,
|
||||||
|
text: window.document.body.textContent,
|
||||||
|
viaMobile: false,
|
||||||
|
geo: undefined,
|
||||||
|
uri: note.id
|
||||||
|
});
|
||||||
|
}
|
@ -1,6 +1,5 @@
|
|||||||
import Resolver from '../resolver';
|
import Resolver from '../../resolver';
|
||||||
import Post from '../../../models/post';
|
import deleteNote from './note';
|
||||||
import { createDb } from '../../../queue';
|
|
||||||
|
|
||||||
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) {
|
||||||
@ -16,13 +15,4 @@ export default async (actor, activity): Promise<void> => {
|
|||||||
deleteNote(object);
|
deleteNote(object);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function deleteNote(note) {
|
|
||||||
const post = await Post.findOneAndDelete({ uri: note.id });
|
|
||||||
|
|
||||||
createDb({
|
|
||||||
type: 'deletePostDependents',
|
|
||||||
id: post._id
|
|
||||||
}).delay(65536).save();
|
|
||||||
}
|
|
||||||
};
|
};
|
11
src/remote/activitypub/act/delete/note.ts
Normal file
11
src/remote/activitypub/act/delete/note.ts
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
import Post from '../../../../models/post';
|
||||||
|
import { createDb } from '../../../../queue';
|
||||||
|
|
||||||
|
export default async function(note) {
|
||||||
|
const post = await Post.findOneAndDelete({ uri: note.id });
|
||||||
|
|
||||||
|
createDb({
|
||||||
|
type: 'deletePostDependents',
|
||||||
|
id: post._id
|
||||||
|
}).delay(65536).save();
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user