1 import { logger } from '@server/helpers/logger'
2 import { WEBSERVER } from '@server/initializers/constants'
3 import { UserModel } from '@server/models/user/user'
4 import { UserNotificationModel } from '@server/models/user/user-notification'
5 import { MUserDefault, MUserWithNotificationSetting, MVideoImportVideo, UserNotificationModelForApi } from '@server/types/models'
6 import { UserNotificationType } from '@shared/models'
7 import { AbstractNotification } from '../common/abstract-notification'
9 export type ImportFinishedForOwnerPayload = {
10 videoImport: MVideoImportVideo
14 export class ImportFinishedForOwner extends AbstractNotification <ImportFinishedForOwnerPayload> {
15 private user: MUserDefault
18 this.user = await UserModel.loadByVideoImportId(this.videoImport.id)
22 logger.info('Notifying user %s its video import %s is finished.', this.user.username, this.videoImport.getTargetIdentifier())
25 getSetting (user: MUserWithNotificationSetting) {
26 return user.NotificationSetting.myVideoImportFinished
30 if (!this.user) return []
35 createNotification (user: MUserWithNotificationSetting) {
36 const notification = UserNotificationModel.build<UserNotificationModelForApi>({
37 type: this.payload.success
38 ? UserNotificationType.MY_VIDEO_IMPORT_SUCCESS
39 : UserNotificationType.MY_VIDEO_IMPORT_ERROR,
42 videoImportId: this.videoImport.id
44 notification.VideoImport = this.videoImport
49 createEmail (to: string) {
50 if (this.payload.success) return this.createSuccessEmail(to)
52 return this.createFailEmail(to)
55 private createSuccessEmail (to: string) {
56 const videoUrl = WEBSERVER.URL + this.videoImport.Video.getWatchStaticPath()
60 subject: `Your video import ${this.videoImport.getTargetIdentifier()} is complete`,
61 text: `Your video "${this.videoImport.getTargetIdentifier()}" just finished importing.`,
63 title: 'Import complete',
72 private createFailEmail (to: string) {
73 const importUrl = WEBSERVER.URL + '/my-library/video-imports'
76 `Your video import "${this.videoImport.getTargetIdentifier()}" encountered an error.` +
78 `See your videos import dashboard for more information: <a href="${importUrl}">${importUrl}</a>.`
82 subject: `Your video import "${this.videoImport.getTargetIdentifier()}" encountered an error`,
85 title: 'Import failed',
87 text: 'Review imports',
94 private get videoImport () {
95 return this.payload.videoImport