2022-02-27 04:07:39 +02:00
|
|
|
import define from '../../define.js';
|
|
|
|
import config from '@/config/index.js';
|
|
|
|
import { createPerson } from '@/remote/activitypub/models/person.js';
|
|
|
|
import { createNote } from '@/remote/activitypub/models/note.js';
|
|
|
|
import Resolver from '@/remote/activitypub/resolver.js';
|
|
|
|
import { ApiError } from '../../error.js';
|
|
|
|
import { extractDbHost } from '@/misc/convert-host.js';
|
|
|
|
import { Users, Notes } from '@/models/index.js';
|
|
|
|
import { Note } from '@/models/entities/note.js';
|
|
|
|
import { User } from '@/models/entities/user.js';
|
|
|
|
import { fetchMeta } from '@/misc/fetch-meta.js';
|
|
|
|
import { isActor, isPost, getApId } from '@/remote/activitypub/type.js';
|
2021-11-12 12:47:04 +02:00
|
|
|
import ms from 'ms';
|
2022-02-27 04:07:39 +02:00
|
|
|
import { SchemaType } from '@/misc/schema.js';
|
2018-10-07 14:20:55 +03:00
|
|
|
|
|
|
|
export const meta = {
|
2019-02-23 04:20:58 +02:00
|
|
|
tags: ['federation'],
|
|
|
|
|
2022-01-18 15:27:10 +02:00
|
|
|
requireCredential: true,
|
2021-10-08 08:05:07 +03:00
|
|
|
|
|
|
|
limit: {
|
|
|
|
duration: ms('1hour'),
|
2021-12-09 16:58:30 +02:00
|
|
|
max: 30,
|
2021-10-08 08:05:07 +03: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',
|
2021-12-09 16:58:30 +02:00
|
|
|
id: 'dc94d745-1262-4e63-a17d-fecaa57efc82',
|
|
|
|
},
|
2021-03-06 15:34:11 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
res: {
|
2022-01-18 15:27:10 +02:00
|
|
|
optional: false, nullable: false,
|
|
|
|
oneOf: [
|
|
|
|
{
|
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
type: {
|
|
|
|
type: 'string',
|
|
|
|
optional: false, nullable: false,
|
|
|
|
enum: ['User'],
|
|
|
|
},
|
|
|
|
object: {
|
|
|
|
type: 'object',
|
|
|
|
optional: false, nullable: false,
|
|
|
|
ref: 'UserDetailedNotMe',
|
|
|
|
}
|
|
|
|
}
|
2021-12-09 16:58:30 +02:00
|
|
|
},
|
2022-01-18 15:27:10 +02:00
|
|
|
{
|
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
type: {
|
|
|
|
type: 'string',
|
|
|
|
optional: false, nullable: false,
|
|
|
|
enum: ['Note'],
|
|
|
|
},
|
|
|
|
object: {
|
|
|
|
type: 'object',
|
|
|
|
optional: false, nullable: false,
|
|
|
|
ref: 'Note',
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
],
|
2021-12-09 16:58:30 +02:00
|
|
|
},
|
2022-01-18 15:27:10 +02:00
|
|
|
} as const;
|
2018-10-07 14:20:55 +03:00
|
|
|
|
2022-02-20 06:15:40 +02:00
|
|
|
export const paramDef = {
|
2022-02-19 07:05:32 +02:00
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
uri: { type: 'string' },
|
|
|
|
},
|
|
|
|
required: ['uri'],
|
|
|
|
} as const;
|
|
|
|
|
2022-01-02 19:12:50 +02:00
|
|
|
// eslint-disable-next-line import/no-default-export
|
2022-02-19 07:05:32 +02:00
|
|
|
export default define(meta, paramDef, async (ps) => {
|
2019-02-22 04:46:58 +02:00
|
|
|
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を解決する
|
|
|
|
*/
|
2022-01-18 15:27:10 +02:00
|
|
|
async function fetchAny(uri: string): Promise<SchemaType<typeof meta['res']> | null> {
|
2018-10-07 14:20:55 +03:00
|
|
|
// 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',
|
2021-12-09 16:58:30 +02:00
|
|
|
object: await Notes.pack(note, null, { detail: true }),
|
2019-04-07 15:50:36 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
} else if (type === 'users') {
|
|
|
|
const user = await Users.findOne(id);
|
|
|
|
|
|
|
|
if (user) {
|
|
|
|
return {
|
|
|
|
type: 'User',
|
2021-12-09 16:58:30 +02:00
|
|
|
object: await Users.pack(user, null, { detail: true }),
|
2019-04-07 15:50:36 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2018-10-07 14:20:55 +03:00
|
|
|
}
|
|
|
|
|
2019-03-13 04:21:16 +02:00
|
|
|
// ブロックしてたら中断
|
2022-01-18 15:27:10 +02:00
|
|
|
const fetchedMeta = await fetchMeta();
|
|
|
|
if (fetchedMeta.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 }),
|
2021-12-09 16:58:30 +02:00
|
|
|
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',
|
2021-12-09 16:58:30 +02:00
|
|
|
object: await Notes.pack(note, null, { detail: true }),
|
2019-04-24 22:07:39 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
} else if (type === 'users') {
|
|
|
|
const user = await Users.findOne(id);
|
|
|
|
|
|
|
|
if (user) {
|
|
|
|
return {
|
|
|
|
type: 'User',
|
2021-12-09 16:58:30 +02:00
|
|
|
object: await Users.pack(user, null, { detail: true }),
|
2019-04-24 22:07:39 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 }),
|
2021-12-09 16:58:30 +02:00
|
|
|
Notes.findOne({ uri: object.id }),
|
2018-10-07 14:20:55 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
const packed = await mergePack(user, note);
|
|
|
|
if (packed !== null) return packed;
|
|
|
|
}
|
|
|
|
|
|
|
|
// それでもみつからなければ新規であるため登録
|
2021-05-31 07:04:13 +03:00
|
|
|
if (isActor(object)) {
|
|
|
|
const user = await createPerson(getApId(object));
|
2018-10-07 14:20:55 +03:00
|
|
|
return {
|
|
|
|
type: 'User',
|
2021-12-09 16:58:30 +02:00
|
|
|
object: await Users.pack(user, null, { detail: true }),
|
2018-10-07 14:20:55 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-05-31 07:04:13 +03:00
|
|
|
if (isPost(object)) {
|
|
|
|
const note = await createNote(getApId(object), undefined, true);
|
2018-10-07 14:20:55 +03:00
|
|
|
return {
|
|
|
|
type: 'Note',
|
2021-12-09 16:58:30 +02:00
|
|
|
object: await Notes.pack(note!, null, { detail: true }),
|
2018-10-07 14:20:55 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-01-18 15:27:10 +02:00
|
|
|
async function mergePack(user: User | null | undefined, note: Note | null | undefined): Promise<SchemaType<typeof meta.res> | null> {
|
2019-04-07 17:05:57 +03:00
|
|
|
if (user != null) {
|
2018-10-07 14:20:55 +03:00
|
|
|
return {
|
|
|
|
type: 'User',
|
2021-12-09 16:58:30 +02: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',
|
2021-12-09 16:58:30 +02:00
|
|
|
object: await Notes.pack(note, null, { detail: true }),
|
2018-10-07 14:20:55 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|