1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
/*
* SPDX-FileCopyrightText: syuilo and misskey-project
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { Inject, Injectable } from '@nestjs/common';
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 {
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();
}
const clip = await this.clipsRepository.insert({
id: this.idService.gen(),
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.gen(),
noteId: noteId,
clipId: clip.id,
});
} 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();
}
}
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();
}
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);
}
}
|