This commit is contained in:
syuilo 2023-08-10 09:09:21 +09:00
parent ba7d04aeb9
commit d4beabd87c
5 changed files with 45 additions and 15 deletions

View File

@ -57,6 +57,33 @@ export class AnnouncementService {
return q.getMany(); return q.getMany();
} }
@bindThis
public async create(values: Partial<Announcement>): Promise<{ raw: Announcement; packed: Packed<'Announcement'> }> {
const announcement = await this.announcementsRepository.insert({
id: this.idService.genId(),
createdAt: new Date(),
updatedAt: null,
title: values.title,
text: values.text,
imageUrl: values.imageUrl,
display: values.display,
forExistingUsers: values.forExistingUsers,
needConfirmationToRead: values.needConfirmationToRead,
userId: values.userId,
}).then(x => this.announcementsRepository.findOneByOrFail(x.identifiers[0]));
const packed = (await this.packMany([announcement]))[0];
this.globalEventService.publishBroadcastStream('announcementCreated', {
announcement: packed,
});
return {
raw: announcement,
packed: packed,
};
}
@bindThis @bindThis
public async read(user: User, announcementId: Announcement['id']): Promise<void> { public async read(user: User, announcementId: Announcement['id']): Promise<void> {
try { try {
@ -87,12 +114,13 @@ export class AnnouncementService {
return announcements.map(announcement => ({ return announcements.map(announcement => ({
id: announcement.id, id: announcement.id,
createdAt: announcement.createdAt.toISOString(), createdAt: announcement.createdAt.toISOString(),
updatedAt: announcement.updatedAt?.toISOString(), updatedAt: announcement.updatedAt?.toISOString() ?? null,
text: announcement.text, text: announcement.text,
title: announcement.title, title: announcement.title,
imageUrl: announcement.imageUrl, imageUrl: announcement.imageUrl,
display: announcement.display, display: announcement.display,
needConfirmationToRead: announcement.needConfirmationToRead, needConfirmationToRead: announcement.needConfirmationToRead,
forYou: announcement.userId === me?.id,
isRead: reads.some(read => read.announcementId === announcement.id), isRead: reads.some(read => read.announcementId === announcement.id),
})); }));
} }

View File

@ -35,6 +35,7 @@ import { packedQueueCountSchema } from '@/models/json-schema/queue.js';
import { packedGalleryPostSchema } from '@/models/json-schema/gallery-post.js'; import { packedGalleryPostSchema } from '@/models/json-schema/gallery-post.js';
import { packedEmojiDetailedSchema, packedEmojiSimpleSchema } from '@/models/json-schema/emoji.js'; import { packedEmojiDetailedSchema, packedEmojiSimpleSchema } from '@/models/json-schema/emoji.js';
import { packedFlashSchema } from '@/models/json-schema/flash.js'; import { packedFlashSchema } from '@/models/json-schema/flash.js';
import { packedAnnouncementSchema } from '@/models/json-schema/announcement.js';
export const refs = { export const refs = {
UserLite: packedUserLiteSchema, UserLite: packedUserLiteSchema,

View File

@ -38,13 +38,17 @@ export const packedAnnouncementSchema = {
type: 'string', type: 'string',
optional: false, nullable: false, optional: false, nullable: false,
}, },
isRead: { forYou: {
type: 'boolean', type: 'boolean',
optional: true, nullable: false, optional: false, nullable: false,
}, },
needConfirmationToRead: { needConfirmationToRead: {
type: 'boolean', type: 'boolean',
optional: false, nullable: false, optional: false, nullable: false,
}, },
isRead: {
type: 'boolean',
optional: true, nullable: false,
},
}, },
} as const; } as const;

View File

@ -3,11 +3,9 @@
* SPDX-License-Identifier: AGPL-3.0-only * SPDX-License-Identifier: AGPL-3.0-only
*/ */
import { Inject, Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
import { Endpoint } from '@/server/api/endpoint-base.js'; import { Endpoint } from '@/server/api/endpoint-base.js';
import type { AnnouncementsRepository } from '@/models/index.js'; import { AnnouncementService } from '@/core/AnnouncementService.js';
import { IdService } from '@/core/IdService.js';
import { DI } from '@/di-symbols.js';
export const meta = { export const meta = {
tags: ['admin'], tags: ['admin'],
@ -69,14 +67,10 @@ export const paramDef = {
@Injectable() @Injectable()
export default class extends Endpoint<typeof meta, typeof paramDef> { export default class extends Endpoint<typeof meta, typeof paramDef> {
constructor( constructor(
@Inject(DI.announcementsRepository) private announcementService: AnnouncementService,
private announcementsRepository: AnnouncementsRepository,
private idService: IdService,
) { ) {
super(meta, paramDef, async (ps, me) => { super(meta, paramDef, async (ps, me) => {
const announcement = await this.announcementsRepository.insert({ const { raw, packed } = await this.announcementService.create({
id: this.idService.genId(),
createdAt: new Date(), createdAt: new Date(),
updatedAt: null, updatedAt: null,
title: ps.title, title: ps.title,
@ -86,9 +80,9 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {
forExistingUsers: ps.forExistingUsers, forExistingUsers: ps.forExistingUsers,
needConfirmationToRead: ps.needConfirmationToRead, needConfirmationToRead: ps.needConfirmationToRead,
userId: ps.userId, userId: ps.userId,
}).then(x => this.announcementsRepository.findOneByOrFail(x.identifiers[0])); });
return Object.assign({}, announcement, { createdAt: announcement.createdAt.toISOString(), updatedAt: null }); return packed;
}); });
} }
} }

View File

@ -64,6 +64,9 @@ export interface BroadcastTypes {
[other: string]: any; [other: string]: any;
}[]; }[];
}; };
announcementCreated: {
announcement: Packed<'Announcement'>;
};
} }
export interface MainStreamTypes { export interface MainStreamTypes {