]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/notifier/shared/abuse/abuse-state-change-for-reporter.ts
Refactor notifier
[github/Chocobozzz/PeerTube.git] / server / lib / notifier / shared / abuse / abuse-state-change-for-reporter.ts
diff --git a/server/lib/notifier/shared/abuse/abuse-state-change-for-reporter.ts b/server/lib/notifier/shared/abuse/abuse-state-change-for-reporter.ts
new file mode 100644 (file)
index 0000000..968b5bc
--- /dev/null
@@ -0,0 +1,74 @@
+import { logger } from '@server/helpers/logger'
+import { WEBSERVER } from '@server/initializers/constants'
+import { getAbuseTargetUrl } from '@server/lib/activitypub/url'
+import { UserModel } from '@server/models/user/user'
+import { UserNotificationModel } from '@server/models/user/user-notification'
+import { MAbuseFull, MUserDefault, MUserWithNotificationSetting, UserNotificationModelForApi } from '@server/types/models'
+import { AbuseState, UserNotificationType } from '@shared/models'
+import { AbstractNotification } from '../common/abstract-notification'
+
+export class AbuseStateChangeForReporter extends AbstractNotification <MAbuseFull> {
+
+  private user: MUserDefault
+
+  async prepare () {
+    const reporter = this.abuse.ReporterAccount
+    if (reporter.isOwned() !== true) return
+
+    this.user = await UserModel.loadByAccountActorId(this.abuse.ReporterAccount.actorId)
+  }
+
+  log () {
+    logger.info('Notifying reporter of abuse % of state change.', getAbuseTargetUrl(this.abuse))
+  }
+
+  getSetting (user: MUserWithNotificationSetting) {
+    return user.NotificationSetting.abuseStateChange
+  }
+
+  getTargetUsers () {
+    if (!this.user) return []
+
+    return [ this.user ]
+  }
+
+  async createNotification (user: MUserWithNotificationSetting) {
+    const notification = await UserNotificationModel.create<UserNotificationModelForApi>({
+      type: UserNotificationType.ABUSE_STATE_CHANGE,
+      userId: user.id,
+      abuseId: this.abuse.id
+    })
+    notification.Abuse = this.abuse
+
+    return notification
+  }
+
+  createEmail (to: string) {
+    const text = this.abuse.state === AbuseState.ACCEPTED
+      ? 'Report #' + this.abuse.id + ' has been accepted'
+      : 'Report #' + this.abuse.id + ' has been rejected'
+
+    const abuseUrl = WEBSERVER.URL + '/my-account/abuses?search=%23' + this.abuse.id
+
+    const action = {
+      text,
+      url: abuseUrl
+    }
+
+    return {
+      template: 'abuse-state-change',
+      to,
+      subject: text,
+      locals: {
+        action,
+        abuseId: this.abuse.id,
+        abuseUrl,
+        isAccepted: this.abuse.state === AbuseState.ACCEPTED
+      }
+    }
+  }
+
+  private get abuse () {
+    return this.payload
+  }
+}