move dialog announcement check into different function
This commit is contained in:
parent
e5aabb2978
commit
4926afc264
1 changed files with 15 additions and 16 deletions
|
|
@ -69,14 +69,7 @@ export class AnnouncementService {
|
||||||
@bindThis
|
@bindThis
|
||||||
public async create(values: Partial<MiAnnouncement>, moderator?: MiUser): Promise<{ raw: MiAnnouncement; packed: Packed<'Announcement'> }> {
|
public async create(values: Partial<MiAnnouncement>, moderator?: MiUser): Promise<{ raw: MiAnnouncement; packed: Packed<'Announcement'> }> {
|
||||||
if (values.display === 'dialog') {
|
if (values.display === 'dialog') {
|
||||||
// Check how many active dialog queries already exist, to enforce a limit
|
await this.assertDialogAnnouncenmentsCountLimit();
|
||||||
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.');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const announcement = await this.announcementsRepository.insertOne({
|
const announcement = await this.announcementsRepository.insertOne({
|
||||||
|
|
@ -134,14 +127,7 @@ export class AnnouncementService {
|
||||||
public async update(announcement: MiAnnouncement, values: Partial<MiAnnouncement>, moderator?: MiUser): Promise<void> {
|
public async update(announcement: MiAnnouncement, values: Partial<MiAnnouncement>, moderator?: MiUser): Promise<void> {
|
||||||
// Check if this operation would produce an active dialog announcement
|
// Check if this operation would produce an active dialog announcement
|
||||||
if ((values.display ?? announcement.display) === 'dialog' && (values.isActive ?? announcement.isActive)) {
|
if ((values.display ?? announcement.display) === 'dialog' && (values.isActive ?? announcement.isActive)) {
|
||||||
// Check how many active dialog queries already exist, to enforce a limit
|
await this.assertDialogAnnouncenmentsCountLimit();
|
||||||
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.announcementsRepository.update(announcement.id, {
|
await this.announcementsRepository.update(announcement.id, {
|
||||||
|
|
@ -246,4 +232,17 @@ export class AnnouncementService {
|
||||||
this.globalEventService.publishMainStream(user.id, 'readAllAnnouncements');
|
this.globalEventService.publishMainStream(user.id, 'readAllAnnouncements');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async assertDialogAnnouncenmentsCountLimit(): Promise<void> {
|
||||||
|
// 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.');
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue