From 4926afc264c88711ec89ab7fb4e8dfa8b8a265e9 Mon Sep 17 00:00:00 2001 From: bunnybeam Date: Wed, 23 Jul 2025 22:45:20 +0100 Subject: [PATCH] move dialog announcement check into different function --- .../backend/src/core/AnnouncementService.ts | 31 +++++++++---------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/packages/backend/src/core/AnnouncementService.ts b/packages/backend/src/core/AnnouncementService.ts index e66d85bac7..51ab4881fd 100644 --- a/packages/backend/src/core/AnnouncementService.ts +++ b/packages/backend/src/core/AnnouncementService.ts @@ -69,14 +69,7 @@ export class AnnouncementService { @bindThis public async create(values: Partial, moderator?: MiUser): Promise<{ raw: MiAnnouncement; packed: Packed<'Announcement'> }> { if (values.display === 'dialog') { - // Check how many active dialog queries already exist, to enforce a limit - const dialogCount = await this.announcementsRepository.createQueryBuilder('announcement') - .where({ isActive: true }) - .where({ display: 'dialog' }) - .getCount(); - if (dialogCount >= 5) { - throw new IdentifiableError('c0d15f15-f18e-4a40-bcb1-f310d58204ee', 'Too many dialog announcements.'); - } + await this.assertDialogAnnouncenmentsCountLimit(); } const announcement = await this.announcementsRepository.insertOne({ @@ -134,14 +127,7 @@ export class AnnouncementService { public async update(announcement: MiAnnouncement, values: Partial, moderator?: MiUser): Promise { // Check if this operation would produce an active dialog announcement if ((values.display ?? announcement.display) === 'dialog' && (values.isActive ?? announcement.isActive)) { - // Check how many active dialog queries already exist, to enforce a limit - const dialogCount = await this.announcementsRepository.createQueryBuilder('announcement') - .where({ isActive: true }) - .where({ display: 'dialog' }) - .getCount(); - if (dialogCount >= 5) { - throw new IdentifiableError('c0d15f15-f18e-4a40-bcb1-f310d58204ee', 'Too many dialog announcements.'); - } + await this.assertDialogAnnouncenmentsCountLimit(); } await this.announcementsRepository.update(announcement.id, { @@ -246,4 +232,17 @@ export class AnnouncementService { this.globalEventService.publishMainStream(user.id, 'readAllAnnouncements'); } } + + private async assertDialogAnnouncenmentsCountLimit(): Promise { + // Check how many active dialog queries already exist, to enforce a limit + const dialogCount = await this.announcementsRepository.createQueryBuilder('announcement') + .where({ + isActive: true, + display: 'dialog', + }) + .getCount(); + if (dialogCount >= 5) { + throw new IdentifiableError('c0d15f15-f18e-4a40-bcb1-f310d58204ee', 'Too many dialog announcements.'); + } + } }