]>
Commit | Line | Data |
---|---|---|
d95d1559 C |
1 | import { readFileSync } from 'fs-extra' |
2 | import { merge } from 'lodash' | |
ecb4e35f | 3 | import { createTransport, Transporter } from 'nodemailer' |
d95d1559 C |
4 | import { join } from 'path' |
5 | import { VideoChannelModel } from '@server/models/video/video-channel' | |
6 | import { MVideoBlacklistLightVideo, MVideoBlacklistVideo } from '@server/types/models/video/video-blacklist' | |
7 | import { MVideoImport, MVideoImportVideo } from '@server/types/models/video/video-import' | |
594d3e48 | 8 | import { AbuseState, EmailPayload, UserAbuse } from '@shared/models' |
d95d1559 | 9 | import { SendEmailOptions } from '../../shared/models/server/emailer.model' |
df4c603d | 10 | import { isTestInstance, root } from '../helpers/core-utils' |
05e67d62 | 11 | import { bunyanLogger, logger } from '../helpers/logger' |
4c1c1709 | 12 | import { CONFIG, isEmailEnabled } from '../initializers/config' |
6dd9de95 | 13 | import { WEBSERVER } from '../initializers/constants' |
d573926e | 14 | import { MAbuseFull, MAbuseMessage, MAccountDefault, MActorFollowActors, MActorFollowFull, MUser } from '../types/models' |
d95d1559 C |
15 | import { MCommentOwnerVideo, MVideo, MVideoAccountLight } from '../types/models/video' |
16 | import { JobQueue } from './job-queue' | |
17 | ||
98b94643 K |
18 | const sanitizeHtml = require('sanitize-html') |
19 | const markdownItEmoji = require('markdown-it-emoji/light') | |
20 | const MarkdownItClass = require('markdown-it') | |
21 | const markdownIt = new MarkdownItClass('default', { linkify: true, breaks: true, html: true }) | |
22 | ||
23 | markdownIt.enable([ | |
24 | 'linkify', | |
25 | 'autolink', | |
26 | 'emphasis', | |
27 | 'link', | |
28 | 'newline', | |
29 | 'list' | |
30 | ]) | |
31 | ||
32 | markdownIt.use(markdownItEmoji) | |
33 | ||
34 | const toSafeHtml = text => { | |
35 | // Restore line feed | |
36 | const textWithLineFeed = text.replace(/<br.?\/?>/g, '\r\n') | |
37 | ||
38 | // Convert possible markdown (emojis, emphasis and lists) to html | |
39 | const html = markdownIt.render(textWithLineFeed) | |
40 | ||
41 | // Convert to safe Html | |
42 | return sanitizeHtml(html, { | |
43 | allowedTags: [ 'a', 'p', 'span', 'br', 'strong', 'em', 'ul', 'ol', 'li' ], | |
44 | allowedSchemes: [ 'http', 'https' ], | |
45 | allowedAttributes: { | |
46 | a: [ 'href', 'class', 'target', 'rel' ] | |
47 | }, | |
48 | transformTags: { | |
49 | a: (tagName, attribs) => { | |
50 | let rel = 'noopener noreferrer' | |
51 | if (attribs.rel === 'me') rel += ' me' | |
52 | ||
53 | return { | |
54 | tagName, | |
55 | attribs: Object.assign(attribs, { | |
56 | target: '_blank', | |
57 | rel | |
58 | }) | |
59 | } | |
60 | } | |
61 | } | |
62 | }) | |
63 | } | |
64 | ||
df4c603d | 65 | const Email = require('email-templates') |
dee77e76 | 66 | |
ecb4e35f C |
67 | class Emailer { |
68 | ||
69 | private static instance: Emailer | |
70 | private initialized = false | |
71 | private transporter: Transporter | |
72 | ||
a1587156 C |
73 | private constructor () { |
74 | } | |
ecb4e35f C |
75 | |
76 | init () { | |
77 | // Already initialized | |
78 | if (this.initialized === true) return | |
79 | this.initialized = true | |
80 | ||
887e1a03 | 81 | if (isEmailEnabled()) { |
ed3f089c IB |
82 | if (CONFIG.SMTP.TRANSPORT === 'smtp') { |
83 | logger.info('Using %s:%s as SMTP server.', CONFIG.SMTP.HOSTNAME, CONFIG.SMTP.PORT) | |
84 | ||
85 | let tls | |
86 | if (CONFIG.SMTP.CA_FILE) { | |
87 | tls = { | |
88 | ca: [ readFileSync(CONFIG.SMTP.CA_FILE) ] | |
89 | } | |
ecb4e35f | 90 | } |
ecb4e35f | 91 | |
ed3f089c IB |
92 | let auth |
93 | if (CONFIG.SMTP.USERNAME && CONFIG.SMTP.PASSWORD) { | |
94 | auth = { | |
95 | user: CONFIG.SMTP.USERNAME, | |
96 | pass: CONFIG.SMTP.PASSWORD | |
97 | } | |
f076daa7 | 98 | } |
f076daa7 | 99 | |
ed3f089c IB |
100 | this.transporter = createTransport({ |
101 | host: CONFIG.SMTP.HOSTNAME, | |
102 | port: CONFIG.SMTP.PORT, | |
103 | secure: CONFIG.SMTP.TLS, | |
104 | debug: CONFIG.LOG.LEVEL === 'debug', | |
105 | logger: bunyanLogger as any, | |
106 | ignoreTLS: CONFIG.SMTP.DISABLE_STARTTLS, | |
107 | tls, | |
108 | auth | |
109 | }) | |
110 | } else { // sendmail | |
111 | logger.info('Using sendmail to send emails') | |
112 | ||
113 | this.transporter = createTransport({ | |
114 | sendmail: true, | |
115 | newline: 'unix', | |
9afa0901 | 116 | path: CONFIG.SMTP.SENDMAIL |
ed3f089c IB |
117 | }) |
118 | } | |
ecb4e35f C |
119 | } else { |
120 | if (!isTestInstance()) { | |
121 | logger.error('Cannot use SMTP server because of lack of configuration. PeerTube will not be able to send mails!') | |
122 | } | |
123 | } | |
124 | } | |
125 | ||
d3e56c0c | 126 | static isEnabled () { |
ed3f089c IB |
127 | if (CONFIG.SMTP.TRANSPORT === 'sendmail') { |
128 | return !!CONFIG.SMTP.SENDMAIL | |
129 | } else if (CONFIG.SMTP.TRANSPORT === 'smtp') { | |
130 | return !!CONFIG.SMTP.HOSTNAME && !!CONFIG.SMTP.PORT | |
131 | } else { | |
132 | return false | |
133 | } | |
3b3b1820 C |
134 | } |
135 | ||
ecb4e35f | 136 | async checkConnectionOrDie () { |
ed3f089c | 137 | if (!this.transporter || CONFIG.SMTP.TRANSPORT !== 'smtp') return |
ecb4e35f | 138 | |
3d3441d6 C |
139 | logger.info('Testing SMTP server...') |
140 | ||
ecb4e35f C |
141 | try { |
142 | const success = await this.transporter.verify() | |
143 | if (success !== true) this.dieOnConnectionFailure() | |
144 | ||
145 | logger.info('Successfully connected to SMTP server.') | |
146 | } catch (err) { | |
147 | this.dieOnConnectionFailure(err) | |
148 | } | |
149 | } | |
150 | ||
453e83ea | 151 | addNewVideoFromSubscriberNotification (to: string[], video: MVideoAccountLight) { |
cef534ed | 152 | const channelName = video.VideoChannel.getDisplayName() |
6dd9de95 | 153 | const videoUrl = WEBSERVER.URL + video.getWatchStaticPath() |
cef534ed | 154 | |
ecb4e35f | 155 | const emailPayload: EmailPayload = { |
cef534ed | 156 | to, |
df4c603d RK |
157 | subject: channelName + ' just published a new video', |
158 | text: `Your subscription ${channelName} just published a new video: "${video.name}".`, | |
159 | locals: { | |
160 | title: 'New content ', | |
161 | action: { | |
162 | text: 'View video', | |
163 | url: videoUrl | |
164 | } | |
165 | } | |
ecb4e35f C |
166 | } |
167 | ||
168 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
169 | } | |
170 | ||
8424c402 | 171 | addNewFollowNotification (to: string[], actorFollow: MActorFollowFull, followType: 'account' | 'channel') { |
f7cc67b4 C |
172 | const followingName = (actorFollow.ActorFollowing.VideoChannel || actorFollow.ActorFollowing.Account).getDisplayName() |
173 | ||
f7cc67b4 | 174 | const emailPayload: EmailPayload = { |
df4c603d | 175 | template: 'follower-on-channel', |
f7cc67b4 | 176 | to, |
df4c603d RK |
177 | subject: `New follower on your channel ${followingName}`, |
178 | locals: { | |
179 | followerName: actorFollow.ActorFollower.Account.getDisplayName(), | |
180 | followerUrl: actorFollow.ActorFollower.url, | |
181 | followingName, | |
182 | followingUrl: actorFollow.ActorFollowing.url, | |
183 | followType | |
184 | } | |
f7cc67b4 C |
185 | } |
186 | ||
187 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
188 | } | |
189 | ||
453e83ea | 190 | addNewInstanceFollowerNotification (to: string[], actorFollow: MActorFollowActors) { |
883993c8 C |
191 | const awaitingApproval = actorFollow.state === 'pending' ? ' awaiting manual approval.' : '' |
192 | ||
883993c8 C |
193 | const emailPayload: EmailPayload = { |
194 | to, | |
df4c603d RK |
195 | subject: 'New instance follower', |
196 | text: `Your instance has a new follower: ${actorFollow.ActorFollower.url}${awaitingApproval}.`, | |
197 | locals: { | |
198 | title: 'New instance follower', | |
199 | action: { | |
200 | text: 'Review followers', | |
201 | url: WEBSERVER.URL + '/admin/follows/followers-list' | |
202 | } | |
203 | } | |
883993c8 C |
204 | } |
205 | ||
206 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
207 | } | |
208 | ||
8424c402 | 209 | addAutoInstanceFollowingNotification (to: string[], actorFollow: MActorFollowActors) { |
df4c603d | 210 | const instanceUrl = actorFollow.ActorFollowing.url |
8424c402 C |
211 | const emailPayload: EmailPayload = { |
212 | to, | |
df4c603d RK |
213 | subject: 'Auto instance following', |
214 | text: `Your instance automatically followed a new instance: <a href="${instanceUrl}">${instanceUrl}</a>.` | |
8424c402 C |
215 | } |
216 | ||
217 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
218 | } | |
219 | ||
453e83ea | 220 | myVideoPublishedNotification (to: string[], video: MVideo) { |
6dd9de95 | 221 | const videoUrl = WEBSERVER.URL + video.getWatchStaticPath() |
dc133480 | 222 | |
dc133480 C |
223 | const emailPayload: EmailPayload = { |
224 | to, | |
df4c603d RK |
225 | subject: `Your video ${video.name} has been published`, |
226 | text: `Your video "${video.name}" has been published.`, | |
227 | locals: { | |
228 | title: 'You video is live', | |
229 | action: { | |
230 | text: 'View video', | |
231 | url: videoUrl | |
232 | } | |
233 | } | |
dc133480 C |
234 | } |
235 | ||
236 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
237 | } | |
238 | ||
453e83ea | 239 | myVideoImportSuccessNotification (to: string[], videoImport: MVideoImportVideo) { |
6dd9de95 | 240 | const videoUrl = WEBSERVER.URL + videoImport.Video.getWatchStaticPath() |
dc133480 | 241 | |
dc133480 C |
242 | const emailPayload: EmailPayload = { |
243 | to, | |
df4c603d RK |
244 | subject: `Your video import ${videoImport.getTargetIdentifier()} is complete`, |
245 | text: `Your video "${videoImport.getTargetIdentifier()}" just finished importing.`, | |
246 | locals: { | |
247 | title: 'Import complete', | |
248 | action: { | |
249 | text: 'View video', | |
250 | url: videoUrl | |
251 | } | |
252 | } | |
dc133480 C |
253 | } |
254 | ||
255 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
256 | } | |
257 | ||
453e83ea | 258 | myVideoImportErrorNotification (to: string[], videoImport: MVideoImport) { |
6dd9de95 | 259 | const importUrl = WEBSERVER.URL + '/my-account/video-imports' |
dc133480 | 260 | |
df4c603d RK |
261 | const text = |
262 | `Your video import "${videoImport.getTargetIdentifier()}" encountered an error.` + | |
a1587156 | 263 | '\n\n' + |
df4c603d | 264 | `See your videos import dashboard for more information: <a href="${importUrl}">${importUrl}</a>.` |
dc133480 C |
265 | |
266 | const emailPayload: EmailPayload = { | |
267 | to, | |
df4c603d RK |
268 | subject: `Your video import "${videoImport.getTargetIdentifier()}" encountered an error`, |
269 | text, | |
270 | locals: { | |
271 | title: 'Import failed', | |
272 | action: { | |
273 | text: 'Review imports', | |
274 | url: importUrl | |
275 | } | |
276 | } | |
dc133480 C |
277 | } |
278 | ||
279 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
280 | } | |
281 | ||
453e83ea | 282 | addNewCommentOnMyVideoNotification (to: string[], comment: MCommentOwnerVideo) { |
cef534ed | 283 | const video = comment.Video |
df4c603d | 284 | const videoUrl = WEBSERVER.URL + comment.Video.getWatchStaticPath() |
6dd9de95 | 285 | const commentUrl = WEBSERVER.URL + comment.getCommentStaticPath() |
98b94643 | 286 | const commentHtml = toSafeHtml(comment.text) |
cef534ed | 287 | |
d9eaee39 | 288 | const emailPayload: EmailPayload = { |
df4c603d | 289 | template: 'video-comment-new', |
cef534ed | 290 | to, |
df4c603d RK |
291 | subject: 'New comment on your video ' + video.name, |
292 | locals: { | |
293 | accountName: comment.Account.getDisplayName(), | |
294 | accountUrl: comment.Account.Actor.url, | |
295 | comment, | |
98b94643 | 296 | commentHtml, |
df4c603d RK |
297 | video, |
298 | videoUrl, | |
299 | action: { | |
300 | text: 'View comment', | |
301 | url: commentUrl | |
302 | } | |
303 | } | |
d9eaee39 JM |
304 | } |
305 | ||
306 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
307 | } | |
308 | ||
453e83ea | 309 | addNewCommentMentionNotification (to: string[], comment: MCommentOwnerVideo) { |
f7cc67b4 C |
310 | const accountName = comment.Account.getDisplayName() |
311 | const video = comment.Video | |
df4c603d | 312 | const videoUrl = WEBSERVER.URL + comment.Video.getWatchStaticPath() |
6dd9de95 | 313 | const commentUrl = WEBSERVER.URL + comment.getCommentStaticPath() |
98b94643 | 314 | const commentHtml = toSafeHtml(comment.text) |
f7cc67b4 | 315 | |
f7cc67b4 | 316 | const emailPayload: EmailPayload = { |
df4c603d | 317 | template: 'video-comment-mention', |
f7cc67b4 | 318 | to, |
df4c603d RK |
319 | subject: 'Mention on video ' + video.name, |
320 | locals: { | |
321 | comment, | |
98b94643 | 322 | commentHtml, |
df4c603d RK |
323 | video, |
324 | videoUrl, | |
325 | accountName, | |
326 | action: { | |
327 | text: 'View comment', | |
328 | url: commentUrl | |
329 | } | |
330 | } | |
f7cc67b4 C |
331 | } |
332 | ||
333 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
334 | } | |
335 | ||
d95d1559 | 336 | addAbuseModeratorsNotification (to: string[], parameters: { |
edbc9325 | 337 | abuse: UserAbuse |
d95d1559 | 338 | abuseInstance: MAbuseFull |
df4c603d RK |
339 | reporter: string |
340 | }) { | |
d95d1559 | 341 | const { abuse, abuseInstance, reporter } = parameters |
ba75d268 | 342 | |
d95d1559 C |
343 | const action = { |
344 | text: 'View report #' + abuse.id, | |
345 | url: WEBSERVER.URL + '/admin/moderation/abuses/list?search=%23' + abuse.id | |
346 | } | |
347 | ||
348 | let emailPayload: EmailPayload | |
349 | ||
350 | if (abuseInstance.VideoAbuse) { | |
351 | const video = abuseInstance.VideoAbuse.Video | |
352 | const videoUrl = WEBSERVER.URL + video.getWatchStaticPath() | |
353 | ||
354 | emailPayload = { | |
355 | template: 'video-abuse-new', | |
356 | to, | |
357 | subject: `New video abuse report from ${reporter}`, | |
358 | locals: { | |
359 | videoUrl, | |
360 | isLocal: video.remote === false, | |
361 | videoCreatedAt: new Date(video.createdAt).toLocaleString(), | |
362 | videoPublishedAt: new Date(video.publishedAt).toLocaleString(), | |
363 | videoName: video.name, | |
364 | reason: abuse.reason, | |
cfde28ba C |
365 | videoChannel: abuse.video.channel, |
366 | reporter, | |
d95d1559 C |
367 | action |
368 | } | |
369 | } | |
370 | } else if (abuseInstance.VideoCommentAbuse) { | |
371 | const comment = abuseInstance.VideoCommentAbuse.VideoComment | |
372 | const commentUrl = WEBSERVER.URL + comment.Video.getWatchStaticPath() + ';threadId=' + comment.getThreadId() | |
373 | ||
374 | emailPayload = { | |
4f32032f | 375 | template: 'video-comment-abuse-new', |
d95d1559 C |
376 | to, |
377 | subject: `New comment abuse report from ${reporter}`, | |
378 | locals: { | |
379 | commentUrl, | |
310b5219 | 380 | videoName: comment.Video.name, |
d95d1559 C |
381 | isLocal: comment.isOwned(), |
382 | commentCreatedAt: new Date(comment.createdAt).toLocaleString(), | |
383 | reason: abuse.reason, | |
384 | flaggedAccount: abuseInstance.FlaggedAccount.getDisplayName(), | |
cfde28ba | 385 | reporter, |
d95d1559 C |
386 | action |
387 | } | |
388 | } | |
389 | } else { | |
390 | const account = abuseInstance.FlaggedAccount | |
391 | const accountUrl = account.getClientUrl() | |
392 | ||
393 | emailPayload = { | |
394 | template: 'account-abuse-new', | |
395 | to, | |
396 | subject: `New account abuse report from ${reporter}`, | |
397 | locals: { | |
398 | accountUrl, | |
399 | accountDisplayName: account.getDisplayName(), | |
400 | isLocal: account.isOwned(), | |
401 | reason: abuse.reason, | |
cfde28ba | 402 | reporter, |
d95d1559 | 403 | action |
df4c603d RK |
404 | } |
405 | } | |
ba75d268 C |
406 | } |
407 | ||
408 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
409 | } | |
410 | ||
594d3e48 C |
411 | addAbuseStateChangeNotification (to: string[], abuse: MAbuseFull) { |
412 | const text = abuse.state === AbuseState.ACCEPTED | |
413 | ? 'Report #' + abuse.id + ' has been accepted' | |
414 | : 'Report #' + abuse.id + ' has been rejected' | |
415 | ||
d573926e C |
416 | const abuseUrl = WEBSERVER.URL + '/my-account/abuses?search=%23' + abuse.id |
417 | ||
594d3e48 C |
418 | const action = { |
419 | text, | |
d573926e | 420 | url: abuseUrl |
594d3e48 C |
421 | } |
422 | ||
423 | const emailPayload: EmailPayload = { | |
424 | template: 'abuse-state-change', | |
425 | to, | |
426 | subject: text, | |
427 | locals: { | |
428 | action, | |
429 | abuseId: abuse.id, | |
d573926e | 430 | abuseUrl, |
594d3e48 C |
431 | isAccepted: abuse.state === AbuseState.ACCEPTED |
432 | } | |
433 | } | |
434 | ||
435 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
436 | } | |
437 | ||
d573926e C |
438 | addAbuseNewMessageNotification ( |
439 | to: string[], | |
440 | options: { | |
441 | target: 'moderator' | 'reporter' | |
442 | abuse: MAbuseFull | |
443 | message: MAbuseMessage | |
444 | accountMessage: MAccountDefault | |
445 | }) { | |
446 | const { abuse, target, message, accountMessage } = options | |
447 | ||
448 | const text = 'New message on report #' + abuse.id | |
449 | const abuseUrl = target === 'moderator' | |
450 | ? WEBSERVER.URL + '/admin/moderation/abuses/list?search=%23' + abuse.id | |
451 | : WEBSERVER.URL + '/my-account/abuses?search=%23' + abuse.id | |
594d3e48 | 452 | |
594d3e48 C |
453 | const action = { |
454 | text, | |
d573926e | 455 | url: abuseUrl |
594d3e48 C |
456 | } |
457 | ||
458 | const emailPayload: EmailPayload = { | |
459 | template: 'abuse-new-message', | |
460 | to, | |
461 | subject: text, | |
462 | locals: { | |
d573926e | 463 | abuseId: abuse.id, |
594d3e48 | 464 | abuseUrl: action.url, |
d573926e | 465 | messageAccountName: accountMessage.getDisplayName(), |
594d3e48 C |
466 | messageText: message.message, |
467 | action | |
468 | } | |
469 | } | |
470 | ||
471 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
472 | } | |
473 | ||
df4c603d | 474 | async addVideoAutoBlacklistModeratorsNotification (to: string[], videoBlacklist: MVideoBlacklistLightVideo) { |
6dd9de95 | 475 | const VIDEO_AUTO_BLACKLIST_URL = WEBSERVER.URL + '/admin/moderation/video-auto-blacklist/list' |
8424c402 | 476 | const videoUrl = WEBSERVER.URL + videoBlacklist.Video.getWatchStaticPath() |
df4c603d | 477 | const channel = (await VideoChannelModel.loadByIdAndPopulateAccount(videoBlacklist.Video.channelId)).toFormattedSummaryJSON() |
7ccddd7b JM |
478 | |
479 | const emailPayload: EmailPayload = { | |
df4c603d | 480 | template: 'video-auto-blacklist-new', |
7ccddd7b | 481 | to, |
df4c603d RK |
482 | subject: 'A new video is pending moderation', |
483 | locals: { | |
484 | channel, | |
485 | videoUrl, | |
486 | videoName: videoBlacklist.Video.name, | |
487 | action: { | |
488 | text: 'Review autoblacklist', | |
489 | url: VIDEO_AUTO_BLACKLIST_URL | |
490 | } | |
491 | } | |
7ccddd7b JM |
492 | } |
493 | ||
494 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
495 | } | |
496 | ||
453e83ea | 497 | addNewUserRegistrationNotification (to: string[], user: MUser) { |
f7cc67b4 | 498 | const emailPayload: EmailPayload = { |
df4c603d | 499 | template: 'user-registered', |
f7cc67b4 | 500 | to, |
df4c603d RK |
501 | subject: `a new user registered on ${WEBSERVER.HOST}: ${user.username}`, |
502 | locals: { | |
503 | user | |
504 | } | |
f7cc67b4 C |
505 | } |
506 | ||
507 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
508 | } | |
509 | ||
453e83ea | 510 | addVideoBlacklistNotification (to: string[], videoBlacklist: MVideoBlacklistVideo) { |
cef534ed | 511 | const videoName = videoBlacklist.Video.name |
6dd9de95 | 512 | const videoUrl = WEBSERVER.URL + videoBlacklist.Video.getWatchStaticPath() |
26b7305a | 513 | |
cef534ed | 514 | const reasonString = videoBlacklist.reason ? ` for the following reason: ${videoBlacklist.reason}` : '' |
6dd9de95 | 515 | const blockedString = `Your video ${videoName} (${videoUrl} on ${WEBSERVER.HOST} has been blacklisted${reasonString}.` |
26b7305a | 516 | |
26b7305a | 517 | const emailPayload: EmailPayload = { |
cef534ed | 518 | to, |
df4c603d RK |
519 | subject: `Video ${videoName} blacklisted`, |
520 | text: blockedString, | |
521 | locals: { | |
522 | title: 'Your video was blacklisted' | |
523 | } | |
26b7305a C |
524 | } |
525 | ||
526 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
527 | } | |
528 | ||
453e83ea | 529 | addVideoUnblacklistNotification (to: string[], video: MVideo) { |
6dd9de95 | 530 | const videoUrl = WEBSERVER.URL + video.getWatchStaticPath() |
26b7305a | 531 | |
26b7305a | 532 | const emailPayload: EmailPayload = { |
cef534ed | 533 | to, |
df4c603d RK |
534 | subject: `Video ${video.name} unblacklisted`, |
535 | text: `Your video "${video.name}" (${videoUrl}) on ${WEBSERVER.HOST} has been unblacklisted.`, | |
536 | locals: { | |
537 | title: 'Your video was unblacklisted' | |
538 | } | |
26b7305a C |
539 | } |
540 | ||
541 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
542 | } | |
543 | ||
963023ab | 544 | addPasswordResetEmailJob (username: string, to: string, resetPasswordUrl: string) { |
cef534ed | 545 | const emailPayload: EmailPayload = { |
df4c603d | 546 | template: 'password-reset', |
cef534ed | 547 | to: [ to ], |
df4c603d RK |
548 | subject: 'Reset your account password', |
549 | locals: { | |
963023ab | 550 | username, |
df4c603d RK |
551 | resetPasswordUrl |
552 | } | |
cef534ed C |
553 | } |
554 | ||
555 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
556 | } | |
557 | ||
df4c603d | 558 | addPasswordCreateEmailJob (username: string, to: string, createPasswordUrl: string) { |
45f1bd72 | 559 | const emailPayload: EmailPayload = { |
df4c603d | 560 | template: 'password-create', |
45f1bd72 | 561 | to: [ to ], |
df4c603d RK |
562 | subject: 'Create your account password', |
563 | locals: { | |
564 | username, | |
565 | createPasswordUrl | |
566 | } | |
45f1bd72 JL |
567 | } |
568 | ||
569 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
570 | } | |
571 | ||
963023ab | 572 | addVerifyEmailJob (username: string, to: string, verifyEmailUrl: string) { |
cef534ed | 573 | const emailPayload: EmailPayload = { |
df4c603d | 574 | template: 'verify-email', |
cef534ed | 575 | to: [ to ], |
df4c603d RK |
576 | subject: `Verify your email on ${WEBSERVER.HOST}`, |
577 | locals: { | |
963023ab | 578 | username, |
df4c603d RK |
579 | verifyEmailUrl |
580 | } | |
cef534ed C |
581 | } |
582 | ||
583 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
584 | } | |
585 | ||
453e83ea | 586 | addUserBlockJob (user: MUser, blocked: boolean, reason?: string) { |
eacb25c4 C |
587 | const reasonString = reason ? ` for the following reason: ${reason}` : '' |
588 | const blockedWord = blocked ? 'blocked' : 'unblocked' | |
eacb25c4 C |
589 | |
590 | const to = user.email | |
591 | const emailPayload: EmailPayload = { | |
592 | to: [ to ], | |
df4c603d RK |
593 | subject: 'Account ' + blockedWord, |
594 | text: `Your account ${user.username} on ${WEBSERVER.HOST} has been ${blockedWord}${reasonString}.` | |
eacb25c4 C |
595 | } |
596 | ||
597 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
598 | } | |
599 | ||
4e9fa5b7 | 600 | addContactFormJob (fromEmail: string, fromName: string, subject: string, body: string) { |
a4101923 | 601 | const emailPayload: EmailPayload = { |
df4c603d | 602 | template: 'contact-form', |
a4101923 | 603 | to: [ CONFIG.ADMIN.EMAIL ], |
df4c603d RK |
604 | replyTo: `"${fromName}" <${fromEmail}>`, |
605 | subject: `(contact form) ${subject}`, | |
606 | locals: { | |
607 | fromName, | |
608 | fromEmail, | |
b9cf3fb6 C |
609 | body, |
610 | ||
611 | // There are not notification preferences for the contact form | |
612 | hideNotificationPreferences: true | |
df4c603d | 613 | } |
a4101923 C |
614 | } |
615 | ||
616 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
617 | } | |
618 | ||
47f6cb31 | 619 | async sendMail (options: EmailPayload) { |
4c1c1709 | 620 | if (!isEmailEnabled()) { |
ecb4e35f C |
621 | throw new Error('Cannot send mail because SMTP is not configured.') |
622 | } | |
623 | ||
df4c603d RK |
624 | const fromDisplayName = options.from |
625 | ? options.from | |
6dd9de95 | 626 | : WEBSERVER.HOST |
4759fedc | 627 | |
df4c603d RK |
628 | const email = new Email({ |
629 | send: true, | |
630 | message: { | |
631 | from: `"${fromDisplayName}" <${CONFIG.SMTP.FROM_ADDRESS}>` | |
632 | }, | |
633 | transport: this.transporter, | |
634 | views: { | |
03fc1928 | 635 | root: join(root(), 'dist', 'server', 'lib', 'emails') |
df4c603d RK |
636 | }, |
637 | subjectPrefix: CONFIG.EMAIL.SUBJECT.PREFIX | |
638 | }) | |
639 | ||
47f6cb31 | 640 | for (const to of options.to) { |
df4c603d RK |
641 | await email |
642 | .send(merge( | |
643 | { | |
644 | template: 'common', | |
645 | message: { | |
646 | to, | |
647 | from: options.from, | |
648 | subject: options.subject, | |
649 | replyTo: options.replyTo | |
650 | }, | |
651 | locals: { // default variables available in all templates | |
652 | WEBSERVER, | |
653 | EMAIL: CONFIG.EMAIL, | |
654 | text: options.text, | |
655 | subject: options.subject | |
656 | } | |
657 | }, | |
658 | options // overriden/new variables given for a specific template in the payload | |
659 | ) as SendEmailOptions) | |
03fc1928 C |
660 | .then(res => logger.debug('Sent email.', { res })) |
661 | .catch(err => logger.error('Error in email sender.', { err })) | |
47f6cb31 | 662 | } |
ecb4e35f C |
663 | } |
664 | ||
665 | private dieOnConnectionFailure (err?: Error) { | |
d5b7d911 | 666 | logger.error('Failed to connect to SMTP %s:%d.', CONFIG.SMTP.HOSTNAME, CONFIG.SMTP.PORT, { err }) |
ecb4e35f C |
667 | process.exit(-1) |
668 | } | |
669 | ||
670 | static get Instance () { | |
671 | return this.instance || (this.instance = new this()) | |
672 | } | |
673 | } | |
674 | ||
675 | // --------------------------------------------------------------------------- | |
676 | ||
677 | export { | |
8dc8a34e | 678 | Emailer |
ecb4e35f | 679 | } |