mirror of
				https://codeberg.org/yeentown/barkey.git
				synced 2025-11-04 15:34:13 +00:00 
			
		
		
		
	refactor(backend): extract clip-related logics to ClipService
This commit is contained in:
		
							parent
							
								
									934e4be658
								
							
						
					
					
						commit
						bb0b2df37e
					
				
					 7 changed files with 211 additions and 147 deletions
				
			
		
							
								
								
									
										152
									
								
								packages/backend/src/core/ClipService.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										152
									
								
								packages/backend/src/core/ClipService.ts
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,152 @@
 | 
				
			||||||
 | 
					/*
 | 
				
			||||||
 | 
					 * SPDX-FileCopyrightText: syuilo and other misskey contributors
 | 
				
			||||||
 | 
					 * SPDX-License-Identifier: AGPL-3.0-only
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import { Inject, Injectable } from '@nestjs/common';
 | 
				
			||||||
 | 
					import * as Redis from 'ioredis';
 | 
				
			||||||
 | 
					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/entities/User.js';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@Injectable()
 | 
				
			||||||
 | 
					export class ClipService {
 | 
				
			||||||
 | 
						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.redis)
 | 
				
			||||||
 | 
							private redisClient: Redis.Redis,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							@Inject(DI.redisForSub)
 | 
				
			||||||
 | 
							private redisForSub: Redis.Redis,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							@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();
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							const clip = await this.clipsRepository.insert({
 | 
				
			||||||
 | 
								id: this.idService.genId(),
 | 
				
			||||||
 | 
								createdAt: new Date(),
 | 
				
			||||||
 | 
								userId: me.id,
 | 
				
			||||||
 | 
								name: name,
 | 
				
			||||||
 | 
								isPublic: isPublic,
 | 
				
			||||||
 | 
								description: description,
 | 
				
			||||||
 | 
							}).then(x => this.clipsRepository.findOneByOrFail(x.identifiers[0]));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							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.genId(),
 | 
				
			||||||
 | 
									noteId: noteId,
 | 
				
			||||||
 | 
									clipId: clip.id,
 | 
				
			||||||
 | 
								});
 | 
				
			||||||
 | 
							} catch (e) {
 | 
				
			||||||
 | 
								if (isDuplicateKeyValueError(e)) {
 | 
				
			||||||
 | 
									throw new ClipService.AlreadyAddedError();
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							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();
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							await this.clipNotesRepository.delete({
 | 
				
			||||||
 | 
								noteId: noteId,
 | 
				
			||||||
 | 
								clipId: clip.id,
 | 
				
			||||||
 | 
							});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							this.notesRepository.decrement({ id: noteId }, 'clippedCount', 1);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -57,6 +57,7 @@ import { ProxyAccountService } from './ProxyAccountService.js';
 | 
				
			||||||
import { UtilityService } from './UtilityService.js';
 | 
					import { UtilityService } from './UtilityService.js';
 | 
				
			||||||
import { FileInfoService } from './FileInfoService.js';
 | 
					import { FileInfoService } from './FileInfoService.js';
 | 
				
			||||||
import { SearchService } from './SearchService.js';
 | 
					import { SearchService } from './SearchService.js';
 | 
				
			||||||
 | 
					import { ClipService } from './ClipService.js';
 | 
				
			||||||
import { ChartLoggerService } from './chart/ChartLoggerService.js';
 | 
					import { ChartLoggerService } from './chart/ChartLoggerService.js';
 | 
				
			||||||
import FederationChart from './chart/charts/federation.js';
 | 
					import FederationChart from './chart/charts/federation.js';
 | 
				
			||||||
import NotesChart from './chart/charts/notes.js';
 | 
					import NotesChart from './chart/charts/notes.js';
 | 
				
			||||||
| 
						 | 
					@ -181,6 +182,7 @@ const $WebhookService: Provider = { provide: 'WebhookService', useExisting: Webh
 | 
				
			||||||
const $UtilityService: Provider = { provide: 'UtilityService', useExisting: UtilityService };
 | 
					const $UtilityService: Provider = { provide: 'UtilityService', useExisting: UtilityService };
 | 
				
			||||||
const $FileInfoService: Provider = { provide: 'FileInfoService', useExisting: FileInfoService };
 | 
					const $FileInfoService: Provider = { provide: 'FileInfoService', useExisting: FileInfoService };
 | 
				
			||||||
const $SearchService: Provider = { provide: 'SearchService', useExisting: SearchService };
 | 
					const $SearchService: Provider = { provide: 'SearchService', useExisting: SearchService };
 | 
				
			||||||
 | 
					const $ClipService: Provider = { provide: 'ClipService', useExisting: ClipService };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const $ChartLoggerService: Provider = { provide: 'ChartLoggerService', useExisting: ChartLoggerService };
 | 
					const $ChartLoggerService: Provider = { provide: 'ChartLoggerService', useExisting: ChartLoggerService };
 | 
				
			||||||
const $FederationChart: Provider = { provide: 'FederationChart', useExisting: FederationChart };
 | 
					const $FederationChart: Provider = { provide: 'FederationChart', useExisting: FederationChart };
 | 
				
			||||||
| 
						 | 
					@ -309,6 +311,7 @@ const $ApQuestionService: Provider = { provide: 'ApQuestionService', useExisting
 | 
				
			||||||
		UtilityService,
 | 
							UtilityService,
 | 
				
			||||||
		FileInfoService,
 | 
							FileInfoService,
 | 
				
			||||||
		SearchService,
 | 
							SearchService,
 | 
				
			||||||
 | 
							ClipService,
 | 
				
			||||||
		ChartLoggerService,
 | 
							ChartLoggerService,
 | 
				
			||||||
		FederationChart,
 | 
							FederationChart,
 | 
				
			||||||
		NotesChart,
 | 
							NotesChart,
 | 
				
			||||||
| 
						 | 
					@ -430,6 +433,7 @@ const $ApQuestionService: Provider = { provide: 'ApQuestionService', useExisting
 | 
				
			||||||
		$UtilityService,
 | 
							$UtilityService,
 | 
				
			||||||
		$FileInfoService,
 | 
							$FileInfoService,
 | 
				
			||||||
		$SearchService,
 | 
							$SearchService,
 | 
				
			||||||
 | 
							$ClipService,
 | 
				
			||||||
		$ChartLoggerService,
 | 
							$ChartLoggerService,
 | 
				
			||||||
		$FederationChart,
 | 
							$FederationChart,
 | 
				
			||||||
		$NotesChart,
 | 
							$NotesChart,
 | 
				
			||||||
| 
						 | 
					@ -552,6 +556,7 @@ const $ApQuestionService: Provider = { provide: 'ApQuestionService', useExisting
 | 
				
			||||||
		UtilityService,
 | 
							UtilityService,
 | 
				
			||||||
		FileInfoService,
 | 
							FileInfoService,
 | 
				
			||||||
		SearchService,
 | 
							SearchService,
 | 
				
			||||||
 | 
							ClipService,
 | 
				
			||||||
		FederationChart,
 | 
							FederationChart,
 | 
				
			||||||
		NotesChart,
 | 
							NotesChart,
 | 
				
			||||||
		UsersChart,
 | 
							UsersChart,
 | 
				
			||||||
| 
						 | 
					@ -672,6 +677,7 @@ const $ApQuestionService: Provider = { provide: 'ApQuestionService', useExisting
 | 
				
			||||||
		$UtilityService,
 | 
							$UtilityService,
 | 
				
			||||||
		$FileInfoService,
 | 
							$FileInfoService,
 | 
				
			||||||
		$SearchService,
 | 
							$SearchService,
 | 
				
			||||||
 | 
							$ClipService,
 | 
				
			||||||
		$FederationChart,
 | 
							$FederationChart,
 | 
				
			||||||
		$NotesChart,
 | 
							$NotesChart,
 | 
				
			||||||
		$UsersChart,
 | 
							$UsersChart,
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -6,11 +6,7 @@
 | 
				
			||||||
import { Inject, Injectable } from '@nestjs/common';
 | 
					import { Inject, Injectable } from '@nestjs/common';
 | 
				
			||||||
import ms from 'ms';
 | 
					import ms from 'ms';
 | 
				
			||||||
import { Endpoint } from '@/server/api/endpoint-base.js';
 | 
					import { Endpoint } from '@/server/api/endpoint-base.js';
 | 
				
			||||||
import { IdService } from '@/core/IdService.js';
 | 
					import { ClipService } from '@/core/ClipService.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 { ApiError } from '../../error.js';
 | 
					import { ApiError } from '../../error.js';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export const meta = {
 | 
					export const meta = {
 | 
				
			||||||
| 
						 | 
					@ -66,63 +62,22 @@ export const paramDef = {
 | 
				
			||||||
@Injectable()
 | 
					@Injectable()
 | 
				
			||||||
export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-disable-line import/no-default-export
 | 
					export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-disable-line import/no-default-export
 | 
				
			||||||
	constructor(
 | 
						constructor(
 | 
				
			||||||
		@Inject(DI.clipsRepository)
 | 
							private clipService: ClipService,
 | 
				
			||||||
		private clipsRepository: ClipsRepository,
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		@Inject(DI.clipNotesRepository)
 | 
					 | 
				
			||||||
		private clipNotesRepository: ClipNotesRepository,
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		@Inject(DI.notesRepository)
 | 
					 | 
				
			||||||
		private notesRepository: NotesRepository,
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		private idService: IdService,
 | 
					 | 
				
			||||||
		private roleService: RoleService,
 | 
					 | 
				
			||||||
		private getterService: GetterService,
 | 
					 | 
				
			||||||
	) {
 | 
						) {
 | 
				
			||||||
		super(meta, paramDef, async (ps, me) => {
 | 
							super(meta, paramDef, async (ps, me) => {
 | 
				
			||||||
			const clip = await this.clipsRepository.findOneBy({
 | 
								try {
 | 
				
			||||||
				id: ps.clipId,
 | 
									await this.clipService.addNote(me, ps.clipId, ps.noteId);
 | 
				
			||||||
				userId: me.id,
 | 
								} catch (e) {
 | 
				
			||||||
			});
 | 
									if (e instanceof ClipService.NoSuchClipError) {
 | 
				
			||||||
 | 
										throw new ApiError(meta.errors.noSuchClip);
 | 
				
			||||||
			if (clip == null) {
 | 
									} else if (e instanceof ClipService.AlreadyAddedError) {
 | 
				
			||||||
				throw new ApiError(meta.errors.noSuchClip);
 | 
										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 { Inject, Injectable } from '@nestjs/common';
 | 
				
			||||||
import { Endpoint } from '@/server/api/endpoint-base.js';
 | 
					import { Endpoint } from '@/server/api/endpoint-base.js';
 | 
				
			||||||
import { IdService } from '@/core/IdService.js';
 | 
					import type { MiClip } from '@/models/_.js';
 | 
				
			||||||
import type { ClipsRepository } from '@/models/_.js';
 | 
					 | 
				
			||||||
import { ClipEntityService } from '@/core/entities/ClipEntityService.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 { ApiError } from '@/server/api/error.js';
 | 
				
			||||||
 | 
					import { ClipService } from '@/core/ClipService.js';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export const meta = {
 | 
					export const meta = {
 | 
				
			||||||
	tags: ['clips'],
 | 
						tags: ['clips'],
 | 
				
			||||||
| 
						 | 
					@ -49,30 +47,19 @@ export const paramDef = {
 | 
				
			||||||
@Injectable()
 | 
					@Injectable()
 | 
				
			||||||
export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-disable-line import/no-default-export
 | 
					export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-disable-line import/no-default-export
 | 
				
			||||||
	constructor(
 | 
						constructor(
 | 
				
			||||||
		@Inject(DI.clipsRepository)
 | 
					 | 
				
			||||||
		private clipsRepository: ClipsRepository,
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		private clipEntityService: ClipEntityService,
 | 
							private clipEntityService: ClipEntityService,
 | 
				
			||||||
		private roleService: RoleService,
 | 
							private clipService: ClipService,
 | 
				
			||||||
		private idService: IdService,
 | 
					 | 
				
			||||||
	) {
 | 
						) {
 | 
				
			||||||
		super(meta, paramDef, async (ps, me) => {
 | 
							super(meta, paramDef, async (ps, me) => {
 | 
				
			||||||
			const currentCount = await this.clipsRepository.countBy({
 | 
								let clip: MiClip;
 | 
				
			||||||
				userId: me.id,
 | 
								try {
 | 
				
			||||||
			});
 | 
									clip = await this.clipService.create(me, ps.name, ps.isPublic, ps.description ?? null);
 | 
				
			||||||
			if (currentCount > (await this.roleService.getUserPolicies(me.id)).clipLimit) {
 | 
								} catch (e) {
 | 
				
			||||||
				throw new ApiError(meta.errors.tooManyClips);
 | 
									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);
 | 
								return await this.clipEntityService.pack(clip, me);
 | 
				
			||||||
		});
 | 
							});
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -5,8 +5,7 @@
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import { Inject, Injectable } from '@nestjs/common';
 | 
					import { Inject, Injectable } from '@nestjs/common';
 | 
				
			||||||
import { Endpoint } from '@/server/api/endpoint-base.js';
 | 
					import { Endpoint } from '@/server/api/endpoint-base.js';
 | 
				
			||||||
import type { ClipsRepository } from '@/models/_.js';
 | 
					import { ClipService } from '@/core/ClipService.js';
 | 
				
			||||||
import { DI } from '@/di-symbols.js';
 | 
					 | 
				
			||||||
import { ApiError } from '../../error.js';
 | 
					import { ApiError } from '../../error.js';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export const meta = {
 | 
					export const meta = {
 | 
				
			||||||
| 
						 | 
					@ -36,20 +35,17 @@ export const paramDef = {
 | 
				
			||||||
@Injectable()
 | 
					@Injectable()
 | 
				
			||||||
export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-disable-line import/no-default-export
 | 
					export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-disable-line import/no-default-export
 | 
				
			||||||
	constructor(
 | 
						constructor(
 | 
				
			||||||
		@Inject(DI.clipsRepository)
 | 
							private clipService: ClipService,
 | 
				
			||||||
		private clipsRepository: ClipsRepository,
 | 
					 | 
				
			||||||
	) {
 | 
						) {
 | 
				
			||||||
		super(meta, paramDef, async (ps, me) => {
 | 
							super(meta, paramDef, async (ps, me) => {
 | 
				
			||||||
			const clip = await this.clipsRepository.findOneBy({
 | 
								try {
 | 
				
			||||||
				id: ps.clipId,
 | 
									await this.clipService.delete(me, ps.clipId);
 | 
				
			||||||
				userId: me.id,
 | 
								} catch (e) {
 | 
				
			||||||
			});
 | 
									if (e instanceof ClipService.NoSuchClipError) {
 | 
				
			||||||
 | 
										throw new ApiError(meta.errors.noSuchClip);
 | 
				
			||||||
			if (clip == null) {
 | 
									}
 | 
				
			||||||
				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 { Inject, Injectable } from '@nestjs/common';
 | 
				
			||||||
import { Endpoint } from '@/server/api/endpoint-base.js';
 | 
					import { Endpoint } from '@/server/api/endpoint-base.js';
 | 
				
			||||||
import type { ClipNotesRepository, ClipsRepository, NotesRepository } from '@/models/_.js';
 | 
					import { ClipService } from '@/core/ClipService.js';
 | 
				
			||||||
import { DI } from '@/di-symbols.js';
 | 
					 | 
				
			||||||
import { GetterService } from '@/server/api/GetterService.js';
 | 
					 | 
				
			||||||
import { ApiError } from '../../error.js';
 | 
					import { ApiError } from '../../error.js';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export const meta = {
 | 
					export const meta = {
 | 
				
			||||||
| 
						 | 
					@ -46,38 +44,17 @@ export const paramDef = {
 | 
				
			||||||
@Injectable()
 | 
					@Injectable()
 | 
				
			||||||
export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-disable-line import/no-default-export
 | 
					export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-disable-line import/no-default-export
 | 
				
			||||||
	constructor(
 | 
						constructor(
 | 
				
			||||||
		@Inject(DI.clipsRepository)
 | 
							private clipService: ClipService,
 | 
				
			||||||
		private clipsRepository: ClipsRepository,
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		@Inject(DI.clipNotesRepository)
 | 
					 | 
				
			||||||
		private clipNotesRepository: ClipNotesRepository,
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		@Inject(DI.notesRepository)
 | 
					 | 
				
			||||||
		private notesRepository: NotesRepository,
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		private getterService: GetterService,
 | 
					 | 
				
			||||||
	) {
 | 
						) {
 | 
				
			||||||
		super(meta, paramDef, async (ps, me) => {
 | 
							super(meta, paramDef, async (ps, me) => {
 | 
				
			||||||
			const clip = await this.clipsRepository.findOneBy({
 | 
								try {
 | 
				
			||||||
				id: ps.clipId,
 | 
									await this.clipService.removeNote(me, ps.clipId, ps.noteId);
 | 
				
			||||||
				userId: me.id,
 | 
								} catch (e) {
 | 
				
			||||||
			});
 | 
									if (e instanceof ClipService.NoSuchClipError) {
 | 
				
			||||||
 | 
										throw new ApiError(meta.errors.noSuchClip);
 | 
				
			||||||
			if (clip == null) {
 | 
									}
 | 
				
			||||||
				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 { Inject, Injectable } from '@nestjs/common';
 | 
				
			||||||
import { Endpoint } from '@/server/api/endpoint-base.js';
 | 
					import { Endpoint } from '@/server/api/endpoint-base.js';
 | 
				
			||||||
import type { ClipsRepository } from '@/models/_.js';
 | 
					 | 
				
			||||||
import { ClipEntityService } from '@/core/entities/ClipEntityService.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';
 | 
					import { ApiError } from '../../error.js';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export const meta = {
 | 
					export const meta = {
 | 
				
			||||||
| 
						 | 
					@ -48,29 +47,21 @@ export const paramDef = {
 | 
				
			||||||
@Injectable()
 | 
					@Injectable()
 | 
				
			||||||
export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-disable-line import/no-default-export
 | 
					export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-disable-line import/no-default-export
 | 
				
			||||||
	constructor(
 | 
						constructor(
 | 
				
			||||||
		@Inject(DI.clipsRepository)
 | 
							private clipService: ClipService,
 | 
				
			||||||
		private clipsRepository: ClipsRepository,
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
		private clipEntityService: ClipEntityService,
 | 
							private clipEntityService: ClipEntityService,
 | 
				
			||||||
	) {
 | 
						) {
 | 
				
			||||||
		super(meta, paramDef, async (ps, me) => {
 | 
							super(meta, paramDef, async (ps, me) => {
 | 
				
			||||||
			// Fetch the clip
 | 
								try {
 | 
				
			||||||
			const clip = await this.clipsRepository.findOneBy({
 | 
									await this.clipService.update(me, ps.clipId, ps.name, ps.isPublic, ps.description);
 | 
				
			||||||
				id: ps.clipId,
 | 
								} catch (e) {
 | 
				
			||||||
				userId: me.id,
 | 
									if (e instanceof ClipService.NoSuchClipError) {
 | 
				
			||||||
			});
 | 
										throw new ApiError(meta.errors.noSuchClip);
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
			if (clip == null) {
 | 
									throw e;
 | 
				
			||||||
				throw new ApiError(meta.errors.noSuchClip);
 | 
					 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			await this.clipsRepository.update(clip.id, {
 | 
								return await this.clipEntityService.pack(ps.clipId, me);
 | 
				
			||||||
				name: ps.name,
 | 
					 | 
				
			||||||
				description: ps.description,
 | 
					 | 
				
			||||||
				isPublic: ps.isPublic,
 | 
					 | 
				
			||||||
			});
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
			return await this.clipEntityService.pack(clip.id, me);
 | 
					 | 
				
			||||||
		});
 | 
							});
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		
		Reference in a new issue