2018-10-07 11:20:55 +00:00
|
|
|
import $ from 'cafy';
|
2018-11-02 04:47:44 +00:00
|
|
|
import define from '../../define';
|
2018-10-07 11:20:55 +00: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';
|
|
|
|
|
|
|
|
export const meta = {
|
|
|
|
desc: {
|
|
|
|
'ja-JP': 'URIを指定してActivityPubオブジェクトを参照します。'
|
|
|
|
},
|
|
|
|
|
|
|
|
requireCredential: false,
|
|
|
|
|
|
|
|
params: {
|
2018-11-01 18:32:24 +00:00
|
|
|
uri: {
|
|
|
|
validator: $.str,
|
2018-10-07 11:20:55 +00:00
|
|
|
desc: {
|
|
|
|
'ja-JP': 'ActivityPubオブジェクトのURI'
|
|
|
|
}
|
2018-11-01 18:32:24 +00:00
|
|
|
},
|
2018-10-07 11:20:55 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2018-11-02 04:47:44 +00:00
|
|
|
export default define(meta, (ps) => new Promise(async (res, rej) => {
|
2018-10-07 11:20:55 +00:00
|
|
|
const object = await fetchAny(ps.uri);
|
2018-11-02 04:47:44 +00:00
|
|
|
if (object == null) return rej('object not found');
|
2018-10-07 11:20:55 +00:00
|
|
|
|
2018-11-02 04:47:44 +00:00
|
|
|
res(object);
|
|
|
|
}));
|
2018-10-07 11:20:55 +00: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 12:38:09 +00:00
|
|
|
const [user, note] = await Promise.all([
|
2018-10-07 11:20:55 +00:00
|
|
|
User.findOne({ _id: id }),
|
|
|
|
Note.findOne({ _id: id })
|
|
|
|
]);
|
|
|
|
|
|
|
|
const packed = await mergePack(user, note);
|
|
|
|
if (packed !== null) return packed;
|
|
|
|
}
|
|
|
|
|
|
|
|
// URI(AP Object id)としてDB検索
|
|
|
|
{
|
2018-10-29 12:38:09 +00:00
|
|
|
const [user, note] = await Promise.all([
|
2018-10-07 11:20:55 +00: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 12:38:09 +00:00
|
|
|
const [user, note] = await Promise.all([
|
2018-10-07 11:20:55 +00: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',
|
|
|
|
object: user
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (object.type === 'Note') {
|
|
|
|
const note = await createNote(object.id);
|
|
|
|
return {
|
|
|
|
type: 'Note',
|
|
|
|
object: note
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|