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';
|
|
|
|
|
|
|
|
export const meta = {
|
|
|
|
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
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2018-12-20 09:09:02 -06:00
|
|
|
export default define(meta, (ps) => new Promise((res, rej) => {
|
|
|
|
fetchAny(ps.uri)
|
|
|
|
.then(object => object != null ? res(object) : rej('object not found'))
|
|
|
|
.catch(e => rej(e));
|
2018-11-01 23:47:44 -05:00
|
|
|
}));
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (object.type === 'Note') {
|
|
|
|
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;
|
|
|
|
}
|