2022-09-17 21:27:08 +03:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
2021-11-12 12:47:04 +02:00
|
|
|
import ms from 'ms';
|
2022-09-17 21:27:08 +03:00
|
|
|
import { Endpoint } from '@/server/api/endpoint-base.js';
|
2022-09-20 23:33:11 +03:00
|
|
|
import type { UsersRepository, NotesRepository } from '@/models/index.js';
|
2022-09-17 21:27:08 +03:00
|
|
|
import type { Note } from '@/models/entities/Note.js';
|
|
|
|
import type { CacheableLocalUser, User } from '@/models/entities/User.js';
|
|
|
|
import { isActor, isPost, getApId } from '@/core/remote/activitypub/type.js';
|
|
|
|
import type { SchemaType } from '@/misc/schema.js';
|
|
|
|
import { ApResolverService } from '@/core/remote/activitypub/ApResolverService.js';
|
|
|
|
import { ApDbResolverService } from '@/core/remote/activitypub/ApDbResolverService.js';
|
|
|
|
import { MetaService } from '@/core/MetaService.js';
|
|
|
|
import { ApPersonService } from '@/core/remote/activitypub/models/ApPersonService.js';
|
|
|
|
import { ApNoteService } from '@/core/remote/activitypub/models/ApNoteService.js';
|
|
|
|
import { UserEntityService } from '@/core/entities/UserEntityService.js';
|
|
|
|
import { NoteEntityService } from '@/core/entities/NoteEntityService.js';
|
|
|
|
import { UtilityService } from '@/core/UtilityService.js';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
|
|
|
import { ApiError } from '../../error.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',
|
2022-09-17 21:27:08 +03:00
|
|
|
},
|
|
|
|
},
|
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',
|
2022-09-17 21:27:08 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-01-18 15:27:10 +02:00
|
|
|
],
|
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-09-17 21:27:08 +03:00
|
|
|
@Injectable()
|
|
|
|
export default class extends Endpoint<typeof meta, typeof paramDef> {
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.usersRepository)
|
|
|
|
private usersRepository: UsersRepository,
|
|
|
|
|
|
|
|
@Inject(DI.notesRepository)
|
|
|
|
private notesRepository: NotesRepository,
|
|
|
|
|
|
|
|
private utilityService: UtilityService,
|
|
|
|
private userEntityService: UserEntityService,
|
|
|
|
private noteEntityService: NoteEntityService,
|
|
|
|
private metaService: MetaService,
|
|
|
|
private apResolverService: ApResolverService,
|
|
|
|
private apDbResolverService: ApDbResolverService,
|
|
|
|
private apPersonService: ApPersonService,
|
|
|
|
private apNoteService: ApNoteService,
|
|
|
|
) {
|
|
|
|
super(meta, paramDef, async (ps, me) => {
|
2022-09-18 21:11:50 +03:00
|
|
|
const object = await this.fetchAny(ps.uri, me);
|
2022-09-17 21:27:08 +03:00
|
|
|
if (object) {
|
|
|
|
return object;
|
|
|
|
} else {
|
|
|
|
throw new ApiError(meta.errors.noSuchObject);
|
|
|
|
}
|
|
|
|
});
|
2019-02-22 04:46:58 +02:00
|
|
|
}
|
2018-10-07 14:20:55 +03:00
|
|
|
|
2022-09-17 21:27:08 +03:00
|
|
|
/***
|
|
|
|
* URIからUserかNoteを解決する
|
|
|
|
*/
|
2022-09-18 21:11:50 +03:00
|
|
|
private async fetchAny(uri: string, me: CacheableLocalUser | null | undefined): Promise<SchemaType<typeof meta['res']> | null> {
|
2019-03-13 04:21:16 +02:00
|
|
|
// ブロックしてたら中断
|
2022-09-17 21:27:08 +03:00
|
|
|
const fetchedMeta = await this.metaService.fetch();
|
|
|
|
if (fetchedMeta.blockedHosts.includes(this.utilityService.extractDbHost(uri))) return null;
|
|
|
|
|
2022-09-18 21:11:50 +03:00
|
|
|
let local = await this.mergePack(me, ...await Promise.all([
|
2022-09-17 21:27:08 +03:00
|
|
|
this.apDbResolverService.getUserFromApId(uri),
|
|
|
|
this.apDbResolverService.getNoteFromApId(uri),
|
2022-06-23 15:32:17 +03:00
|
|
|
]));
|
|
|
|
if (local != null) return local;
|
2018-10-07 14:20:55 +03:00
|
|
|
|
2022-09-17 21:27:08 +03:00
|
|
|
// リモートから一旦オブジェクトフェッチ
|
|
|
|
const resolver = this.apResolverService.createResolver();
|
|
|
|
const object = await resolver.resolve(uri) as any;
|
|
|
|
|
|
|
|
// /@user のような正規id以外で取得できるURIが指定されていた場合、ここで初めて正規URIが確定する
|
|
|
|
// これはDBに存在する可能性があるため再度DB検索
|
|
|
|
if (uri !== object.id) {
|
2022-09-18 21:11:50 +03:00
|
|
|
local = await this.mergePack(me, ...await Promise.all([
|
2022-09-17 21:27:08 +03:00
|
|
|
this.apDbResolverService.getUserFromApId(object.id),
|
|
|
|
this.apDbResolverService.getNoteFromApId(object.id),
|
|
|
|
]));
|
|
|
|
if (local != null) return local;
|
|
|
|
}
|
2018-10-07 14:20:55 +03:00
|
|
|
|
2022-09-18 21:11:50 +03:00
|
|
|
return await this.mergePack(
|
2022-09-17 21:27:08 +03:00
|
|
|
me,
|
|
|
|
isActor(object) ? await this.apPersonService.createPerson(getApId(object)) : null,
|
|
|
|
isPost(object) ? await this.apNoteService.createNote(getApId(object), undefined, true) : null,
|
|
|
|
);
|
|
|
|
}
|
2022-06-23 15:32:17 +03:00
|
|
|
|
2022-09-18 21:11:50 +03:00
|
|
|
private async mergePack(me: CacheableLocalUser | null | undefined, user: User | null | undefined, note: Note | null | undefined): Promise<SchemaType<typeof meta.res> | null> {
|
2022-09-17 21:27:08 +03:00
|
|
|
if (user != null) {
|
2022-06-23 15:32:17 +03:00
|
|
|
return {
|
2022-09-17 21:27:08 +03:00
|
|
|
type: 'User',
|
|
|
|
object: await this.userEntityService.pack(user, me, { detail: true }),
|
2022-06-23 15:32:17 +03:00
|
|
|
};
|
2022-09-17 21:27:08 +03:00
|
|
|
} else if (note != null) {
|
|
|
|
try {
|
|
|
|
const object = await this.noteEntityService.pack(note, me, { detail: true });
|
|
|
|
|
|
|
|
return {
|
|
|
|
type: 'Note',
|
|
|
|
object,
|
|
|
|
};
|
|
|
|
} catch (e) {
|
|
|
|
return null;
|
|
|
|
}
|
2022-06-23 15:32:17 +03:00
|
|
|
}
|
2018-10-07 14:20:55 +03:00
|
|
|
|
2022-09-17 21:27:08 +03:00
|
|
|
return null;
|
|
|
|
}
|
2018-10-07 14:20:55 +03:00
|
|
|
}
|