]>
Commit | Line | Data |
---|---|---|
d26836cd C |
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' | |
8 | ||
9 | export type ImportFinishedForOwnerPayload = { | |
10 | videoImport: MVideoImportVideo | |
11 | success: boolean | |
12 | } | |
13 | ||
14 | export class ImportFinishedForOwner extends AbstractNotification <ImportFinishedForOwnerPayload> { | |
15 | private user: MUserDefault | |
16 | ||
17 | async prepare () { | |
18 | this.user = await UserModel.loadByVideoImportId(this.videoImport.id) | |
19 | } | |
20 | ||
21 | log () { | |
22 | logger.info('Notifying user %s its video import %s is finished.', this.user.username, this.videoImport.getTargetIdentifier()) | |
23 | } | |
24 | ||
25 | getSetting (user: MUserWithNotificationSetting) { | |
26 | return user.NotificationSetting.myVideoImportFinished | |
27 | } | |
28 | ||
29 | getTargetUsers () { | |
30 | if (!this.user) return [] | |
31 | ||
32 | return [ this.user ] | |
33 | } | |
34 | ||
35 | async createNotification (user: MUserWithNotificationSetting) { | |
36 | const notification = await UserNotificationModel.create<UserNotificationModelForApi>({ | |
37 | type: this.payload.success | |
38 | ? UserNotificationType.MY_VIDEO_IMPORT_SUCCESS | |
39 | : UserNotificationType.MY_VIDEO_IMPORT_ERROR, | |
40 | ||
41 | userId: user.id, | |
42 | videoImportId: this.videoImport.id | |
43 | }) | |
44 | notification.VideoImport = this.videoImport | |
45 | ||
46 | return notification | |
47 | } | |
48 | ||
49 | createEmail (to: string) { | |
50 | if (this.payload.success) return this.createSuccessEmail(to) | |
51 | ||
52 | return this.createFailEmail(to) | |
53 | } | |
54 | ||
55 | private createSuccessEmail (to: string) { | |
56 | const videoUrl = WEBSERVER.URL + this.videoImport.Video.getWatchStaticPath() | |
57 | ||
58 | return { | |
59 | to, | |
60 | subject: `Your video import ${this.videoImport.getTargetIdentifier()} is complete`, | |
61 | text: `Your video "${this.videoImport.getTargetIdentifier()}" just finished importing.`, | |
62 | locals: { | |
63 | title: 'Import complete', | |
64 | action: { | |
65 | text: 'View video', | |
66 | url: videoUrl | |
67 | } | |
68 | } | |
69 | } | |
70 | } | |
71 | ||
72 | private createFailEmail (to: string) { | |
73 | const importUrl = WEBSERVER.URL + '/my-library/video-imports' | |
74 | ||
75 | const text = | |
76 | `Your video import "${this.videoImport.getTargetIdentifier()}" encountered an error.` + | |
77 | '\n\n' + | |
78 | `See your videos import dashboard for more information: <a href="${importUrl}">${importUrl}</a>.` | |
79 | ||
80 | return { | |
81 | to, | |
82 | subject: `Your video import "${this.videoImport.getTargetIdentifier()}" encountered an error`, | |
83 | text, | |
84 | locals: { | |
85 | title: 'Import failed', | |
86 | action: { | |
87 | text: 'Review imports', | |
88 | url: importUrl | |
89 | } | |
90 | } | |
91 | } | |
92 | } | |
93 | ||
94 | private get videoImport () { | |
95 | return this.payload.videoImport | |
96 | } | |
97 | } |