feat: 通報を受けた際にメールまたはWebhookで通知を送出出来るようにする (#13758)
* feat: 通報を受けた際にメールまたはWebhookで通知を送出出来るようにする * モデログに対応&エンドポイントを単一オブジェクトでのサポートに変更(API経由で大量に作るシチュエーションもないと思うので) * fix spdx * fix migration * fix migration * fix models * add e2e webhook * tweak * fix modlog * fix bugs * add tests and fix bugs * add tests and fix bugs * add tests * fix path * regenerate locale * 混入除去 * 混入除去 * add abuseReportResolved * fix pnpm-lock.yaml * add abuseReportResolved test * fix bugs * fix ui * add tests * fix CHANGELOG.md * add tests * add RoleService.getModeratorIds tests * WebhookServiceをUserとSystemに分割 * fix CHANGELOG.md * fix test * insertOneを使う用に * fix * regenerate locales * revert version * separate webhook job queue * fix * 🎨 * Update QueueProcessorService.ts --------- Co-authored-by: osamu <46447427+sam-osamu@users.noreply.github.com> Co-authored-by: syuilo <4439005+syuilo@users.noreply.github.com>
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: syuilo and misskey-project
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { In } from 'typeorm';
|
||||
import { DI } from '@/di-symbols.js';
|
||||
import type { AbuseReportNotificationRecipientRepository, MiAbuseReportNotificationRecipient } from '@/models/_.js';
|
||||
import { bindThis } from '@/decorators.js';
|
||||
import { UserEntityService } from '@/core/entities/UserEntityService.js';
|
||||
import { Packed } from '@/misc/json-schema.js';
|
||||
import { SystemWebhookEntityService } from '@/core/entities/SystemWebhookEntityService.js';
|
||||
import { isNotNull } from '@/misc/is-not-null.js';
|
||||
|
||||
@Injectable()
|
||||
export class AbuseReportNotificationRecipientEntityService {
|
||||
constructor(
|
||||
@Inject(DI.abuseReportNotificationRecipientRepository)
|
||||
private abuseReportNotificationRecipientRepository: AbuseReportNotificationRecipientRepository,
|
||||
private userEntityService: UserEntityService,
|
||||
private systemWebhookEntityService: SystemWebhookEntityService,
|
||||
) {
|
||||
}
|
||||
|
||||
@bindThis
|
||||
public async pack(
|
||||
src: MiAbuseReportNotificationRecipient['id'] | MiAbuseReportNotificationRecipient,
|
||||
opts?: {
|
||||
users: Map<string, Packed<'UserLite'>>,
|
||||
webhooks: Map<string, Packed<'SystemWebhook'>>,
|
||||
},
|
||||
): Promise<Packed<'AbuseReportNotificationRecipient'>> {
|
||||
const recipient = typeof src === 'object'
|
||||
? src
|
||||
: await this.abuseReportNotificationRecipientRepository.findOneByOrFail({ id: src });
|
||||
const user = recipient.userId
|
||||
? (opts?.users.get(recipient.userId) ?? await this.userEntityService.pack<'UserLite'>(recipient.userId))
|
||||
: undefined;
|
||||
const webhook = recipient.systemWebhookId
|
||||
? (opts?.webhooks.get(recipient.systemWebhookId) ?? await this.systemWebhookEntityService.pack(recipient.systemWebhookId))
|
||||
: undefined;
|
||||
|
||||
return {
|
||||
id: recipient.id,
|
||||
isActive: recipient.isActive,
|
||||
updatedAt: recipient.updatedAt.toISOString(),
|
||||
name: recipient.name,
|
||||
method: recipient.method,
|
||||
userId: recipient.userId ?? undefined,
|
||||
user: user,
|
||||
systemWebhookId: recipient.systemWebhookId ?? undefined,
|
||||
systemWebhook: webhook,
|
||||
};
|
||||
}
|
||||
|
||||
@bindThis
|
||||
public async packMany(
|
||||
src: MiAbuseReportNotificationRecipient['id'][] | MiAbuseReportNotificationRecipient[],
|
||||
): Promise<Packed<'AbuseReportNotificationRecipient'>[]> {
|
||||
const objs = src.filter((it): it is MiAbuseReportNotificationRecipient => typeof it === 'object');
|
||||
const ids = src.filter((it): it is MiAbuseReportNotificationRecipient['id'] => typeof it === 'string');
|
||||
if (ids.length > 0) {
|
||||
objs.push(
|
||||
...await this.abuseReportNotificationRecipientRepository.findBy({ id: In(ids) }),
|
||||
);
|
||||
}
|
||||
|
||||
const userIds = objs.map(it => it.userId).filter(isNotNull);
|
||||
const users: Map<string, Packed<'UserLite'>> = (userIds.length > 0)
|
||||
? await this.userEntityService.packMany(userIds)
|
||||
.then(it => new Map(it.map(it => [it.id, it])))
|
||||
: new Map();
|
||||
|
||||
const systemWebhookIds = objs.map(it => it.systemWebhookId).filter(isNotNull);
|
||||
const systemWebhooks: Map<string, Packed<'SystemWebhook'>> = (systemWebhookIds.length > 0)
|
||||
? await this.systemWebhookEntityService.packMany(systemWebhookIds)
|
||||
.then(it => new Map(it.map(it => [it.id, it])))
|
||||
: new Map();
|
||||
|
||||
return Promise
|
||||
.all(
|
||||
objs.map(it => this.pack(it, { users: users, webhooks: systemWebhooks })),
|
||||
)
|
||||
.then(it => it.sort((a, b) => a.id.localeCompare(b.id)));
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: syuilo and misskey-project
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { In } from 'typeorm';
|
||||
import { DI } from '@/di-symbols.js';
|
||||
import type { MiSystemWebhook, SystemWebhooksRepository } from '@/models/_.js';
|
||||
import { bindThis } from '@/decorators.js';
|
||||
import { Packed } from '@/misc/json-schema.js';
|
||||
|
||||
@Injectable()
|
||||
export class SystemWebhookEntityService {
|
||||
constructor(
|
||||
@Inject(DI.systemWebhooksRepository)
|
||||
private systemWebhooksRepository: SystemWebhooksRepository,
|
||||
) {
|
||||
}
|
||||
|
||||
@bindThis
|
||||
public async pack(
|
||||
src: MiSystemWebhook['id'] | MiSystemWebhook,
|
||||
opts?: {
|
||||
webhooks: Map<string, MiSystemWebhook>
|
||||
},
|
||||
): Promise<Packed<'SystemWebhook'>> {
|
||||
const webhook = typeof src === 'object'
|
||||
? src
|
||||
: opts?.webhooks.get(src) ?? await this.systemWebhooksRepository.findOneByOrFail({ id: src });
|
||||
|
||||
return {
|
||||
id: webhook.id,
|
||||
isActive: webhook.isActive,
|
||||
updatedAt: webhook.updatedAt.toISOString(),
|
||||
latestSentAt: webhook.latestSentAt?.toISOString() ?? null,
|
||||
latestStatus: webhook.latestStatus,
|
||||
name: webhook.name,
|
||||
on: webhook.on,
|
||||
url: webhook.url,
|
||||
secret: webhook.secret,
|
||||
};
|
||||
}
|
||||
|
||||
@bindThis
|
||||
public async packMany(src: MiSystemWebhook['id'][] | MiSystemWebhook[]): Promise<Packed<'SystemWebhook'>[]> {
|
||||
if (src.length === 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const webhooks = Array.of<MiSystemWebhook>();
|
||||
webhooks.push(
|
||||
...src.filter((it): it is MiSystemWebhook => typeof it === 'object'),
|
||||
);
|
||||
|
||||
const ids = src.filter((it): it is MiSystemWebhook['id'] => typeof it === 'string');
|
||||
if (ids.length > 0) {
|
||||
webhooks.push(
|
||||
...await this.systemWebhooksRepository.findBy({ id: In(ids) }),
|
||||
);
|
||||
}
|
||||
|
||||
return Promise
|
||||
.all(
|
||||
webhooks.map(x =>
|
||||
this.pack(x, {
|
||||
webhooks: new Map(webhooks.map(x => [x.id, x])),
|
||||
}),
|
||||
),
|
||||
)
|
||||
.then(it => it.sort((a, b) => a.id.localeCompare(b.id)));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user