misskey/packages/backend/src/core/ClipService.ts

159 lines
4.2 KiB
TypeScript
Raw Normal View History

/*
* SPDX-FileCopyrightText: syuilo and misskey-project
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { Inject, Injectable } from '@nestjs/common';
2023-09-20 04:36:39 +02:00
import { QueryFailedError } from 'typeorm';
import { DI } from '@/di-symbols.js';
import type { ClipsRepository, MiNote, MiClip, ClipNotesRepository, NotesRepository } from '@/models/_.js';
import { bindThis } from '@/decorators.js';
import { isDuplicateKeyValueError } from '@/misc/is-duplicate-key-value-error.js';
import { RoleService } from '@/core/RoleService.js';
import { IdService } from '@/core/IdService.js';
import type { MiLocalUser } from '@/models/User.js';
@Injectable()
export class ClipService {
2023-09-20 04:29:10 +02:00
public static NoSuchNoteError = class extends Error {};
public static NoSuchClipError = class extends Error {};
public static AlreadyAddedError = class extends Error {};
public static TooManyClipNotesError = class extends Error {};
public static TooManyClipsError = class extends Error {};
constructor(
@Inject(DI.clipsRepository)
private clipsRepository: ClipsRepository,
@Inject(DI.clipNotesRepository)
private clipNotesRepository: ClipNotesRepository,
@Inject(DI.notesRepository)
private notesRepository: NotesRepository,
private roleService: RoleService,
private idService: IdService,
) {
}
@bindThis
public async create(me: MiLocalUser, name: string, isPublic: boolean, description: string | null): Promise<MiClip> {
const currentCount = await this.clipsRepository.countBy({
userId: me.id,
});
if (currentCount >= (await this.roleService.getUserPolicies(me.id)).clipLimit) {
throw new ClipService.TooManyClipsError();
}
fix(backend): use insertOne insteadof insert/findOneOrFail combination (#13908) * fix(backend): use insertOne insteadof insert/findOneOrFail combination * fix: typo * fix(backend): inherit mainAlias? * refactor(backend): use extend * fix(backend): invalid entityTarget * fix(backend): fake where * chore: debug * chore: debug * test: log * fix(backend): column names * fix(backend): remove dummy from * revert: log * fix(backend): position * fix(backend): automatic aliasing * chore(backend): alias * chore(backend): remove from * fix(backend): type * fix(backend): avoid pure name * test(backend): fix type * chore(backend): use cte * fix(backend): avoid useless alias * fix(backend): fix typo * fix(backend): __disambiguation__ * fix(backend): quote * chore(backend): t * chore(backend): accessible * chore(backend): concrete returning * fix(backend): quote * chore: log more * chore: log metadata * chore(backend): use raw * fix(backend): returning column name * fix(backend): transform * build(backend): wanna logging * build(backend): transform empty * build(backend): build alias * build(backend): restore name * chore: return entity * fix: test case * test(backend): 204 * chore(backend): log sql * chore(backend): assert user joined * fix(backend): typo * chore(backend): log long sql * chore(backend): log join * chore(backend): log join depth null * chore(backend): joinAttributes * chore(backend): override createJoinExpression * chore: join log * fix(backend): escape * test(backend): log log * chore(backend): join gonna success? * chore(backend): relations * chore(backend): undefined * chore(backend): target * chore(backend): remove log * chore(backend): log chart update * chore(backend): log columns * chore(backend): check hasMetadata * chore(backend): unshift id when not included * chore(backend): missing select * chore(backend): remove debug code
2024-06-01 04:16:44 +02:00
const clip = await this.clipsRepository.insertOne({
id: this.idService.gen(),
userId: me.id,
name: name,
isPublic: isPublic,
description: description,
fix(backend): use insertOne insteadof insert/findOneOrFail combination (#13908) * fix(backend): use insertOne insteadof insert/findOneOrFail combination * fix: typo * fix(backend): inherit mainAlias? * refactor(backend): use extend * fix(backend): invalid entityTarget * fix(backend): fake where * chore: debug * chore: debug * test: log * fix(backend): column names * fix(backend): remove dummy from * revert: log * fix(backend): position * fix(backend): automatic aliasing * chore(backend): alias * chore(backend): remove from * fix(backend): type * fix(backend): avoid pure name * test(backend): fix type * chore(backend): use cte * fix(backend): avoid useless alias * fix(backend): fix typo * fix(backend): __disambiguation__ * fix(backend): quote * chore(backend): t * chore(backend): accessible * chore(backend): concrete returning * fix(backend): quote * chore: log more * chore: log metadata * chore(backend): use raw * fix(backend): returning column name * fix(backend): transform * build(backend): wanna logging * build(backend): transform empty * build(backend): build alias * build(backend): restore name * chore: return entity * fix: test case * test(backend): 204 * chore(backend): log sql * chore(backend): assert user joined * fix(backend): typo * chore(backend): log long sql * chore(backend): log join * chore(backend): log join depth null * chore(backend): joinAttributes * chore(backend): override createJoinExpression * chore: join log * fix(backend): escape * test(backend): log log * chore(backend): join gonna success? * chore(backend): relations * chore(backend): undefined * chore(backend): target * chore(backend): remove log * chore(backend): log chart update * chore(backend): log columns * chore(backend): check hasMetadata * chore(backend): unshift id when not included * chore(backend): missing select * chore(backend): remove debug code
2024-06-01 04:16:44 +02:00
});
return clip;
}
@bindThis
public async update(me: MiLocalUser, clipId: MiClip['id'], name: string | undefined, isPublic: boolean | undefined, description: string | null | undefined): Promise<void> {
const clip = await this.clipsRepository.findOneBy({
id: clipId,
userId: me.id,
});
if (clip == null) {
throw new ClipService.NoSuchClipError();
}
await this.clipsRepository.update(clip.id, {
name: name,
description: description,
isPublic: isPublic,
});
}
@bindThis
public async delete(me: MiLocalUser, clipId: MiClip['id']): Promise<void> {
const clip = await this.clipsRepository.findOneBy({
id: clipId,
userId: me.id,
});
if (clip == null) {
throw new ClipService.NoSuchClipError();
}
await this.clipsRepository.delete(clip.id);
}
@bindThis
public async addNote(me: MiLocalUser, clipId: MiClip['id'], noteId: MiNote['id']): Promise<void> {
const clip = await this.clipsRepository.findOneBy({
id: clipId,
userId: me.id,
});
if (clip == null) {
throw new ClipService.NoSuchClipError();
}
const currentCount = await this.clipNotesRepository.countBy({
clipId: clip.id,
});
if (currentCount >= (await this.roleService.getUserPolicies(me.id)).noteEachClipsLimit) {
throw new ClipService.TooManyClipNotesError();
}
try {
await this.clipNotesRepository.insert({
id: this.idService.gen(),
noteId: noteId,
clipId: clip.id,
});
2023-09-20 04:36:39 +02:00
} catch (e: unknown) {
if (e instanceof QueryFailedError) {
if (isDuplicateKeyValueError(e)) {
throw new ClipService.AlreadyAddedError();
} else if (e.driverError.detail.includes('is not present in table "note".')) {
throw new ClipService.NoSuchNoteError();
}
}
2023-09-20 04:29:10 +02:00
throw e;
}
this.clipsRepository.update(clip.id, {
lastClippedAt: new Date(),
});
this.notesRepository.increment({ id: noteId }, 'clippedCount', 1);
}
@bindThis
public async removeNote(me: MiLocalUser, clipId: MiClip['id'], noteId: MiNote['id']): Promise<void> {
const clip = await this.clipsRepository.findOneBy({
id: clipId,
userId: me.id,
});
if (clip == null) {
throw new ClipService.NoSuchClipError();
}
2023-09-20 04:43:33 +02:00
const note = await this.notesRepository.findOneBy({ id: noteId });
if (note == null) {
throw new ClipService.NoSuchNoteError();
}
await this.clipNotesRepository.delete({
noteId: noteId,
clipId: clip.id,
});
this.notesRepository.decrement({ id: noteId }, 'clippedCount', 1);
}
}