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 { createPerson } from '../../../../remote/activitypub/models/person';
|
|
|
|
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';
|
2019-03-13 03:21:16 +01:00
|
|
|
import { extractDbHost } from '../../../../misc/convert-host';
|
2019-04-07 14:50:36 +02:00
|
|
|
import { Users, Notes } from '../../../../models';
|
|
|
|
import { Note } from '../../../../models/entities/note';
|
|
|
|
import { User } from '../../../../models/entities/user';
|
2019-04-24 01:11:19 +02:00
|
|
|
import { fetchMeta } from '../../../../misc/fetch-meta';
|
2019-06-28 11:54:10 +02:00
|
|
|
import { validActor, validPost } from '../../../../remote/activitypub/type';
|
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: {
|
2021-03-06 14:34:11 +01:00
|
|
|
'ja-JP': 'URIを指定してActivityPubオブジェクトを参照します。',
|
|
|
|
'en-US': 'Browse to the ActivityPub object by specifying the URI.'
|
2018-10-07 13:20:55 +02:00
|
|
|
},
|
|
|
|
|
2020-02-15 13:33:32 +01:00
|
|
|
requireCredential: false as const,
|
2018-10-07 13:20:55 +02:00
|
|
|
|
|
|
|
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'
|
|
|
|
}
|
2021-03-06 14:34:11 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
res: {
|
|
|
|
type: 'object' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
|
|
|
properties: {
|
|
|
|
type: {
|
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
|
|
|
enum: ['User', 'Note']
|
|
|
|
},
|
|
|
|
object: {
|
|
|
|
type: 'object' as const,
|
|
|
|
optional: false as const, nullable: false as const
|
|
|
|
}
|
|
|
|
}
|
2019-02-22 03:46:58 +01:00
|
|
|
}
|
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 + '/')) {
|
2019-04-07 14:50:36 +02: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 13:20:55 +02:00
|
|
|
}
|
|
|
|
|
2019-03-13 03:21:16 +01:00
|
|
|
// ブロックしてたら中断
|
2019-04-07 14:50:36 +02:00
|
|
|
const meta = await fetchMeta();
|
|
|
|
if (meta.blockedHosts.includes(extractDbHost(uri))) return null;
|
2019-03-13 03:21:16 +01:00
|
|
|
|
2018-10-07 13:20:55 +02:00
|
|
|
// URI(AP Object id)としてDB検索
|
|
|
|
{
|
2018-10-29 13:38:09 +01:00
|
|
|
const [user, note] = await Promise.all([
|
2019-04-07 14:50:36 +02:00
|
|
|
Users.findOne({ uri: uri }),
|
|
|
|
Notes.findOne({ uri: uri })
|
2018-10-07 13:20:55 +02: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) {
|
2019-04-24 21:07:39 +02:00
|
|
|
if (object.id.startsWith(config.url + '/')) {
|
|
|
|
const parts = object.id.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-29 13:38:09 +01:00
|
|
|
const [user, note] = await Promise.all([
|
2019-04-07 14:50:36 +02:00
|
|
|
Users.findOne({ uri: object.id }),
|
|
|
|
Notes.findOne({ uri: object.id })
|
2018-10-07 13:20:55 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
const packed = await mergePack(user, note);
|
|
|
|
if (packed !== null) return packed;
|
|
|
|
}
|
|
|
|
|
|
|
|
// それでもみつからなければ新規であるため登録
|
2019-04-10 20:09:12 +02:00
|
|
|
if (validActor.includes(object.type)) {
|
2018-10-07 13:20:55 +02:00
|
|
|
const user = await createPerson(object.id);
|
|
|
|
return {
|
|
|
|
type: 'User',
|
2019-04-07 14:50:36 +02:00
|
|
|
object: await Users.pack(user, null, { detail: true })
|
2018-10-07 13:20:55 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-06-28 11:54:10 +02:00
|
|
|
if (validPost.includes(object.type)) {
|
2019-04-24 21:07:39 +02:00
|
|
|
const note = await createNote(object.id, undefined, true);
|
2018-10-07 13:20:55 +02:00
|
|
|
return {
|
|
|
|
type: 'Note',
|
2019-04-12 18:43:22 +02:00
|
|
|
object: await Notes.pack(note!, null, { detail: true })
|
2018-10-07 13:20:55 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-04-12 18:43:22 +02:00
|
|
|
async function mergePack(user: User | null | undefined, note: Note | null | undefined) {
|
2019-04-07 16:05:57 +02:00
|
|
|
if (user != null) {
|
2018-10-07 13:20:55 +02:00
|
|
|
return {
|
|
|
|
type: 'User',
|
2019-04-07 14:50:36 +02:00
|
|
|
object: await Users.pack(user, null, { detail: true })
|
2018-10-07 13:20:55 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-04-07 16:05:57 +02:00
|
|
|
if (note != null) {
|
2018-10-07 13:20:55 +02:00
|
|
|
return {
|
|
|
|
type: 'Note',
|
2019-04-07 14:50:36 +02:00
|
|
|
object: await Notes.pack(note, null, { detail: true })
|
2018-10-07 13:20:55 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|