Skip to content
Snippets Groups Projects
Commit ba1a81da authored by syuilo's avatar syuilo
Browse files

Bug fixes and some refactors

parent ef30390e
No related branches found
No related tags found
No related merge requests found
......@@ -16,8 +16,6 @@ export default async (actor: IRemoteUser, activity): Promise<void> => {
log(`Create: ${uri}`);
// TODO: 同じURIをもつものが既に登録されていないかチェック
const resolver = new Resolver();
let object;
......
......@@ -4,23 +4,31 @@ 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 { IRemoteUser } from '../../../../models/user';
import resolvePerson from '../../resolve-person';
import createImage from './image';
import config from '../../../../config';
const log = debug('misskey:activitypub');
/**
* 投稿作成アクティビティを捌きます
*/
export default async function createNote(resolver: Resolver, actor: IRemoteUser, note, silent = false): Promise<IPost> {
if (
('attributedTo' in note && actor.account.uri !== note.attributedTo) ||
typeof note.id !== 'string'
) {
if (typeof note.id !== 'string') {
log(`invalid note: ${JSON.stringify(note, null, 2)}`);
throw new Error('invalid note');
}
// 既に同じURIを持つものが登録されていないかチェックし、登録されていたらそれを返す
const exist = await Post.findOne({ uri: note.id });
if (exist) {
return exist;
}
log(`Creating the Note: ${note.id}`);
//#region 添付メディア
const media = [];
if ('attachment' in note && note.attachment != null) {
// TODO: attachmentは必ずしもImageではない
......@@ -30,21 +38,31 @@ export default async function createNote(resolver: Resolver, actor: IRemoteUser,
media.push(created);
});
}
//#endregion
//#region リプライ
let reply = null;
if ('inReplyTo' in note && note.inReplyTo != null) {
const inReplyToPost = await Post.findOne({ uri: note.inReplyTo.id || note.inReplyTo });
// リプライ先の投稿がMisskeyに登録されているか調べる
const uri: string = note.inReplyTo.id || note.inReplyTo;
const inReplyToPost = uri.startsWith(config.url + '/')
? await Post.findOne({ _id: uri.split('/').pop() })
: await Post.findOne({ uri });
if (inReplyToPost) {
reply = inReplyToPost;
} else {
// 無かったらフェッチ
const inReplyTo = await resolver.resolve(note.inReplyTo) as any;
const actor = await resolvePerson(inReplyTo.attributedTo);
if (isRemoteUser(actor)) {
// TODO: silentを常にtrueにしてはならない
reply = await createNote(resolver, actor, inReplyTo);
}
// リプライ先の投稿の投稿者をフェッチ
const actor = await resolvePerson(inReplyTo.attributedTo) as IRemoteUser;
// TODO: silentを常にtrueにしてはならない
reply = await createNote(resolver, actor, inReplyTo);
}
}
//#endregion
const { window } = new JSDOM(note.content);
......
......@@ -19,11 +19,11 @@ export default async (user: IUser, post: IPost) => {
if (inReplyToPost !== null) {
const inReplyToUser = await User.findOne({
_id: post.userId,
_id: inReplyToPost.userId,
});
if (inReplyToUser !== null) {
inReplyTo = `${config.url}@${inReplyToUser.username}/${inReplyToPost._id}`;
inReplyTo = inReplyToPost.uri || `${config.url}/@${inReplyToUser.username}/${inReplyToPost._id}`;
}
}
} else {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment