2022-09-17 18:27:08 +00:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
2021-11-12 10:47:04 +00:00
|
|
|
import ms from 'ms';
|
2022-09-17 18:27:08 +00:00
|
|
|
import { Endpoint } from '@/server/api/endpoint-base.js';
|
2022-09-20 20:33:11 +00:00
|
|
|
import type { UsersRepository, NotesRepository } from '@/models/index.js';
|
2022-09-17 18:27:08 +00:00
|
|
|
import type { Note } from '@/models/entities/Note.js';
|
|
|
|
import type { CacheableLocalUser, User } from '@/models/entities/User.js';
|
2022-12-04 01:16:03 +00:00
|
|
|
import { isActor, isPost, getApId } from '@/core/activitypub/type.js';
|
2022-09-17 18:27:08 +00:00
|
|
|
import type { SchemaType } from '@/misc/schema.js';
|
2022-12-04 01:16:03 +00:00
|
|
|
import { ApResolverService } from '@/core/activitypub/ApResolverService.js';
|
|
|
|
import { ApDbResolverService } from '@/core/activitypub/ApDbResolverService.js';
|
2022-09-17 18:27:08 +00:00
|
|
|
import { MetaService } from '@/core/MetaService.js';
|
2022-12-04 01:16:03 +00:00
|
|
|
import { ApPersonService } from '@/core/activitypub/models/ApPersonService.js';
|
|
|
|
import { ApNoteService } from '@/core/activitypub/models/ApNoteService.js';
|
2022-09-17 18:27:08 +00:00
|
|
|
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';
|
2022-12-04 06:03:09 +00:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2022-09-17 18:27:08 +00:00
|
|
|
import { ApiError } from '../../error.js';
|
2018-10-07 11:20:55 +00:00
|
|
|
|
|
|
|
export const meta = {
|
2019-02-23 02:20:58 +00:00
|
|
|
tags: ['federation'],
|
|
|
|
|
2022-01-18 13:27:10 +00:00
|
|
|
requireCredential: true,
|
2021-10-08 05:05:07 +00:00
|
|
|
|
|
|
|
limit: {
|
|
|
|
duration: ms('1hour'),
|
2021-12-09 14:58:30 +00:00
|
|
|
max: 30,
|
2021-10-08 05:05:07 +00:00
|
|
|
},
|
2018-10-07 11:20:55 +00:00
|
|
|
|
2019-02-22 02:46:58 +00:00
|
|
|
errors: {
|
|
|
|
noSuchObject: {
|
|
|
|
message: 'No such object.',
|
|
|
|
code: 'NO_SUCH_OBJECT',
|
2021-12-09 14:58:30 +00:00
|
|
|
id: 'dc94d745-1262-4e63-a17d-fecaa57efc82',
|
|
|
|
},
|
2021-03-06 13:34:11 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
res: {
|
2022-01-18 13:27:10 +00: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 18:27:08 +00:00
|
|
|
},
|
|
|
|
},
|
2021-12-09 14:58:30 +00:00
|
|
|
},
|
2022-01-18 13:27:10 +00: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 18:27:08 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-01-18 13:27:10 +00:00
|
|
|
],
|
2021-12-09 14:58:30 +00:00
|
|
|
},
|
2022-01-18 13:27:10 +00:00
|
|
|
} as const;
|
2018-10-07 11:20:55 +00:00
|
|
|
|
2022-02-20 04:15:40 +00:00
|
|
|
export const paramDef = {
|
2022-02-19 05:05:32 +00:00
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
uri: { type: 'string' },
|
|
|
|
},
|
|
|
|
required: ['uri'],
|
|
|
|
} as const;
|
|
|
|
|
2022-01-02 17:12:50 +00:00
|
|
|
// eslint-disable-next-line import/no-default-export
|
2022-09-17 18:27:08 +00: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 18:11:50 +00:00
|
|
|
const object = await this.fetchAny(ps.uri, me);
|
2022-09-17 18:27:08 +00:00
|
|
|
if (object) {
|
|
|
|
return object;
|
|
|
|
} else {
|
|
|
|
throw new ApiError(meta.errors.noSuchObject);
|
|
|
|
}
|
|
|
|
});
|
2019-02-22 02:46:58 +00:00
|
|
|
}
|
2018-10-07 11:20:55 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
/***
|
|
|
|
* URIからUserかNoteを解決する
|
|
|
|
*/
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2022-09-18 18:11:50 +00:00
|
|
|
private async fetchAny(uri: string, me: CacheableLocalUser | null | undefined): Promise<SchemaType<typeof meta['res']> | null> {
|
2019-03-13 02:21:16 +00:00
|
|
|
// ブロックしてたら中断
|
2022-09-17 18:27:08 +00:00
|
|
|
const fetchedMeta = await this.metaService.fetch();
|
|
|
|
if (fetchedMeta.blockedHosts.includes(this.utilityService.extractDbHost(uri))) return null;
|
|
|
|
|
2022-09-18 18:11:50 +00:00
|
|
|
let local = await this.mergePack(me, ...await Promise.all([
|
2022-09-17 18:27:08 +00:00
|
|
|
this.apDbResolverService.getUserFromApId(uri),
|
|
|
|
this.apDbResolverService.getNoteFromApId(uri),
|
2022-06-23 12:32:17 +00:00
|
|
|
]));
|
|
|
|
if (local != null) return local;
|
2018-10-07 11:20:55 +00:00
|
|
|
|
2022-09-17 18:27:08 +00: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 18:11:50 +00:00
|
|
|
local = await this.mergePack(me, ...await Promise.all([
|
2022-09-17 18:27:08 +00:00
|
|
|
this.apDbResolverService.getUserFromApId(object.id),
|
|
|
|
this.apDbResolverService.getNoteFromApId(object.id),
|
|
|
|
]));
|
|
|
|
if (local != null) return local;
|
|
|
|
}
|
2018-10-07 11:20:55 +00:00
|
|
|
|
2022-09-18 18:11:50 +00:00
|
|
|
return await this.mergePack(
|
2022-09-17 18:27:08 +00: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 12:32:17 +00:00
|
|
|
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2022-09-18 18:11:50 +00: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 18:27:08 +00:00
|
|
|
if (user != null) {
|
2022-06-23 12:32:17 +00:00
|
|
|
return {
|
2022-09-17 18:27:08 +00:00
|
|
|
type: 'User',
|
|
|
|
object: await this.userEntityService.pack(user, me, { detail: true }),
|
2022-06-23 12:32:17 +00:00
|
|
|
};
|
2022-09-17 18:27:08 +00: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 12:32:17 +00:00
|
|
|
}
|
2018-10-07 11:20:55 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
return null;
|
|
|
|
}
|
2018-10-07 11:20:55 +00:00
|
|
|
}
|