fix(backend): アンテナ等がポリシーで定められた上限を超えている場合、変更や追加ができないように (MisskeyIO#646)

This commit is contained in:
kabo2468 2024-06-16 20:11:25 +09:00 committed by GitHub
parent 5ff34d2f8f
commit 2b2975c0dd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
17 changed files with 281 additions and 19 deletions

View file

@ -20,6 +20,8 @@ export class ClipService {
public static AlreadyAddedError = class extends Error {};
public static TooManyClipNotesError = class extends Error {};
public static TooManyClipsError = class extends Error {};
public static ClipLimitExceededError = class extends Error {};
public static ClipNotesLimitExceededError = class extends Error {};
constructor(
@Inject(DI.clipsRepository)
@ -38,13 +40,26 @@ export class ClipService {
@bindThis
public async create(me: MiLocalUser, name: string, isPublic: boolean, description: string | null): Promise<MiClip> {
const policies = await this.roleService.getUserPolicies(me.id);
const currentCount = await this.clipsRepository.countBy({
userId: me.id,
});
if (currentCount > (await this.roleService.getUserPolicies(me.id)).clipLimit) {
if (currentCount >= policies.clipLimit) {
throw new ClipService.TooManyClipsError();
}
const currentNoteCounts = await this.clipNotesRepository
.createQueryBuilder('cn')
.select('COUNT(*)')
.innerJoin('cn.clip', 'c')
.where('c.userId = :userId', { userId: me.id })
.groupBy('cn.clipId')
.getRawMany<{ count: number }>();
if (currentNoteCounts.some((x) => x.count > policies.noteEachClipsLimit)) {
throw new ClipService.ClipNotesLimitExceededError();
}
const clip = await this.clipsRepository.insert({
id: this.idService.gen(),
userId: me.id,
@ -67,6 +82,26 @@ export class ClipService {
throw new ClipService.NoSuchClipError();
}
const policies = await this.roleService.getUserPolicies(me.id);
const currentCount = await this.clipsRepository.countBy({
userId: me.id,
});
if (currentCount > policies.clipLimit) {
throw new ClipService.ClipLimitExceededError();
}
const currentNoteCounts = await this.clipNotesRepository
.createQueryBuilder('cn')
.select('COUNT(*)')
.innerJoin('cn.clip', 'c')
.where('c.userId = :userId', { userId: me.id })
.groupBy('cn.clipId')
.getRawMany<{ count: number }>();
if (currentNoteCounts.some((x) => x.count > policies.noteEachClipsLimit)) {
throw new ClipService.ClipNotesLimitExceededError();
}
await this.clipsRepository.update(clip.id, {
name: name,
description: description,
@ -99,10 +134,30 @@ export class ClipService {
throw new ClipService.NoSuchClipError();
}
const currentCount = await this.clipNotesRepository.countBy({
const policies = await this.roleService.getUserPolicies(me.id);
const currentClipCount = await this.clipsRepository.countBy({
userId: me.id,
});
if (currentClipCount > policies.clipLimit) {
throw new ClipService.ClipLimitExceededError();
}
const currentNoteCounts = await this.clipNotesRepository
.createQueryBuilder('cn')
.select('COUNT(*)')
.innerJoin('cn.clip', 'c')
.where('c.userId = :userId', { userId: me.id })
.groupBy('cn.clipId')
.getRawMany<{ count: number }>();
if (currentNoteCounts.some((x) => x.count > policies.noteEachClipsLimit)) {
throw new ClipService.ClipNotesLimitExceededError();
}
const currentNoteCount = await this.clipNotesRepository.countBy({
clipId: clip.id,
});
if (currentCount > (await this.roleService.getUserPolicies(me.id)).noteEachClipsLimit) {
if (currentNoteCount >= policies.noteEachClipsLimit) {
throw new ClipService.TooManyClipNotesError();
}

View file

@ -95,7 +95,7 @@ export class UserListService implements OnApplicationShutdown, OnModuleInit {
const currentCount = await this.userListMembershipsRepository.countBy({
userListId: list.id,
});
if (currentCount > (await this.roleService.getUserPolicies(me.id)).userEachUserListsLimit) {
if (currentCount >= (await this.roleService.getUserPolicies(me.id)).userEachUserListsLimit) {
throw new UserListService.TooManyUsersError();
}