2018-10-07 13:20:55 +02:00
|
|
|
import $ from 'cafy';
|
2018-11-02 05:47:44 +01:00
|
|
|
import define from '../../define';
|
2018-10-07 13:20:55 +02: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-22 03:46:58 +01:00
|
|
|
import { ApiError } from '../../error';
|
2018-10-07 13:20:55 +02:00
|
|
|
|
|
|
|
export const meta = {
|
2019-02-23 03:20:58 +01:00
|
|
|
tags: ['federation'],
|
|
|
|
|
2018-10-07 13:20:55 +02:00
|
|
|
desc: {
|
|
|
|
'ja-JP': 'URIを指定してActivityPubオブジェクトを参照します。'
|
|
|
|
},
|
|
|
|
|
|
|
|
requireCredential: false,
|
|
|
|
|
|
|
|
params: {
|
2018-11-01 19:32:24 +01:00
|
|
|
uri: {
|
|
|
|
validator: $.str,
|
2018-10-07 13:20:55 +02:00
|
|
|
desc: {
|
|
|
|
'ja-JP': 'ActivityPubオブジェクトのURI'
|
|
|
|
}
|
2018-11-01 19:32:24 +01:00
|
|
|
},
|
2018-10-07 13:20:55 +02:00
|
|
|
},
|
2019-02-22 03:46:58 +01:00
|
|
|
|
|
|
|
errors: {
|
|
|
|
noSuchObject: {
|
|
|
|
message: 'No such object.',
|
|
|
|
code: 'NO_SUCH_OBJECT',
|
|
|
|
id: 'dc94d745-1262-4e63-a17d-fecaa57efc82'
|
|
|
|
}
|
|
|
|
}
|
2018-10-07 13:20:55 +02:00
|
|
|
};
|
|
|
|
|
2019-02-22 03:46:58 +01: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 13:20:55 +02: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 13:38:09 +01:00
|
|
|
const [user, note] = await Promise.all([
|
2018-10-07 13:20:55 +02: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 13:38:09 +01:00
|
|
|
const [user, note] = await Promise.all([
|
2018-10-07 13:20:55 +02: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 13:38:09 +01:00
|
|
|
const [user, note] = await Promise.all([
|
2018-10-07 13:20:55 +02: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 13:18:14 +01:00
|
|
|
object: await packUser(user, null, { detail: true })
|
2018-10-07 13:20:55 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-03-06 14:55:47 +01:00
|
|
|
if (['Note', 'Question'].includes(object.type)) {
|
2018-10-07 13:20:55 +02:00
|
|
|
const note = await createNote(object.id);
|
|
|
|
return {
|
|
|
|
type: 'Note',
|
2018-11-20 13:18:14 +01:00
|
|
|
object: await packNote(note, null, { detail: true })
|
2018-10-07 13:20:55 +02: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;
|
|
|
|
}
|