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