2018-10-07 06:20:55 -05:00
|
|
|
import $ from 'cafy';
|
2018-11-01 23:47:44 -05:00
|
|
|
import define from '../../define';
|
2018-10-07 06:20:55 -05:00
|
|
|
import config from '../../../../config';
|
|
|
|
import * as mongo from 'mongodb';
|
|
|
|
import User, { pack as packUser, IUser } from '../../../../models/user';
|
|
|
|
import { createPerson } from '../../../../remote/activitypub/models/person';
|
|
|
|
import Note, { pack as packNote, INote } from '../../../../models/note';
|
|
|
|
import { createNote } from '../../../../remote/activitypub/models/note';
|
|
|
|
import Resolver from '../../../../remote/activitypub/resolver';
|
2019-02-21 20:46:58 -06:00
|
|
|
import { ApiError } from '../../error';
|
2019-03-12 21:21:16 -05:00
|
|
|
import Instance from '../../../../models/instance';
|
|
|
|
import { extractDbHost } from '../../../../misc/convert-host';
|
2018-10-07 06:20:55 -05:00
|
|
|
|
|
|
|
export const meta = {
|
2019-02-22 20:20:58 -06:00
|
|
|
tags: ['federation'],
|
|
|
|
|
2018-10-07 06:20:55 -05:00
|
|
|
desc: {
|
|
|
|
'ja-JP': 'URIを指定してActivityPubオブジェクトを参照します。'
|
|
|
|
},
|
|
|
|
|
|
|
|
requireCredential: false,
|
|
|
|
|
|
|
|
params: {
|
2018-11-01 13:32:24 -05:00
|
|
|
uri: {
|
|
|
|
validator: $.str,
|
2018-10-07 06:20:55 -05:00
|
|
|
desc: {
|
|
|
|
'ja-JP': 'ActivityPubオブジェクトのURI'
|
|
|
|
}
|
2018-11-01 13:32:24 -05:00
|
|
|
},
|
2018-10-07 06:20:55 -05:00
|
|
|
},
|
2019-02-21 20:46:58 -06:00
|
|
|
|
|
|
|
errors: {
|
|
|
|
noSuchObject: {
|
|
|
|
message: 'No such object.',
|
|
|
|
code: 'NO_SUCH_OBJECT',
|
|
|
|
id: 'dc94d745-1262-4e63-a17d-fecaa57efc82'
|
|
|
|
}
|
|
|
|
}
|
2018-10-07 06:20:55 -05:00
|
|
|
};
|
|
|
|
|
2019-02-21 20:46:58 -06:00
|
|
|
export default define(meta, async (ps) => {
|
|
|
|
const object = await fetchAny(ps.uri);
|
|
|
|
if (object) {
|
|
|
|
return object;
|
|
|
|
} else {
|
|
|
|
throw new ApiError(meta.errors.noSuchObject);
|
|
|
|
}
|
|
|
|
});
|
2018-10-07 06:20:55 -05:00
|
|
|
|
|
|
|
/***
|
|
|
|
* URIからUserかNoteを解決する
|
|
|
|
*/
|
|
|
|
async function fetchAny(uri: string) {
|
|
|
|
// URIがこのサーバーを指しているなら、ローカルユーザーIDとしてDBからフェッチ
|
|
|
|
if (uri.startsWith(config.url + '/')) {
|
|
|
|
const id = new mongo.ObjectID(uri.split('/').pop());
|
2018-10-29 07:38:09 -05:00
|
|
|
const [user, note] = await Promise.all([
|
2018-10-07 06:20:55 -05:00
|
|
|
User.findOne({ _id: id }),
|
|
|
|
Note.findOne({ _id: id })
|
|
|
|
]);
|
|
|
|
|
|
|
|
const packed = await mergePack(user, note);
|
|
|
|
if (packed !== null) return packed;
|
|
|
|
}
|
|
|
|
|
2019-03-12 21:21:16 -05:00
|
|
|
// ブロックしてたら中断
|
|
|
|
const instance = await Instance.findOne({ host: extractDbHost(uri) });
|
|
|
|
if (instance && instance.isBlocked) return null;
|
|
|
|
|
2018-10-07 06:20:55 -05:00
|
|
|
// URI(AP Object id)としてDB検索
|
|
|
|
{
|
2018-10-29 07:38:09 -05:00
|
|
|
const [user, note] = await Promise.all([
|
2018-10-07 06:20:55 -05:00
|
|
|
User.findOne({ uri: uri }),
|
|
|
|
Note.findOne({ uri: uri })
|
|
|
|
]);
|
|
|
|
|
|
|
|
const packed = await mergePack(user, note);
|
|
|
|
if (packed !== null) return packed;
|
|
|
|
}
|
|
|
|
|
|
|
|
// リモートから一旦オブジェクトフェッチ
|
|
|
|
const resolver = new Resolver();
|
|
|
|
const object = await resolver.resolve(uri) as any;
|
|
|
|
|
|
|
|
// /@user のような正規id以外で取得できるURIが指定されていた場合、ここで初めて正規URIが確定する
|
|
|
|
// これはDBに存在する可能性があるため再度DB検索
|
|
|
|
if (uri !== object.id) {
|
2018-10-29 07:38:09 -05:00
|
|
|
const [user, note] = await Promise.all([
|
2018-10-07 06:20:55 -05:00
|
|
|
User.findOne({ uri: object.id }),
|
|
|
|
Note.findOne({ uri: object.id })
|
|
|
|
]);
|
|
|
|
|
|
|
|
const packed = await mergePack(user, note);
|
|
|
|
if (packed !== null) return packed;
|
|
|
|
}
|
|
|
|
|
|
|
|
// それでもみつからなければ新規であるため登録
|
|
|
|
if (object.type === 'Person') {
|
|
|
|
const user = await createPerson(object.id);
|
|
|
|
return {
|
|
|
|
type: 'User',
|
2018-11-20 06:18:14 -06:00
|
|
|
object: await packUser(user, null, { detail: true })
|
2018-10-07 06:20:55 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-03-14 10:23:24 -05:00
|
|
|
if (['Note', 'Question', 'Article'].includes(object.type)) {
|
2018-10-07 06:20:55 -05:00
|
|
|
const note = await createNote(object.id);
|
|
|
|
return {
|
|
|
|
type: 'Note',
|
2018-11-20 06:18:14 -06:00
|
|
|
object: await packNote(note, null, { detail: true })
|
2018-10-07 06:20:55 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function mergePack(user: IUser, note: INote) {
|
|
|
|
if (user !== null) {
|
|
|
|
return {
|
|
|
|
type: 'User',
|
|
|
|
object: await packUser(user, null, { detail: true })
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (note !== null) {
|
|
|
|
return {
|
|
|
|
type: 'Note',
|
|
|
|
object: await packNote(note, null, { detail: true })
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|