2022-02-27 03:07:39 +01: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';
|
2022-06-23 14:32:17 +02:00
|
|
|
import DbResolver from '@/remote/activitypub/db-resolver.js';
|
2022-02-27 03:07:39 +01:00
|
|
|
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';
|
2022-06-23 14:32:17 +02:00
|
|
|
import { CacheableLocalUser, User } from '@/models/entities/user.js';
|
2022-02-27 03:07:39 +01:00
|
|
|
import { fetchMeta } from '@/misc/fetch-meta.js';
|
|
|
|
import { isActor, isPost, getApId } from '@/remote/activitypub/type.js';
|
|
|
|
import { SchemaType } from '@/misc/schema.js';
|
2022-07-14 00:01:23 +02:00
|
|
|
import { HOUR } from '@/const.js';
|
2018-10-07 13:20:55 +02:00
|
|
|
|
|
|
|
export const meta = {
|
2019-02-23 03:20:58 +01:00
|
|
|
tags: ['federation'],
|
|
|
|
|
2022-01-18 14:27:10 +01:00
|
|
|
requireCredential: true,
|
2021-10-08 07:05:07 +02:00
|
|
|
|
|
|
|
limit: {
|
2022-07-14 00:01:23 +02:00
|
|
|
duration: HOUR,
|
2021-12-09 15:58:30 +01:00
|
|
|
max: 30,
|
2021-10-08 07:05:07 +02: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',
|
2021-12-09 15:58:30 +01:00
|
|
|
id: 'dc94d745-1262-4e63-a17d-fecaa57efc82',
|
|
|
|
},
|
2021-03-06 14:34:11 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
res: {
|
2022-01-18 14:27:10 +01: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 15:58:30 +01:00
|
|
|
},
|
2022-01-18 14:27:10 +01: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 15:58:30 +01:00
|
|
|
},
|
2022-01-18 14:27:10 +01:00
|
|
|
} as const;
|
2018-10-07 13:20:55 +02:00
|
|
|
|
2022-02-20 05:15:40 +01:00
|
|
|
export const paramDef = {
|
2022-02-19 06:05:32 +01:00
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
uri: { type: 'string' },
|
|
|
|
},
|
|
|
|
required: ['uri'],
|
|
|
|
} as const;
|
|
|
|
|
2022-01-02 18:12:50 +01:00
|
|
|
// eslint-disable-next-line import/no-default-export
|
2022-06-23 14:32:17 +02:00
|
|
|
export default define(meta, paramDef, async (ps, me) => {
|
|
|
|
const object = await fetchAny(ps.uri, me);
|
2019-02-22 03:46:58 +01:00
|
|
|
if (object) {
|
|
|
|
return object;
|
|
|
|
} else {
|
|
|
|
throw new ApiError(meta.errors.noSuchObject);
|
|
|
|
}
|
|
|
|
});
|
2018-10-07 13:20:55 +02:00
|
|
|
|
|
|
|
/***
|
2022-12-07 00:54:06 +01:00
|
|
|
* Resolve User or Note from URI
|
2018-10-07 13:20:55 +02:00
|
|
|
*/
|
2022-06-23 14:32:17 +02:00
|
|
|
async function fetchAny(uri: string, me: CacheableLocalUser | null | undefined): Promise<SchemaType<typeof meta['res']> | null> {
|
2022-12-07 01:16:35 +01:00
|
|
|
// Wait if blocked.
|
2022-01-18 14:27:10 +01:00
|
|
|
const fetchedMeta = await fetchMeta();
|
|
|
|
if (fetchedMeta.blockedHosts.includes(extractDbHost(uri))) return null;
|
2019-03-13 03:21:16 +01:00
|
|
|
|
2022-06-23 14:32:17 +02:00
|
|
|
const dbResolver = new DbResolver();
|
2018-10-07 13:20:55 +02:00
|
|
|
|
2022-06-23 14:32:17 +02:00
|
|
|
let local = await mergePack(me, ...await Promise.all([
|
|
|
|
dbResolver.getUserFromApId(uri),
|
|
|
|
dbResolver.getNoteFromApId(uri),
|
|
|
|
]));
|
|
|
|
if (local != null) return local;
|
2018-10-07 13:20:55 +02:00
|
|
|
|
2022-12-07 01:16:35 +01:00
|
|
|
// fetching Object once from remote
|
2018-10-07 13:20:55 +02:00
|
|
|
const resolver = new Resolver();
|
|
|
|
const object = await resolver.resolve(uri) as any;
|
|
|
|
|
2022-12-07 01:16:35 +01:00
|
|
|
// /@user If a URI other than the id is specified,
|
|
|
|
// the URI is determined here
|
2018-10-07 13:20:55 +02:00
|
|
|
if (uri !== object.id) {
|
2022-06-23 14:32:17 +02:00
|
|
|
local = await mergePack(me, ...await Promise.all([
|
|
|
|
dbResolver.getUserFromApId(object.id),
|
|
|
|
dbResolver.getNoteFromApId(object.id),
|
|
|
|
]));
|
|
|
|
if (local != null) return local;
|
2018-10-07 13:20:55 +02:00
|
|
|
}
|
|
|
|
|
2022-06-23 14:32:17 +02:00
|
|
|
return await mergePack(
|
|
|
|
me,
|
|
|
|
isActor(object) ? await createPerson(getApId(object)) : null,
|
|
|
|
isPost(object) ? await createNote(getApId(object), undefined, true) : null,
|
|
|
|
);
|
2018-10-07 13:20:55 +02:00
|
|
|
}
|
|
|
|
|
2022-06-23 14:32:17 +02:00
|
|
|
async function mergePack(me: CacheableLocalUser | null | undefined, user: User | null | undefined, note: Note | null | undefined): Promise<SchemaType<typeof meta.res> | null> {
|
2019-04-07 16:05:57 +02:00
|
|
|
if (user != null) {
|
2018-10-07 13:20:55 +02:00
|
|
|
return {
|
|
|
|
type: 'User',
|
2022-06-23 14:32:17 +02:00
|
|
|
object: await Users.pack(user, me, { detail: true }),
|
2018-10-07 13:20:55 +02:00
|
|
|
};
|
2022-06-23 14:32:17 +02:00
|
|
|
} else if (note != null) {
|
|
|
|
try {
|
|
|
|
const object = await Notes.pack(note, me, { detail: true });
|
|
|
|
|
|
|
|
return {
|
|
|
|
type: 'Note',
|
|
|
|
object,
|
|
|
|
};
|
|
|
|
} catch (e) {
|
|
|
|
return null;
|
|
|
|
}
|
2018-10-07 13:20:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|