refactor(backend): extract clip-related logics to ClipService
This commit is contained in:
@@ -6,11 +6,7 @@
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import ms from 'ms';
|
||||
import { Endpoint } from '@/server/api/endpoint-base.js';
|
||||
import { IdService } from '@/core/IdService.js';
|
||||
import { DI } from '@/di-symbols.js';
|
||||
import type { ClipNotesRepository, ClipsRepository, NotesRepository } from '@/models/_.js';
|
||||
import { GetterService } from '@/server/api/GetterService.js';
|
||||
import { RoleService } from '@/core/RoleService.js';
|
||||
import { ClipService } from '@/core/ClipService.js';
|
||||
import { ApiError } from '../../error.js';
|
||||
|
||||
export const meta = {
|
||||
@@ -66,63 +62,22 @@ export const paramDef = {
|
||||
@Injectable()
|
||||
export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-disable-line import/no-default-export
|
||||
constructor(
|
||||
@Inject(DI.clipsRepository)
|
||||
private clipsRepository: ClipsRepository,
|
||||
|
||||
@Inject(DI.clipNotesRepository)
|
||||
private clipNotesRepository: ClipNotesRepository,
|
||||
|
||||
@Inject(DI.notesRepository)
|
||||
private notesRepository: NotesRepository,
|
||||
|
||||
private idService: IdService,
|
||||
private roleService: RoleService,
|
||||
private getterService: GetterService,
|
||||
private clipService: ClipService,
|
||||
) {
|
||||
super(meta, paramDef, async (ps, me) => {
|
||||
const clip = await this.clipsRepository.findOneBy({
|
||||
id: ps.clipId,
|
||||
userId: me.id,
|
||||
});
|
||||
|
||||
if (clip == null) {
|
||||
throw new ApiError(meta.errors.noSuchClip);
|
||||
try {
|
||||
await this.clipService.addNote(me, ps.clipId, ps.noteId);
|
||||
} catch (e) {
|
||||
if (e instanceof ClipService.NoSuchClipError) {
|
||||
throw new ApiError(meta.errors.noSuchClip);
|
||||
} else if (e instanceof ClipService.AlreadyAddedError) {
|
||||
throw new ApiError(meta.errors.alreadyClipped);
|
||||
} else if (e instanceof ClipService.TooManyClipNotesError) {
|
||||
throw new ApiError(meta.errors.tooManyClipNotes);
|
||||
} else {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
const note = await this.getterService.getNote(ps.noteId).catch(e => {
|
||||
if (e.id === '9725d0ce-ba28-4dde-95a7-2cbb2c15de24') throw new ApiError(meta.errors.noSuchNote);
|
||||
throw e;
|
||||
});
|
||||
|
||||
const exist = await this.clipNotesRepository.exist({
|
||||
where: {
|
||||
noteId: note.id,
|
||||
clipId: clip.id,
|
||||
},
|
||||
});
|
||||
|
||||
if (exist) {
|
||||
throw new ApiError(meta.errors.alreadyClipped);
|
||||
}
|
||||
|
||||
const currentCount = await this.clipNotesRepository.countBy({
|
||||
clipId: clip.id,
|
||||
});
|
||||
if (currentCount > (await this.roleService.getUserPolicies(me.id)).noteEachClipsLimit) {
|
||||
throw new ApiError(meta.errors.tooManyClipNotes);
|
||||
}
|
||||
|
||||
await this.clipNotesRepository.insert({
|
||||
id: this.idService.genId(),
|
||||
noteId: note.id,
|
||||
clipId: clip.id,
|
||||
});
|
||||
|
||||
this.clipsRepository.update(clip.id, {
|
||||
lastClippedAt: new Date(),
|
||||
});
|
||||
|
||||
this.notesRepository.increment({ id: note.id }, 'clippedCount', 1);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@@ -5,12 +5,10 @@
|
||||
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { Endpoint } from '@/server/api/endpoint-base.js';
|
||||
import { IdService } from '@/core/IdService.js';
|
||||
import type { ClipsRepository } from '@/models/_.js';
|
||||
import type { MiClip } from '@/models/_.js';
|
||||
import { ClipEntityService } from '@/core/entities/ClipEntityService.js';
|
||||
import { DI } from '@/di-symbols.js';
|
||||
import { RoleService } from '@/core/RoleService.js';
|
||||
import { ApiError } from '@/server/api/error.js';
|
||||
import { ClipService } from '@/core/ClipService.js';
|
||||
|
||||
export const meta = {
|
||||
tags: ['clips'],
|
||||
@@ -49,30 +47,19 @@ export const paramDef = {
|
||||
@Injectable()
|
||||
export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-disable-line import/no-default-export
|
||||
constructor(
|
||||
@Inject(DI.clipsRepository)
|
||||
private clipsRepository: ClipsRepository,
|
||||
|
||||
private clipEntityService: ClipEntityService,
|
||||
private roleService: RoleService,
|
||||
private idService: IdService,
|
||||
private clipService: ClipService,
|
||||
) {
|
||||
super(meta, paramDef, async (ps, me) => {
|
||||
const currentCount = await this.clipsRepository.countBy({
|
||||
userId: me.id,
|
||||
});
|
||||
if (currentCount > (await this.roleService.getUserPolicies(me.id)).clipLimit) {
|
||||
throw new ApiError(meta.errors.tooManyClips);
|
||||
let clip: MiClip;
|
||||
try {
|
||||
clip = await this.clipService.create(me, ps.name, ps.isPublic, ps.description ?? null);
|
||||
} catch (e) {
|
||||
if (e instanceof ClipService.TooManyClipsError) {
|
||||
throw new ApiError(meta.errors.tooManyClips);
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
|
||||
const clip = await this.clipsRepository.insert({
|
||||
id: this.idService.genId(),
|
||||
createdAt: new Date(),
|
||||
userId: me.id,
|
||||
name: ps.name,
|
||||
isPublic: ps.isPublic,
|
||||
description: ps.description,
|
||||
}).then(x => this.clipsRepository.findOneByOrFail(x.identifiers[0]));
|
||||
|
||||
return await this.clipEntityService.pack(clip, me);
|
||||
});
|
||||
}
|
||||
|
@@ -5,8 +5,7 @@
|
||||
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { Endpoint } from '@/server/api/endpoint-base.js';
|
||||
import type { ClipsRepository } from '@/models/_.js';
|
||||
import { DI } from '@/di-symbols.js';
|
||||
import { ClipService } from '@/core/ClipService.js';
|
||||
import { ApiError } from '../../error.js';
|
||||
|
||||
export const meta = {
|
||||
@@ -36,20 +35,17 @@ export const paramDef = {
|
||||
@Injectable()
|
||||
export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-disable-line import/no-default-export
|
||||
constructor(
|
||||
@Inject(DI.clipsRepository)
|
||||
private clipsRepository: ClipsRepository,
|
||||
private clipService: ClipService,
|
||||
) {
|
||||
super(meta, paramDef, async (ps, me) => {
|
||||
const clip = await this.clipsRepository.findOneBy({
|
||||
id: ps.clipId,
|
||||
userId: me.id,
|
||||
});
|
||||
|
||||
if (clip == null) {
|
||||
throw new ApiError(meta.errors.noSuchClip);
|
||||
try {
|
||||
await this.clipService.delete(me, ps.clipId);
|
||||
} catch (e) {
|
||||
if (e instanceof ClipService.NoSuchClipError) {
|
||||
throw new ApiError(meta.errors.noSuchClip);
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
|
||||
await this.clipsRepository.delete(clip.id);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@@ -5,9 +5,7 @@
|
||||
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { Endpoint } from '@/server/api/endpoint-base.js';
|
||||
import type { ClipNotesRepository, ClipsRepository, NotesRepository } from '@/models/_.js';
|
||||
import { DI } from '@/di-symbols.js';
|
||||
import { GetterService } from '@/server/api/GetterService.js';
|
||||
import { ClipService } from '@/core/ClipService.js';
|
||||
import { ApiError } from '../../error.js';
|
||||
|
||||
export const meta = {
|
||||
@@ -46,38 +44,17 @@ export const paramDef = {
|
||||
@Injectable()
|
||||
export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-disable-line import/no-default-export
|
||||
constructor(
|
||||
@Inject(DI.clipsRepository)
|
||||
private clipsRepository: ClipsRepository,
|
||||
|
||||
@Inject(DI.clipNotesRepository)
|
||||
private clipNotesRepository: ClipNotesRepository,
|
||||
|
||||
@Inject(DI.notesRepository)
|
||||
private notesRepository: NotesRepository,
|
||||
|
||||
private getterService: GetterService,
|
||||
private clipService: ClipService,
|
||||
) {
|
||||
super(meta, paramDef, async (ps, me) => {
|
||||
const clip = await this.clipsRepository.findOneBy({
|
||||
id: ps.clipId,
|
||||
userId: me.id,
|
||||
});
|
||||
|
||||
if (clip == null) {
|
||||
throw new ApiError(meta.errors.noSuchClip);
|
||||
try {
|
||||
await this.clipService.removeNote(me, ps.clipId, ps.noteId);
|
||||
} catch (e) {
|
||||
if (e instanceof ClipService.NoSuchClipError) {
|
||||
throw new ApiError(meta.errors.noSuchClip);
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
|
||||
const note = await this.getterService.getNote(ps.noteId).catch(err => {
|
||||
if (err.id === '9725d0ce-ba28-4dde-95a7-2cbb2c15de24') throw new ApiError(meta.errors.noSuchNote);
|
||||
throw err;
|
||||
});
|
||||
|
||||
await this.clipNotesRepository.delete({
|
||||
noteId: note.id,
|
||||
clipId: clip.id,
|
||||
});
|
||||
|
||||
this.notesRepository.decrement({ id: note.id }, 'clippedCount', 1);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@@ -5,9 +5,8 @@
|
||||
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { Endpoint } from '@/server/api/endpoint-base.js';
|
||||
import type { ClipsRepository } from '@/models/_.js';
|
||||
import { ClipEntityService } from '@/core/entities/ClipEntityService.js';
|
||||
import { DI } from '@/di-symbols.js';
|
||||
import { ClipService } from '@/core/ClipService.js';
|
||||
import { ApiError } from '../../error.js';
|
||||
|
||||
export const meta = {
|
||||
@@ -48,29 +47,21 @@ export const paramDef = {
|
||||
@Injectable()
|
||||
export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-disable-line import/no-default-export
|
||||
constructor(
|
||||
@Inject(DI.clipsRepository)
|
||||
private clipsRepository: ClipsRepository,
|
||||
private clipService: ClipService,
|
||||
|
||||
private clipEntityService: ClipEntityService,
|
||||
) {
|
||||
super(meta, paramDef, async (ps, me) => {
|
||||
// Fetch the clip
|
||||
const clip = await this.clipsRepository.findOneBy({
|
||||
id: ps.clipId,
|
||||
userId: me.id,
|
||||
});
|
||||
|
||||
if (clip == null) {
|
||||
throw new ApiError(meta.errors.noSuchClip);
|
||||
try {
|
||||
await this.clipService.update(me, ps.clipId, ps.name, ps.isPublic, ps.description);
|
||||
} catch (e) {
|
||||
if (e instanceof ClipService.NoSuchClipError) {
|
||||
throw new ApiError(meta.errors.noSuchClip);
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
|
||||
await this.clipsRepository.update(clip.id, {
|
||||
name: ps.name,
|
||||
description: ps.description,
|
||||
isPublic: ps.isPublic,
|
||||
});
|
||||
|
||||
return await this.clipEntityService.pack(clip.id, me);
|
||||
return await this.clipEntityService.pack(ps.clipId, me);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user