2018-10-07 20:20:55 +09:00
|
|
|
import $ from 'cafy';
|
2018-11-02 13:47:44 +09:00
|
|
|
import define from '../../define';
|
2018-10-07 20:20:55 +09:00
|
|
|
import config from '../../../../config';
|
|
|
|
import { createPerson } from '../../../../remote/activitypub/models/person';
|
|
|
|
import { createNote } from '../../../../remote/activitypub/models/note';
|
|
|
|
import Resolver from '../../../../remote/activitypub/resolver';
|
2019-02-22 11:46:58 +09:00
|
|
|
import { ApiError } from '../../error';
|
2019-03-13 11:21:16 +09:00
|
|
|
import { extractDbHost } from '../../../../misc/convert-host';
|
2019-04-07 21:50:36 +09:00
|
|
|
import { Users, Notes } from '../../../../models';
|
|
|
|
import { Note } from '../../../../models/entities/note';
|
|
|
|
import { User } from '../../../../models/entities/user';
|
|
|
|
import fetchMeta from '../../../../misc/fetch-meta';
|
2018-10-07 20:20:55 +09:00
|
|
|
|
|
|
|
export const meta = {
|
2019-02-23 11:20:58 +09:00
|
|
|
tags: ['federation'],
|
|
|
|
|
2018-10-07 20:20:55 +09:00
|
|
|
desc: {
|
|
|
|
'ja-JP': 'URIを指定してActivityPubオブジェクトを参照します。'
|
|
|
|
},
|
|
|
|
|
|
|
|
requireCredential: false,
|
|
|
|
|
|
|
|
params: {
|
2018-11-02 03:32:24 +09:00
|
|
|
uri: {
|
|
|
|
validator: $.str,
|
2018-10-07 20:20:55 +09:00
|
|
|
desc: {
|
|
|
|
'ja-JP': 'ActivityPubオブジェクトのURI'
|
|
|
|
}
|
2018-11-02 03:32:24 +09:00
|
|
|
},
|
2018-10-07 20:20:55 +09:00
|
|
|
},
|
2019-02-22 11:46:58 +09:00
|
|
|
|
|
|
|
errors: {
|
|
|
|
noSuchObject: {
|
|
|
|
message: 'No such object.',
|
|
|
|
code: 'NO_SUCH_OBJECT',
|
|
|
|
id: 'dc94d745-1262-4e63-a17d-fecaa57efc82'
|
|
|
|
}
|
|
|
|
}
|
2018-10-07 20:20:55 +09:00
|
|
|
};
|
|
|
|
|
2019-02-22 11:46:58 +09: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 20:20:55 +09:00
|
|
|
|
|
|
|
/***
|
|
|
|
* URIからUserかNoteを解決する
|
|
|
|
*/
|
|
|
|
async function fetchAny(uri: string) {
|
|
|
|
// URIがこのサーバーを指しているなら、ローカルユーザーIDとしてDBからフェッチ
|
|
|
|
if (uri.startsWith(config.url + '/')) {
|
2019-04-07 21:50:36 +09:00
|
|
|
const parts = uri.split('/');
|
|
|
|
const id = parts.pop();
|
|
|
|
const type = parts.pop();
|
|
|
|
|
|
|
|
if (type === 'notes') {
|
|
|
|
const note = await Notes.findOne(id);
|
|
|
|
|
|
|
|
if (note) {
|
|
|
|
return {
|
|
|
|
type: 'Note',
|
|
|
|
object: await Notes.pack(note, null, { detail: true })
|
|
|
|
};
|
|
|
|
}
|
|
|
|
} else if (type === 'users') {
|
|
|
|
const user = await Users.findOne(id);
|
|
|
|
|
|
|
|
if (user) {
|
|
|
|
return {
|
|
|
|
type: 'User',
|
|
|
|
object: await Users.pack(user, null, { detail: true })
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2018-10-07 20:20:55 +09:00
|
|
|
}
|
|
|
|
|
2019-03-13 11:21:16 +09:00
|
|
|
// ブロックしてたら中断
|
2019-04-07 21:50:36 +09:00
|
|
|
const meta = await fetchMeta();
|
|
|
|
if (meta.blockedHosts.includes(extractDbHost(uri))) return null;
|
2019-03-13 11:21:16 +09:00
|
|
|
|
2018-10-07 20:20:55 +09:00
|
|
|
// URI(AP Object id)としてDB検索
|
|
|
|
{
|
2018-10-29 21:38:09 +09:00
|
|
|
const [user, note] = await Promise.all([
|
2019-04-07 21:50:36 +09:00
|
|
|
Users.findOne({ uri: uri }),
|
|
|
|
Notes.findOne({ uri: uri })
|
2018-10-07 20:20:55 +09:00
|
|
|
]);
|
|
|
|
|
|
|
|
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 21:38:09 +09:00
|
|
|
const [user, note] = await Promise.all([
|
2019-04-07 21:50:36 +09:00
|
|
|
Users.findOne({ uri: object.id }),
|
|
|
|
Notes.findOne({ uri: object.id })
|
2018-10-07 20:20:55 +09:00
|
|
|
]);
|
|
|
|
|
|
|
|
const packed = await mergePack(user, note);
|
|
|
|
if (packed !== null) return packed;
|
|
|
|
}
|
|
|
|
|
|
|
|
// それでもみつからなければ新規であるため登録
|
|
|
|
if (object.type === 'Person') {
|
|
|
|
const user = await createPerson(object.id);
|
|
|
|
return {
|
|
|
|
type: 'User',
|
2019-04-07 21:50:36 +09:00
|
|
|
object: await Users.pack(user, null, { detail: true })
|
2018-10-07 20:20:55 +09:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-03-15 00:23:24 +09:00
|
|
|
if (['Note', 'Question', 'Article'].includes(object.type)) {
|
2018-10-07 20:20:55 +09:00
|
|
|
const note = await createNote(object.id);
|
|
|
|
return {
|
|
|
|
type: 'Note',
|
2019-04-07 21:50:36 +09:00
|
|
|
object: await Notes.pack(note, null, { detail: true })
|
2018-10-07 20:20:55 +09:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
async function mergePack(user: User, note: Note) {
|
2018-10-07 20:20:55 +09:00
|
|
|
if (user !== null) {
|
|
|
|
return {
|
|
|
|
type: 'User',
|
2019-04-07 21:50:36 +09:00
|
|
|
object: await Users.pack(user, null, { detail: true })
|
2018-10-07 20:20:55 +09:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (note !== null) {
|
|
|
|
return {
|
|
|
|
type: 'Note',
|
2019-04-07 21:50:36 +09:00
|
|
|
object: await Notes.pack(note, null, { detail: true })
|
2018-10-07 20:20:55 +09:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|