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