2023-07-27 14:31:52 +09:00
|
|
|
/*
|
2024-02-14 00:59:27 +09:00
|
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
2023-07-27 14:31:52 +09:00
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2023-09-15 14:28:29 +09:00
|
|
|
import type { NotesRepository, UsersRepository } from '@/models/_.js';
|
2022-09-18 03:27:08 +09:00
|
|
|
import { IdentifiableError } from '@/misc/identifiable-error.js';
|
2023-09-20 11:33:36 +09:00
|
|
|
import type { MiLocalUser, MiRemoteUser, MiUser } from '@/models/User.js';
|
|
|
|
import type { MiNote } from '@/models/Note.js';
|
2022-09-23 06:21:31 +09:00
|
|
|
import { UserEntityService } from '@/core/entities/UserEntityService.js';
|
2022-12-04 15:03:09 +09:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2022-09-18 03:27:08 +09:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class GetterService {
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.usersRepository)
|
|
|
|
private usersRepository: UsersRepository,
|
|
|
|
|
|
|
|
@Inject(DI.notesRepository)
|
|
|
|
private notesRepository: NotesRepository,
|
2022-09-23 06:21:31 +09:00
|
|
|
|
|
|
|
private userEntityService: UserEntityService,
|
2022-09-18 03:27:08 +09:00
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get note for API processing
|
|
|
|
*/
|
2022-12-04 15:03:09 +09:00
|
|
|
@bindThis
|
2023-08-16 17:51:28 +09:00
|
|
|
public async getNote(noteId: MiNote['id']) {
|
2022-09-18 03:27:08 +09:00
|
|
|
const note = await this.notesRepository.findOneBy({ id: noteId });
|
|
|
|
|
|
|
|
if (note == null) {
|
|
|
|
throw new IdentifiableError('9725d0ce-ba28-4dde-95a7-2cbb2c15de24', 'No such note.');
|
|
|
|
}
|
|
|
|
|
|
|
|
return note;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get user for API processing
|
|
|
|
*/
|
2022-12-04 15:03:09 +09:00
|
|
|
@bindThis
|
2023-08-16 17:51:28 +09:00
|
|
|
public async getUser(userId: MiUser['id']) {
|
2022-09-18 03:27:08 +09:00
|
|
|
const user = await this.usersRepository.findOneBy({ id: userId });
|
|
|
|
|
|
|
|
if (user == null) {
|
|
|
|
throw new IdentifiableError('15348ddd-432d-49c2-8a5a-8069753becff', 'No such user.');
|
|
|
|
}
|
|
|
|
|
2023-08-16 17:51:28 +09:00
|
|
|
return user as MiLocalUser | MiRemoteUser;
|
2022-09-18 03:27:08 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get remote user for API processing
|
|
|
|
*/
|
2022-12-04 15:03:09 +09:00
|
|
|
@bindThis
|
2023-08-16 17:51:28 +09:00
|
|
|
public async getRemoteUser(userId: MiUser['id']) {
|
2022-09-18 03:27:08 +09:00
|
|
|
const user = await this.getUser(userId);
|
|
|
|
|
|
|
|
if (!this.userEntityService.isRemoteUser(user)) {
|
|
|
|
throw new Error('user is not a remote user');
|
|
|
|
}
|
|
|
|
|
|
|
|
return user;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get local user for API processing
|
|
|
|
*/
|
2022-12-04 15:03:09 +09:00
|
|
|
@bindThis
|
2023-08-16 17:51:28 +09:00
|
|
|
public async getLocalUser(userId: MiUser['id']) {
|
2022-09-18 03:27:08 +09:00
|
|
|
const user = await this.getUser(userId);
|
|
|
|
|
|
|
|
if (!this.userEntityService.isLocalUser(user)) {
|
|
|
|
throw new Error('user is not a local user');
|
|
|
|
}
|
|
|
|
|
|
|
|
return user;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|