+ private async notifyReporterOfAbuseStateChange (abuse: MAbuseFull) {
+ // Only notify our users
+ if (abuse.ReporterAccount.isOwned() !== true) return
+
+ const url = this.getAbuseUrl(abuse)
+
+ logger.info('Notifying reporter of abuse % of state change.', url)
+
+ const reporter = await UserModel.loadByAccountActorId(abuse.ReporterAccount.actorId)
+
+ function settingGetter (user: MUserWithNotificationSetting) {
+ return user.NotificationSetting.abuseStateChange
+ }
+
+ async function notificationCreator (user: MUserWithNotificationSetting) {
+ const notification = await UserNotificationModel.create<UserNotificationModelForApi>({
+ type: UserNotificationType.ABUSE_STATE_CHANGE,
+ userId: user.id,
+ abuseId: abuse.id
+ })
+ notification.Abuse = abuse
+
+ return notification
+ }
+
+ function emailSender (emails: string[]) {
+ return Emailer.Instance.addAbuseStateChangeNotification(emails, abuse)
+ }
+
+ return this.notify({ users: [ reporter ], settingGetter, notificationCreator, emailSender })
+ }
+
+ private async notifyOfNewAbuseMessage (abuse: MAbuseFull, message: MAbuseMessage) {
+ const url = this.getAbuseUrl(abuse)
+ logger.info('Notifying reporter and moderators of new abuse message on %s.', url)
+
+ const accountMessage = await AccountModel.load(message.accountId)
+
+ function settingGetter (user: MUserWithNotificationSetting) {
+ return user.NotificationSetting.abuseNewMessage
+ }
+
+ async function notificationCreator (user: MUserWithNotificationSetting) {
+ const notification = await UserNotificationModel.create<UserNotificationModelForApi>({
+ type: UserNotificationType.ABUSE_NEW_MESSAGE,
+ userId: user.id,
+ abuseId: abuse.id
+ })
+ notification.Abuse = abuse
+
+ return notification
+ }
+
+ function emailSenderReporter (emails: string[]) {
+ return Emailer.Instance.addAbuseNewMessageNotification(emails, { target: 'reporter', abuse, message, accountMessage })
+ }
+
+ function emailSenderModerators (emails: string[]) {
+ return Emailer.Instance.addAbuseNewMessageNotification(emails, { target: 'moderator', abuse, message, accountMessage })
+ }
+
+ async function buildReporterOptions () {
+ // Only notify our users
+ if (abuse.ReporterAccount.isOwned() !== true) return
+
+ const reporter = await UserModel.loadByAccountActorId(abuse.ReporterAccount.actorId)
+ // Don't notify my own message
+ if (reporter.Account.id === message.accountId) return
+
+ return { users: [ reporter ], settingGetter, notificationCreator, emailSender: emailSenderReporter }
+ }
+
+ async function buildModeratorsOptions () {
+ let moderators = await UserModel.listWithRight(UserRight.MANAGE_ABUSES)
+ // Don't notify my own message
+ moderators = moderators.filter(m => m.Account.id !== message.accountId)
+
+ if (moderators.length === 0) return
+
+ return { users: moderators, settingGetter, notificationCreator, emailSender: emailSenderModerators }
+ }
+
+ const [ reporterOptions, moderatorsOptions ] = await Promise.all([
+ buildReporterOptions(),
+ buildModeratorsOptions()
+ ])
+
+ return Promise.all([
+ this.notify(reporterOptions),
+ this.notify(moderatorsOptions)
+ ])
+ }
+