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