2018-10-07 14:20:55 +03:00
|
|
|
import $ from 'cafy';
|
2018-11-02 06:47:44 +02:00
|
|
|
import define from '../../define';
|
2018-10-07 14:20:55 +03: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 20:32:24 +02:00
|
|
|
uri: {
|
|
|
|
validator: $.str,
|
2018-10-07 14:20:55 +03:00
|
|
|
desc: {
|
|
|
|
'ja-JP': 'ActivityPubオブジェクトのURI'
|
|
|
|
}
|
2018-11-01 20:32:24 +02:00
|
|
|
},
|
2018-10-07 14:20:55 +03:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2018-11-02 06:47:44 +02:00
|
|
|
export default define(meta, (ps) => new Promise(async (res, rej) => {
|
2018-10-07 14:20:55 +03:00
|
|
|
const object = await fetchAny(ps.uri);
|
2018-11-02 06:47:44 +02:00
|
|
|
if (object == null) return rej('object not found');
|
2018-10-07 14:20:55 +03:00
|
|
|
|
2018-11-02 06:47:44 +02:00
|
|
|
res(object);
|
|
|
|
}));
|
2018-10-07 14:20:55 +03: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 14:38:09 +02:00
|
|
|
const [user, note] = await Promise.all([
|
2018-10-07 14:20:55 +03: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 14:38:09 +02:00
|
|
|
const [user, note] = await Promise.all([
|
2018-10-07 14:20:55 +03: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 14:38:09 +02:00
|
|
|
const [user, note] = await Promise.all([
|
2018-10-07 14:20:55 +03: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 14:18:14 +02:00
|
|
|
object: await packUser(user, null, { detail: true })
|
2018-10-07 14:20:55 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (object.type === 'Note') {
|
|
|
|
const note = await createNote(object.id);
|
|
|
|
return {
|
|
|
|
type: 'Note',
|
2018-11-20 14:18:14 +02:00
|
|
|
object: await packNote(note, null, { detail: true })
|
2018-10-07 14:20:55 +03: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;
|
|
|
|
}
|