merge: Add check to prevent creating too many active dialog announcements (!1175)
View MR for information: https://activitypub.software/TransFem-org/Sharkey/-/merge_requests/1175 Closes #769 Approved-by: Hazelnoot <acomputerdog@gmail.com> Approved-by: dakkar <dakkar@thenautilus.net>
This commit is contained in:
commit
e72d153cc8
7 changed files with 98 additions and 32 deletions
|
|
@ -14,11 +14,16 @@ import { IdService } from '@/core/IdService.js';
|
|||
import { AnnouncementEntityService } from '@/core/entities/AnnouncementEntityService.js';
|
||||
import { GlobalEventService } from '@/core/GlobalEventService.js';
|
||||
import { ModerationLogService } from '@/core/ModerationLogService.js';
|
||||
import { IdentifiableError } from '@/misc/identifiable-error.js';
|
||||
import type { Config } from '@/config.js';
|
||||
import { RoleService } from '@/core/RoleService.js';
|
||||
|
||||
@Injectable()
|
||||
export class AnnouncementService {
|
||||
constructor(
|
||||
@Inject(DI.config)
|
||||
private config: Config,
|
||||
|
||||
@Inject(DI.announcementsRepository)
|
||||
private announcementsRepository: AnnouncementsRepository,
|
||||
|
||||
|
|
@ -74,6 +79,10 @@ export class AnnouncementService {
|
|||
|
||||
@bindThis
|
||||
public async create(values: Partial<MiAnnouncement>, moderator?: MiUser): Promise<{ raw: MiAnnouncement; packed: Packed<'Announcement'> }> {
|
||||
if (values.display === 'dialog') {
|
||||
await this.assertDialogAnnouncementsCountLimit();
|
||||
}
|
||||
|
||||
const announcement = await this.announcementsRepository.insertOne({
|
||||
id: this.idService.gen(),
|
||||
updatedAt: null,
|
||||
|
|
@ -128,6 +137,11 @@ export class AnnouncementService {
|
|||
|
||||
@bindThis
|
||||
public async update(announcement: MiAnnouncement, values: Partial<MiAnnouncement>, moderator?: MiUser): Promise<void> {
|
||||
// Check if this operation would produce an active dialog announcement
|
||||
if ((values.display ?? announcement.display) === 'dialog' && (values.isActive ?? announcement.isActive)) {
|
||||
await this.assertDialogAnnouncementsCountLimit();
|
||||
}
|
||||
|
||||
await this.announcementsRepository.update(announcement.id, {
|
||||
updatedAt: new Date(),
|
||||
title: values.title,
|
||||
|
|
@ -231,4 +245,17 @@ export class AnnouncementService {
|
|||
this.globalEventService.publishMainStream(user.id, 'readAllAnnouncements');
|
||||
}
|
||||
}
|
||||
|
||||
private async assertDialogAnnouncementsCountLimit(): 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 >= this.config.maxDialogAnnouncements) {
|
||||
throw new IdentifiableError('c0d15f15-f18e-4a40-bcb1-f310d58204ee', 'Too many dialog announcements.');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue