Skip to content

Commit

Permalink
fix AnnouncementService
Browse files Browse the repository at this point in the history
  • Loading branch information
riku6460 committed Sep 27, 2023
1 parent 3a57fb2 commit 50d246a
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 112 deletions.
90 changes: 68 additions & 22 deletions packages/backend/src/core/AnnouncementService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import { Packed } from '@/misc/json-schema.js';
import { IdService } from '@/core/IdService.js';
import { GlobalEventService } from '@/core/GlobalEventService.js';
import { ModerationLogService } from '@/core/ModerationLogService.js';
import { AnnouncementEntityService } from '@/core/entities/AnnouncementEntityService.js';
import { UserEntityService } from '@/core/entities/UserEntityService.js';

@Injectable()
Expand All @@ -31,7 +30,6 @@ export class AnnouncementService {

private idService: IdService,
private userEntityService: UserEntityService,
private announcementEntityService: AnnouncementEntityService,
private globalEventService: GlobalEventService,
private moderationLogService: ModerationLogService,
) {
Expand Down Expand Up @@ -99,7 +97,7 @@ export class AnnouncementService {
'announcement."createdAt"': 'DESC',
});

return this.announcementEntityService.packMany(
return this.packMany(
await query
.limit(limit)
.offset(offset)
Expand All @@ -109,31 +107,41 @@ export class AnnouncementService {
}

@bindThis
public async getUnreadAnnouncements(user: MiUser): Promise<MiAnnouncement[]> {
const readsQuery = this.announcementReadsRepository.createQueryBuilder('read')
.select('read.announcementId')
.where('read.userId = :userId', { userId: user.id });

const q = this.announcementsRepository.createQueryBuilder('announcement')
.where('announcement.isActive = true')
public async getUnreadAnnouncements(user: MiUser): Promise<Packed<'Announcement'>[]> {
const query = this.announcementsRepository.createQueryBuilder('announcement');
query.leftJoin(
MiAnnouncementRead,
'read',
'read."announcementId" = announcement.id AND read."userId" = :userId',
{ userId: user.id },
);
query.select([
'announcement.*',
'CASE WHEN read.id IS NULL THEN FALSE ELSE TRUE END as "isRead"',
]);
query.andWhere('read.id IS NULL');
query.andWhere('announcement."isActive" = true');

query
.andWhere(new Brackets(qb => {
qb.orWhere('announcement.userId = :userId', { userId: user.id });
qb.orWhere('announcement.userId IS NULL');
}))
.andWhere(new Brackets(qb => {
qb.orWhere('announcement.forExistingUsers = false');
qb.orWhere('announcement.createdAt > :createdAt', { createdAt: user.createdAt });
}))
.andWhere(`announcement.id NOT IN (${ readsQuery.getQuery() })`);
}));

q.setParameters(readsQuery.getParameters());
q.orderBy({
query.orderBy({
'announcement."isActive"': 'DESC',
'announcement."displayOrder"': 'DESC',
'announcement."createdAt"': 'DESC',
});

return q.getMany();
return this.packMany(
await query.getMany(),
user,
);
}

@bindThis
Expand Down Expand Up @@ -239,6 +247,13 @@ export class AnnouncementService {

@bindThis
public async update(announcement: MiAnnouncement, values: Partial<MiAnnouncement>, moderator?: MiUser): Promise<void> {
if (announcement.userId && announcement.userId !== values.userId) {
await this.announcementReadsRepository.delete({
announcementId: announcement.id,
userId: announcement.userId,
});
}

await this.announcementsRepository.update(announcement.id, {
updatedAt: new Date(),
title: values.title,
Expand All @@ -252,6 +267,7 @@ export class AnnouncementService {
closeDuration: values.closeDuration,
displayOrder: values.displayOrder,
isActive: values.isActive,
userId: values.userId,
});

const after = await this.announcementsRepository.findOneByOrFail({ id: announcement.id });
Expand Down Expand Up @@ -279,6 +295,9 @@ export class AnnouncementService {

@bindThis
public async delete(announcement: MiAnnouncement, moderator?: MiUser): Promise<void> {
await this.announcementReadsRepository.delete({
announcementId: announcement.id,
});
await this.announcementsRepository.delete(announcement.id);

if (moderator) {
Expand All @@ -296,6 +315,37 @@ export class AnnouncementService {
}
}

@bindThis
public async countUnreadAnnouncements(me: MiUser): Promise<number> {
const query = this.announcementsRepository.createQueryBuilder('announcement');
query.leftJoinAndSelect(
MiAnnouncementRead,
'read',
'read."announcementId" = announcement.id AND read."userId" = :userId',
{ userId: me.id },
);
query.andWhere('read.id IS NULL');
query.andWhere('announcement."isActive" = true');

query
.andWhere(
new Brackets((qb) => {
qb.orWhere('announcement."userId" = :userId', { userId: me.id });
qb.orWhere('announcement."userId" IS NULL');
}),
)
.andWhere(
new Brackets((qb) => {
qb.orWhere('announcement."forExistingUsers" = false');
qb.orWhere('announcement."createdAt" > :createdAt', {
createdAt: me.createdAt,
});
}),
);

return query.getCount();
}

@bindThis
public async read(user: MiUser, announcementId: MiAnnouncement['id']): Promise<void> {
try {
Expand All @@ -309,20 +359,16 @@ export class AnnouncementService {
return;
}

if ((await this.getUnreadAnnouncements(user)).length === 0) {
if ((await this.countUnreadAnnouncements(user)) === 0) {
this.globalEventService.publishMainStream(user.id, 'readAllAnnouncements');
}
}

@bindThis
public async packMany(
announcements: MiAnnouncement[],
announcements: (MiAnnouncement & { isRead?: boolean | null })[],
me?: { id: MiUser['id'] } | null | undefined,
options?: {
reads?: MiAnnouncementRead[];
},
): Promise<Packed<'Announcement'>[]> {
const reads = me ? (options?.reads ?? await this.getReads(me.id)) : [];
return announcements.map(announcement => ({
id: announcement.id,
createdAt: announcement.createdAt.toISOString(),
Expand All @@ -336,7 +382,7 @@ export class AnnouncementService {
closeDuration: announcement.closeDuration,
displayOrder: announcement.displayOrder,
forYou: announcement.userId === me?.id,
isRead: reads.some(read => read.announcementId === announcement.id),
isRead: announcement.isRead ?? undefined,
}));
}
}
6 changes: 0 additions & 6 deletions packages/backend/src/core/CoreModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ import PerUserDriveChart from './chart/charts/per-user-drive.js';
import ApRequestChart from './chart/charts/ap-request.js';
import { ChartManagementService } from './chart/ChartManagementService.js';
import { AbuseUserReportEntityService } from './entities/AbuseUserReportEntityService.js';
import { AnnouncementEntityService } from './entities/AnnouncementEntityService.js';
import { AntennaEntityService } from './entities/AntennaEntityService.js';
import { AppEntityService } from './entities/AppEntityService.js';
import { AuthSessionEntityService } from './entities/AuthSessionEntityService.js';
Expand Down Expand Up @@ -203,7 +202,6 @@ const $ApRequestChart: Provider = { provide: 'ApRequestChart', useExisting: ApRe
const $ChartManagementService: Provider = { provide: 'ChartManagementService', useExisting: ChartManagementService };

const $AbuseUserReportEntityService: Provider = { provide: 'AbuseUserReportEntityService', useExisting: AbuseUserReportEntityService };
const $AnnouncementEntityService: Provider = { provide: 'AnnouncementEntityService', useExisting: AnnouncementEntityService };
const $AntennaEntityService: Provider = { provide: 'AntennaEntityService', useExisting: AntennaEntityService };
const $AppEntityService: Provider = { provide: 'AppEntityService', useExisting: AppEntityService };
const $AuthSessionEntityService: Provider = { provide: 'AuthSessionEntityService', useExisting: AuthSessionEntityService };
Expand Down Expand Up @@ -332,7 +330,6 @@ const $ApQuestionService: Provider = { provide: 'ApQuestionService', useExisting
ApRequestChart,
ChartManagementService,
AbuseUserReportEntityService,
AnnouncementEntityService,
AntennaEntityService,
AppEntityService,
AuthSessionEntityService,
Expand Down Expand Up @@ -456,7 +453,6 @@ const $ApQuestionService: Provider = { provide: 'ApQuestionService', useExisting
$ApRequestChart,
$ChartManagementService,
$AbuseUserReportEntityService,
$AnnouncementEntityService,
$AntennaEntityService,
$AppEntityService,
$AuthSessionEntityService,
Expand Down Expand Up @@ -580,7 +576,6 @@ const $ApQuestionService: Provider = { provide: 'ApQuestionService', useExisting
ApRequestChart,
ChartManagementService,
AbuseUserReportEntityService,
AnnouncementEntityService,
AntennaEntityService,
AppEntityService,
AuthSessionEntityService,
Expand Down Expand Up @@ -703,7 +698,6 @@ const $ApQuestionService: Provider = { provide: 'ApQuestionService', useExisting
$ApRequestChart,
$ChartManagementService,
$AbuseUserReportEntityService,
$AnnouncementEntityService,
$AntennaEntityService,
$AppEntityService,
$AuthSessionEntityService,
Expand Down
72 changes: 0 additions & 72 deletions packages/backend/src/core/entities/AnnouncementEntityService.ts

This file was deleted.

13 changes: 1 addition & 12 deletions packages/backend/src/server/api/endpoints/announcements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,9 @@
* SPDX-License-Identifier: AGPL-3.0-only
*/

import { Inject, Injectable } from '@nestjs/common';
import { Brackets } from 'typeorm';
import { Injectable } from '@nestjs/common';
import { Endpoint } from '@/server/api/endpoint-base.js';
import { QueryService } from '@/core/QueryService.js';
import { AnnouncementService } from '@/core/AnnouncementService.js';
import { DI } from '@/di-symbols.js';
import type { AnnouncementReadsRepository, AnnouncementsRepository } from '@/models/_.js';

export const meta = {
tags: ['meta'],
Expand Down Expand Up @@ -40,13 +36,6 @@ export const paramDef = {
@Injectable()
export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-disable-line import/no-default-export
constructor(
@Inject(DI.announcementsRepository)
private announcementsRepository: AnnouncementsRepository,

@Inject(DI.announcementReadsRepository)
private announcementReadsRepository: AnnouncementReadsRepository,

private queryService: QueryService,
private announcementService: AnnouncementService,
) {
super(meta, paramDef, async (ps, me) => {
Expand Down

0 comments on commit 50d246a

Please sign in to comment.