Bug fixes and some refactors

This commit is contained in:
syuilo 2018-04-07 06:13:40 +09:00
parent ef30390e76
commit ba1a81dab1
3 changed files with 31 additions and 15 deletions

View file

@ -16,8 +16,6 @@ export default async (actor: IRemoteUser, activity): Promise<void> => {
log(`Create: ${uri}`); log(`Create: ${uri}`);
// TODO: 同じURIをもつものが既に登録されていないかチェック
const resolver = new Resolver(); const resolver = new Resolver();
let object; let object;

View file

@ -4,23 +4,31 @@ import * as debug from 'debug';
import Resolver from '../../resolver'; import Resolver from '../../resolver';
import Post, { IPost } from '../../../../models/post'; import Post, { IPost } from '../../../../models/post';
import createPost from '../../../../services/post/create'; import createPost from '../../../../services/post/create';
import { IRemoteUser, isRemoteUser } from '../../../../models/user'; import { IRemoteUser } from '../../../../models/user';
import resolvePerson from '../../resolve-person'; import resolvePerson from '../../resolve-person';
import createImage from './image'; import createImage from './image';
import config from '../../../../config';
const log = debug('misskey:activitypub'); const log = debug('misskey:activitypub');
/**
* 稿
*/
export default async function createNote(resolver: Resolver, actor: IRemoteUser, note, silent = false): Promise<IPost> { export default async function createNote(resolver: Resolver, actor: IRemoteUser, note, silent = false): Promise<IPost> {
if ( if (typeof note.id !== 'string') {
('attributedTo' in note && actor.account.uri !== note.attributedTo) ||
typeof note.id !== 'string'
) {
log(`invalid note: ${JSON.stringify(note, null, 2)}`); log(`invalid note: ${JSON.stringify(note, null, 2)}`);
throw new Error('invalid note'); throw new Error('invalid note');
} }
// 既に同じURIを持つものが登録されていないかチェックし、登録されていたらそれを返す
const exist = await Post.findOne({ uri: note.id });
if (exist) {
return exist;
}
log(`Creating the Note: ${note.id}`); log(`Creating the Note: ${note.id}`);
//#region 添付メディア
const media = []; const media = [];
if ('attachment' in note && note.attachment != null) { if ('attachment' in note && note.attachment != null) {
// TODO: attachmentは必ずしもImageではない // TODO: attachmentは必ずしもImageではない
@ -30,21 +38,31 @@ export default async function createNote(resolver: Resolver, actor: IRemoteUser,
media.push(created); media.push(created);
}); });
} }
//#endregion
//#region リプライ
let reply = null; let reply = null;
if ('inReplyTo' in note && note.inReplyTo != 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) { if (inReplyToPost) {
reply = inReplyToPost; reply = inReplyToPost;
} else { } else {
// 無かったらフェッチ
const inReplyTo = await resolver.resolve(note.inReplyTo) as any; const inReplyTo = await resolver.resolve(note.inReplyTo) as any;
const actor = await resolvePerson(inReplyTo.attributedTo);
if (isRemoteUser(actor)) { // リプライ先の投稿の投稿者をフェッチ
const actor = await resolvePerson(inReplyTo.attributedTo) as IRemoteUser;
// TODO: silentを常にtrueにしてはならない // TODO: silentを常にtrueにしてはならない
reply = await createNote(resolver, actor, inReplyTo); reply = await createNote(resolver, actor, inReplyTo);
} }
} }
} //#endregion
const { window } = new JSDOM(note.content); const { window } = new JSDOM(note.content);

View file

@ -19,11 +19,11 @@ export default async (user: IUser, post: IPost) => {
if (inReplyToPost !== null) { if (inReplyToPost !== null) {
const inReplyToUser = await User.findOne({ const inReplyToUser = await User.findOne({
_id: post.userId, _id: inReplyToPost.userId,
}); });
if (inReplyToUser !== null) { if (inReplyToUser !== null) {
inReplyTo = `${config.url}@${inReplyToUser.username}/${inReplyToPost._id}`; inReplyTo = inReplyToPost.uri || `${config.url}/@${inReplyToUser.username}/${inReplyToPost._id}`;
} }
} }
} else { } else {