From d26836cd95e981d636006652927773c7943e77ce Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 30 Jul 2021 16:51:27 +0200 Subject: Refactor notifier --- server/lib/notifier/shared/instance/index.ts | 3 ++ .../instance/new-peertube-version-for-admins.ts | 54 ++++++++++++++++++++ .../instance/new-plugin-version-for-admins.ts | 58 ++++++++++++++++++++++ .../shared/instance/registration-for-moderators.ts | 49 ++++++++++++++++++ 4 files changed, 164 insertions(+) create mode 100644 server/lib/notifier/shared/instance/index.ts create mode 100644 server/lib/notifier/shared/instance/new-peertube-version-for-admins.ts create mode 100644 server/lib/notifier/shared/instance/new-plugin-version-for-admins.ts create mode 100644 server/lib/notifier/shared/instance/registration-for-moderators.ts (limited to 'server/lib/notifier/shared/instance') diff --git a/server/lib/notifier/shared/instance/index.ts b/server/lib/notifier/shared/instance/index.ts new file mode 100644 index 000000000..c3bb22aec --- /dev/null +++ b/server/lib/notifier/shared/instance/index.ts @@ -0,0 +1,3 @@ +export * from './new-peertube-version-for-admins' +export * from './new-plugin-version-for-admins' +export * from './registration-for-moderators' diff --git a/server/lib/notifier/shared/instance/new-peertube-version-for-admins.ts b/server/lib/notifier/shared/instance/new-peertube-version-for-admins.ts new file mode 100644 index 000000000..ab5bfb1ac --- /dev/null +++ b/server/lib/notifier/shared/instance/new-peertube-version-for-admins.ts @@ -0,0 +1,54 @@ +import { logger } from '@server/helpers/logger' +import { UserModel } from '@server/models/user/user' +import { UserNotificationModel } from '@server/models/user/user-notification' +import { MApplication, MUserDefault, MUserWithNotificationSetting, UserNotificationModelForApi } from '@server/types/models' +import { UserNotificationType, UserRight } from '@shared/models' +import { AbstractNotification } from '../common/abstract-notification' + +export type NewPeerTubeVersionForAdminsPayload = { + application: MApplication + latestVersion: string +} + +export class NewPeerTubeVersionForAdmins extends AbstractNotification { + private admins: MUserDefault[] + + async prepare () { + // Use the debug right to know who is an administrator + this.admins = await UserModel.listWithRight(UserRight.MANAGE_DEBUG) + } + + log () { + logger.info('Notifying %s admins of new PeerTube version %s.', this.admins.length, this.payload.latestVersion) + } + + getSetting (user: MUserWithNotificationSetting) { + return user.NotificationSetting.newPeerTubeVersion + } + + getTargetUsers () { + return this.admins + } + + async createNotification (user: MUserWithNotificationSetting) { + const notification = await UserNotificationModel.create({ + type: UserNotificationType.NEW_PEERTUBE_VERSION, + userId: user.id, + applicationId: this.payload.application.id + }) + notification.Application = this.payload.application + + return notification + } + + async createEmail (to: string) { + return { + to, + template: 'peertube-version-new', + subject: `A new PeerTube version is available: ${this.payload.latestVersion}`, + locals: { + latestVersion: this.payload.latestVersion + } + } + } +} diff --git a/server/lib/notifier/shared/instance/new-plugin-version-for-admins.ts b/server/lib/notifier/shared/instance/new-plugin-version-for-admins.ts new file mode 100644 index 000000000..e5e456a70 --- /dev/null +++ b/server/lib/notifier/shared/instance/new-plugin-version-for-admins.ts @@ -0,0 +1,58 @@ +import { logger } from '@server/helpers/logger' +import { WEBSERVER } from '@server/initializers/constants' +import { UserModel } from '@server/models/user/user' +import { UserNotificationModel } from '@server/models/user/user-notification' +import { MPlugin, MUserDefault, MUserWithNotificationSetting, UserNotificationModelForApi } from '@server/types/models' +import { UserNotificationType, UserRight } from '@shared/models' +import { AbstractNotification } from '../common/abstract-notification' + +export class NewPluginVersionForAdmins extends AbstractNotification { + private admins: MUserDefault[] + + async prepare () { + // Use the debug right to know who is an administrator + this.admins = await UserModel.listWithRight(UserRight.MANAGE_DEBUG) + } + + log () { + logger.info('Notifying %s admins of new PeerTube version %s.', this.admins.length, this.payload.latestVersion) + } + + getSetting (user: MUserWithNotificationSetting) { + return user.NotificationSetting.newPluginVersion + } + + getTargetUsers () { + return this.admins + } + + async createNotification (user: MUserWithNotificationSetting) { + const notification = await UserNotificationModel.create({ + type: UserNotificationType.NEW_PLUGIN_VERSION, + userId: user.id, + pluginId: this.plugin.id + }) + notification.Plugin = this.plugin + + return notification + } + + async createEmail (to: string) { + const pluginUrl = WEBSERVER.URL + '/admin/plugins/list-installed?pluginType=' + this.plugin.type + + return { + to, + template: 'plugin-version-new', + subject: `A new plugin/theme version is available: ${this.plugin.name}@${this.plugin.latestVersion}`, + locals: { + pluginName: this.plugin.name, + latestVersion: this.plugin.latestVersion, + pluginUrl + } + } + } + + private get plugin () { + return this.payload + } +} diff --git a/server/lib/notifier/shared/instance/registration-for-moderators.ts b/server/lib/notifier/shared/instance/registration-for-moderators.ts new file mode 100644 index 000000000..4deb5a2cc --- /dev/null +++ b/server/lib/notifier/shared/instance/registration-for-moderators.ts @@ -0,0 +1,49 @@ +import { logger } from '@server/helpers/logger' +import { CONFIG } from '@server/initializers/config' +import { UserModel } from '@server/models/user/user' +import { UserNotificationModel } from '@server/models/user/user-notification' +import { MUserDefault, MUserWithNotificationSetting, UserNotificationModelForApi } from '@server/types/models' +import { UserNotificationType, UserRight } from '@shared/models' +import { AbstractNotification } from '../common/abstract-notification' + +export class RegistrationForModerators extends AbstractNotification { + private moderators: MUserDefault[] + + async prepare () { + this.moderators = await UserModel.listWithRight(UserRight.MANAGE_USERS) + } + + log () { + logger.info('Notifying %s moderators of new user registration of %s.', this.moderators.length, this.payload.username) + } + + getSetting (user: MUserWithNotificationSetting) { + return user.NotificationSetting.newUserRegistration + } + + getTargetUsers () { + return this.moderators + } + + async createNotification (user: MUserWithNotificationSetting) { + const notification = await UserNotificationModel.create({ + type: UserNotificationType.NEW_USER_REGISTRATION, + userId: user.id, + accountId: this.payload.Account.id + }) + notification.Account = this.payload.Account + + return notification + } + + async createEmail (to: string) { + return { + template: 'user-registered', + to, + subject: `a new user registered on ${CONFIG.INSTANCE.NAME}: ${this.payload.username}`, + locals: { + user: this.payload + } + } + } +} -- cgit v1.2.3