2023-08-13 13:12:29 +02:00
|
|
|
/*
|
2024-02-13 16:50:11 +01:00
|
|
|
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
2023-08-13 13:12:29 +02:00
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
import { Brackets } from 'typeorm';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2023-09-20 04:33:36 +02:00
|
|
|
import type { MiUser } from '@/models/User.js';
|
2023-09-25 03:29:12 +02:00
|
|
|
import type { AnnouncementReadsRepository, AnnouncementsRepository, MiAnnouncement, MiAnnouncementRead, UsersRepository } from '@/models/_.js';
|
2023-08-13 13:12:29 +02:00
|
|
|
import { bindThis } from '@/decorators.js';
|
|
|
|
import { Packed } from '@/misc/json-schema.js';
|
|
|
|
import { IdService } from '@/core/IdService.js';
|
|
|
|
import { GlobalEventService } from '@/core/GlobalEventService.js';
|
2023-09-23 11:28:16 +02:00
|
|
|
import { ModerationLogService } from '@/core/ModerationLogService.js';
|
2023-08-13 13:12:29 +02:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class AnnouncementService {
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.announcementsRepository)
|
|
|
|
private announcementsRepository: AnnouncementsRepository,
|
|
|
|
|
|
|
|
@Inject(DI.announcementReadsRepository)
|
|
|
|
private announcementReadsRepository: AnnouncementReadsRepository,
|
|
|
|
|
2023-09-25 03:29:12 +02:00
|
|
|
@Inject(DI.usersRepository)
|
|
|
|
private usersRepository: UsersRepository,
|
|
|
|
|
2023-08-13 13:12:29 +02:00
|
|
|
private idService: IdService,
|
|
|
|
private globalEventService: GlobalEventService,
|
2023-09-23 11:28:16 +02:00
|
|
|
private moderationLogService: ModerationLogService,
|
2023-08-13 13:12:29 +02:00
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
|
|
|
@bindThis
|
2023-08-16 10:51:28 +02:00
|
|
|
public async getReads(userId: MiUser['id']): Promise<MiAnnouncementRead[]> {
|
2023-08-13 13:12:29 +02:00
|
|
|
return this.announcementReadsRepository.findBy({
|
|
|
|
userId: userId,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@bindThis
|
2023-08-16 10:51:28 +02:00
|
|
|
public async getUnreadAnnouncements(user: MiUser): Promise<MiAnnouncement[]> {
|
2023-08-13 13:12:29 +02:00
|
|
|
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')
|
2023-11-05 01:04:38 +01:00
|
|
|
.andWhere('announcement.silence = false')
|
2023-08-13 13:12:29 +02:00
|
|
|
.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');
|
2023-10-16 03:45:22 +02:00
|
|
|
qb.orWhere('announcement.id > :userId', { userId: user.id });
|
2023-08-13 13:12:29 +02:00
|
|
|
}))
|
|
|
|
.andWhere(`announcement.id NOT IN (${ readsQuery.getQuery() })`);
|
|
|
|
|
|
|
|
q.setParameters(readsQuery.getParameters());
|
|
|
|
|
|
|
|
return q.getMany();
|
|
|
|
}
|
|
|
|
|
|
|
|
@bindThis
|
2023-09-24 03:46:09 +02:00
|
|
|
public async create(values: Partial<MiAnnouncement>, moderator?: MiUser): Promise<{ raw: MiAnnouncement; packed: Packed<'Announcement'> }> {
|
2023-08-13 13:12:29 +02:00
|
|
|
const announcement = await this.announcementsRepository.insert({
|
2023-10-16 03:45:22 +02:00
|
|
|
id: this.idService.gen(),
|
2023-08-13 13:12:29 +02:00
|
|
|
updatedAt: null,
|
|
|
|
title: values.title,
|
|
|
|
text: values.text,
|
|
|
|
imageUrl: values.imageUrl,
|
|
|
|
icon: values.icon,
|
|
|
|
display: values.display,
|
|
|
|
forExistingUsers: values.forExistingUsers,
|
2023-11-05 01:04:38 +01:00
|
|
|
silence: values.silence,
|
2023-08-13 13:12:29 +02:00
|
|
|
needConfirmationToRead: values.needConfirmationToRead,
|
|
|
|
userId: values.userId,
|
|
|
|
}).then(x => this.announcementsRepository.findOneByOrFail(x.identifiers[0]));
|
|
|
|
|
|
|
|
const packed = (await this.packMany([announcement]))[0];
|
|
|
|
|
|
|
|
if (values.userId) {
|
|
|
|
this.globalEventService.publishMainStream(values.userId, 'announcementCreated', {
|
|
|
|
announcement: packed,
|
|
|
|
});
|
2023-09-23 11:28:16 +02:00
|
|
|
|
2023-09-24 03:46:09 +02:00
|
|
|
if (moderator) {
|
2023-09-25 03:29:12 +02:00
|
|
|
const user = await this.usersRepository.findOneByOrFail({ id: values.userId });
|
2023-09-24 03:46:09 +02:00
|
|
|
this.moderationLogService.log(moderator, 'createUserAnnouncement', {
|
|
|
|
announcementId: announcement.id,
|
|
|
|
announcement: announcement,
|
|
|
|
userId: values.userId,
|
2023-09-25 03:29:12 +02:00
|
|
|
userUsername: user.username,
|
|
|
|
userHost: user.host,
|
2023-09-24 03:46:09 +02:00
|
|
|
});
|
|
|
|
}
|
2023-08-13 13:12:29 +02:00
|
|
|
} else {
|
|
|
|
this.globalEventService.publishBroadcastStream('announcementCreated', {
|
|
|
|
announcement: packed,
|
|
|
|
});
|
2023-09-23 11:28:16 +02:00
|
|
|
|
2023-09-24 03:46:09 +02:00
|
|
|
if (moderator) {
|
|
|
|
this.moderationLogService.log(moderator, 'createGlobalAnnouncement', {
|
|
|
|
announcementId: announcement.id,
|
|
|
|
announcement: announcement,
|
|
|
|
});
|
|
|
|
}
|
2023-08-13 13:12:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
raw: announcement,
|
|
|
|
packed: packed,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-09-24 03:46:09 +02:00
|
|
|
@bindThis
|
|
|
|
public async update(announcement: MiAnnouncement, values: Partial<MiAnnouncement>, moderator?: MiUser): Promise<void> {
|
|
|
|
await this.announcementsRepository.update(announcement.id, {
|
|
|
|
updatedAt: new Date(),
|
|
|
|
title: values.title,
|
|
|
|
text: values.text,
|
|
|
|
/* eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing -- 空の文字列の場合、nullを渡すようにするため */
|
|
|
|
imageUrl: values.imageUrl || null,
|
|
|
|
display: values.display,
|
|
|
|
icon: values.icon,
|
|
|
|
forExistingUsers: values.forExistingUsers,
|
2023-11-05 01:04:38 +01:00
|
|
|
silence: values.silence,
|
2023-09-24 03:46:09 +02:00
|
|
|
needConfirmationToRead: values.needConfirmationToRead,
|
|
|
|
isActive: values.isActive,
|
|
|
|
});
|
|
|
|
|
|
|
|
const after = await this.announcementsRepository.findOneByOrFail({ id: announcement.id });
|
|
|
|
|
|
|
|
if (moderator) {
|
|
|
|
if (announcement.userId) {
|
2023-09-25 03:29:12 +02:00
|
|
|
const user = await this.usersRepository.findOneByOrFail({ id: announcement.userId });
|
2023-09-24 03:46:09 +02:00
|
|
|
this.moderationLogService.log(moderator, 'updateUserAnnouncement', {
|
|
|
|
announcementId: announcement.id,
|
|
|
|
before: announcement,
|
|
|
|
after: after,
|
2023-09-25 03:29:12 +02:00
|
|
|
userId: announcement.userId,
|
|
|
|
userUsername: user.username,
|
|
|
|
userHost: user.host,
|
2023-09-24 03:46:09 +02:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.moderationLogService.log(moderator, 'updateGlobalAnnouncement', {
|
|
|
|
announcementId: announcement.id,
|
|
|
|
before: announcement,
|
|
|
|
after: after,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@bindThis
|
|
|
|
public async delete(announcement: MiAnnouncement, moderator?: MiUser): Promise<void> {
|
|
|
|
await this.announcementsRepository.delete(announcement.id);
|
|
|
|
|
|
|
|
if (moderator) {
|
|
|
|
if (announcement.userId) {
|
2023-10-04 12:55:35 +02:00
|
|
|
const user = await this.usersRepository.findOneByOrFail({ id: announcement.userId });
|
2023-09-24 03:46:09 +02:00
|
|
|
this.moderationLogService.log(moderator, 'deleteUserAnnouncement', {
|
|
|
|
announcementId: announcement.id,
|
|
|
|
announcement: announcement,
|
2023-10-04 12:55:35 +02:00
|
|
|
userId: announcement.userId,
|
|
|
|
userUsername: user.username,
|
|
|
|
userHost: user.host,
|
2023-09-24 03:46:09 +02:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.moderationLogService.log(moderator, 'deleteGlobalAnnouncement', {
|
|
|
|
announcementId: announcement.id,
|
|
|
|
announcement: announcement,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-13 13:12:29 +02:00
|
|
|
@bindThis
|
2023-08-16 10:51:28 +02:00
|
|
|
public async read(user: MiUser, announcementId: MiAnnouncement['id']): Promise<void> {
|
2023-08-13 13:12:29 +02:00
|
|
|
try {
|
|
|
|
await this.announcementReadsRepository.insert({
|
2023-10-16 03:45:22 +02:00
|
|
|
id: this.idService.gen(),
|
2023-08-13 13:12:29 +02:00
|
|
|
announcementId: announcementId,
|
|
|
|
userId: user.id,
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((await this.getUnreadAnnouncements(user)).length === 0) {
|
|
|
|
this.globalEventService.publishMainStream(user.id, 'readAllAnnouncements');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@bindThis
|
|
|
|
public async packMany(
|
2023-08-16 10:51:28 +02:00
|
|
|
announcements: MiAnnouncement[],
|
|
|
|
me?: { id: MiUser['id'] } | null | undefined,
|
2023-08-13 13:12:29 +02:00
|
|
|
options?: {
|
2023-08-16 10:51:28 +02:00
|
|
|
reads?: MiAnnouncementRead[];
|
2023-08-13 13:12:29 +02:00
|
|
|
},
|
|
|
|
): Promise<Packed<'Announcement'>[]> {
|
|
|
|
const reads = me ? (options?.reads ?? await this.getReads(me.id)) : [];
|
|
|
|
return announcements.map(announcement => ({
|
|
|
|
id: announcement.id,
|
2023-10-16 03:45:22 +02:00
|
|
|
createdAt: this.idService.parse(announcement.id).date.toISOString(),
|
2023-08-13 13:12:29 +02:00
|
|
|
updatedAt: announcement.updatedAt?.toISOString() ?? null,
|
|
|
|
text: announcement.text,
|
|
|
|
title: announcement.title,
|
|
|
|
imageUrl: announcement.imageUrl,
|
|
|
|
icon: announcement.icon,
|
|
|
|
display: announcement.display,
|
|
|
|
needConfirmationToRead: announcement.needConfirmationToRead,
|
2023-11-05 01:04:38 +01:00
|
|
|
silence: announcement.silence,
|
2023-08-13 13:12:29 +02:00
|
|
|
forYou: announcement.userId === me?.id,
|
|
|
|
isRead: reads.some(read => read.announcementId === announcement.id),
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|