]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/emailer.ts
Move to eslint
[github/Chocobozzz/PeerTube.git] / server / lib / emailer.ts
1 import { createTransport, Transporter } from 'nodemailer'
2 import { isTestInstance } from '../helpers/core-utils'
3 import { bunyanLogger, logger } from '../helpers/logger'
4 import { CONFIG } from '../initializers/config'
5 import { JobQueue } from './job-queue'
6 import { EmailPayload } from './job-queue/handlers/email'
7 import { readFileSync } from 'fs-extra'
8 import { WEBSERVER } from '../initializers/constants'
9 import {
10 MCommentOwnerVideo,
11 MVideo,
12 MVideoAbuseVideo,
13 MVideoAccountLight,
14 MVideoBlacklistLightVideo,
15 MVideoBlacklistVideo
16 } from '../typings/models/video'
17 import { MActorFollowActors, MActorFollowFull, MUser } from '../typings/models'
18 import { MVideoImport, MVideoImportVideo } from '@server/typings/models/video/video-import'
19
20 type SendEmailOptions = {
21 to: string[]
22 subject: string
23 text: string
24
25 fromDisplayName?: string
26 replyTo?: string
27 }
28
29 class Emailer {
30
31 private static instance: Emailer
32 private initialized = false
33 private transporter: Transporter
34
35 private constructor () {
36 }
37
38 init () {
39 // Already initialized
40 if (this.initialized === true) return
41 this.initialized = true
42
43 if (Emailer.isEnabled()) {
44 logger.info('Using %s:%s as SMTP server.', CONFIG.SMTP.HOSTNAME, CONFIG.SMTP.PORT)
45
46 let tls
47 if (CONFIG.SMTP.CA_FILE) {
48 tls = {
49 ca: [ readFileSync(CONFIG.SMTP.CA_FILE) ]
50 }
51 }
52
53 let auth
54 if (CONFIG.SMTP.USERNAME && CONFIG.SMTP.PASSWORD) {
55 auth = {
56 user: CONFIG.SMTP.USERNAME,
57 pass: CONFIG.SMTP.PASSWORD
58 }
59 }
60
61 this.transporter = createTransport({
62 host: CONFIG.SMTP.HOSTNAME,
63 port: CONFIG.SMTP.PORT,
64 secure: CONFIG.SMTP.TLS,
65 debug: CONFIG.LOG.LEVEL === 'debug',
66 logger: bunyanLogger as any,
67 ignoreTLS: CONFIG.SMTP.DISABLE_STARTTLS,
68 tls,
69 auth
70 })
71 } else {
72 if (!isTestInstance()) {
73 logger.error('Cannot use SMTP server because of lack of configuration. PeerTube will not be able to send mails!')
74 }
75 }
76 }
77
78 static isEnabled () {
79 return !!CONFIG.SMTP.HOSTNAME && !!CONFIG.SMTP.PORT
80 }
81
82 async checkConnectionOrDie () {
83 if (!this.transporter) return
84
85 logger.info('Testing SMTP server...')
86
87 try {
88 const success = await this.transporter.verify()
89 if (success !== true) this.dieOnConnectionFailure()
90
91 logger.info('Successfully connected to SMTP server.')
92 } catch (err) {
93 this.dieOnConnectionFailure(err)
94 }
95 }
96
97 addNewVideoFromSubscriberNotification (to: string[], video: MVideoAccountLight) {
98 const channelName = video.VideoChannel.getDisplayName()
99 const videoUrl = WEBSERVER.URL + video.getWatchStaticPath()
100
101 const text = 'Hi dear user,\n\n' +
102 `Your subscription ${channelName} just published a new video: ${video.name}` +
103 '\n\n' +
104 `You can view it on ${videoUrl} ` +
105 '\n\n' +
106 'Cheers,\n' +
107 `${CONFIG.EMAIL.BODY.SIGNATURE}`
108
109 const emailPayload: EmailPayload = {
110 to,
111 subject: CONFIG.EMAIL.SUBJECT.PREFIX + channelName + ' just published a new video',
112 text
113 }
114
115 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
116 }
117
118 addNewFollowNotification (to: string[], actorFollow: MActorFollowFull, followType: 'account' | 'channel') {
119 const followerName = actorFollow.ActorFollower.Account.getDisplayName()
120 const followingName = (actorFollow.ActorFollowing.VideoChannel || actorFollow.ActorFollowing.Account).getDisplayName()
121
122 const text = 'Hi dear user,\n\n' +
123 `Your ${followType} ${followingName} has a new subscriber: ${followerName}` +
124 '\n\n' +
125 'Cheers,\n' +
126 `${CONFIG.EMAIL.BODY.SIGNATURE}`
127
128 const emailPayload: EmailPayload = {
129 to,
130 subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'New follower on your channel ' + followingName,
131 text
132 }
133
134 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
135 }
136
137 addNewInstanceFollowerNotification (to: string[], actorFollow: MActorFollowActors) {
138 const awaitingApproval = actorFollow.state === 'pending' ? ' awaiting manual approval.' : ''
139
140 const text = 'Hi dear admin,\n\n' +
141 `Your instance has a new follower: ${actorFollow.ActorFollower.url}${awaitingApproval}` +
142 '\n\n' +
143 'Cheers,\n' +
144 `${CONFIG.EMAIL.BODY.SIGNATURE}`
145
146 const emailPayload: EmailPayload = {
147 to,
148 subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'New instance follower',
149 text
150 }
151
152 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
153 }
154
155 addAutoInstanceFollowingNotification (to: string[], actorFollow: MActorFollowActors) {
156 const text = 'Hi dear admin,\n\n' +
157 `Your instance automatically followed a new instance: ${actorFollow.ActorFollowing.url}` +
158 '\n\n' +
159 'Cheers,\n' +
160 `${CONFIG.EMAIL.BODY.SIGNATURE}`
161
162 const emailPayload: EmailPayload = {
163 to,
164 subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'Auto instance following',
165 text
166 }
167
168 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
169 }
170
171 myVideoPublishedNotification (to: string[], video: MVideo) {
172 const videoUrl = WEBSERVER.URL + video.getWatchStaticPath()
173
174 const text = 'Hi dear user,\n\n' +
175 `Your video ${video.name} has been published.` +
176 '\n\n' +
177 `You can view it on ${videoUrl} ` +
178 '\n\n' +
179 'Cheers,\n' +
180 `${CONFIG.EMAIL.BODY.SIGNATURE}`
181
182 const emailPayload: EmailPayload = {
183 to,
184 subject: CONFIG.EMAIL.SUBJECT.PREFIX + `Your video ${video.name} is published`,
185 text
186 }
187
188 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
189 }
190
191 myVideoImportSuccessNotification (to: string[], videoImport: MVideoImportVideo) {
192 const videoUrl = WEBSERVER.URL + videoImport.Video.getWatchStaticPath()
193
194 const text = 'Hi dear user,\n\n' +
195 `Your video import ${videoImport.getTargetIdentifier()} is finished.` +
196 '\n\n' +
197 `You can view the imported video on ${videoUrl} ` +
198 '\n\n' +
199 'Cheers,\n' +
200 `${CONFIG.EMAIL.BODY.SIGNATURE}`
201
202 const emailPayload: EmailPayload = {
203 to,
204 subject: CONFIG.EMAIL.SUBJECT.PREFIX + `Your video import ${videoImport.getTargetIdentifier()} is finished`,
205 text
206 }
207
208 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
209 }
210
211 myVideoImportErrorNotification (to: string[], videoImport: MVideoImport) {
212 const importUrl = WEBSERVER.URL + '/my-account/video-imports'
213
214 const text = 'Hi dear user,\n\n' +
215 `Your video import ${videoImport.getTargetIdentifier()} encountered an error.` +
216 '\n\n' +
217 `See your videos import dashboard for more information: ${importUrl}` +
218 '\n\n' +
219 'Cheers,\n' +
220 `${CONFIG.EMAIL.BODY.SIGNATURE}`
221
222 const emailPayload: EmailPayload = {
223 to,
224 subject: CONFIG.EMAIL.SUBJECT.PREFIX + `Your video import ${videoImport.getTargetIdentifier()} encountered an error`,
225 text
226 }
227
228 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
229 }
230
231 addNewCommentOnMyVideoNotification (to: string[], comment: MCommentOwnerVideo) {
232 const accountName = comment.Account.getDisplayName()
233 const video = comment.Video
234 const commentUrl = WEBSERVER.URL + comment.getCommentStaticPath()
235
236 const text = 'Hi dear user,\n\n' +
237 `A new comment has been posted by ${accountName} on your video ${video.name}` +
238 '\n\n' +
239 `You can view it on ${commentUrl} ` +
240 '\n\n' +
241 'Cheers,\n' +
242 `${CONFIG.EMAIL.BODY.SIGNATURE}`
243
244 const emailPayload: EmailPayload = {
245 to,
246 subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'New comment on your video ' + video.name,
247 text
248 }
249
250 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
251 }
252
253 addNewCommentMentionNotification (to: string[], comment: MCommentOwnerVideo) {
254 const accountName = comment.Account.getDisplayName()
255 const video = comment.Video
256 const commentUrl = WEBSERVER.URL + comment.getCommentStaticPath()
257
258 const text = 'Hi dear user,\n\n' +
259 `${accountName} mentioned you on video ${video.name}` +
260 '\n\n' +
261 `You can view the comment on ${commentUrl} ` +
262 '\n\n' +
263 'Cheers,\n' +
264 `${CONFIG.EMAIL.BODY.SIGNATURE}`
265
266 const emailPayload: EmailPayload = {
267 to,
268 subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'Mention on video ' + video.name,
269 text
270 }
271
272 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
273 }
274
275 addVideoAbuseModeratorsNotification (to: string[], videoAbuse: MVideoAbuseVideo) {
276 const videoUrl = WEBSERVER.URL + videoAbuse.Video.getWatchStaticPath()
277
278 const text = 'Hi,\n\n' +
279 `${WEBSERVER.HOST} received an abuse for the following video ${videoUrl}\n\n` +
280 'Cheers,\n' +
281 `${CONFIG.EMAIL.BODY.SIGNATURE}`
282
283 const emailPayload: EmailPayload = {
284 to,
285 subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'Received a video abuse',
286 text
287 }
288
289 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
290 }
291
292 addVideoAutoBlacklistModeratorsNotification (to: string[], videoBlacklist: MVideoBlacklistLightVideo) {
293 const VIDEO_AUTO_BLACKLIST_URL = WEBSERVER.URL + '/admin/moderation/video-auto-blacklist/list'
294 const videoUrl = WEBSERVER.URL + videoBlacklist.Video.getWatchStaticPath()
295
296 const text = 'Hi,\n\n' +
297 'A recently added video was auto-blacklisted and requires moderator review before publishing.' +
298 '\n\n' +
299 `You can view it and take appropriate action on ${videoUrl}` +
300 '\n\n' +
301 `A full list of auto-blacklisted videos can be reviewed here: ${VIDEO_AUTO_BLACKLIST_URL}` +
302 '\n\n' +
303 'Cheers,\n' +
304 `${CONFIG.EMAIL.BODY.SIGNATURE}`
305
306 const emailPayload: EmailPayload = {
307 to,
308 subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'An auto-blacklisted video is awaiting review',
309 text
310 }
311
312 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
313 }
314
315 addNewUserRegistrationNotification (to: string[], user: MUser) {
316 const text = 'Hi,\n\n' +
317 `User ${user.username} just registered on ${WEBSERVER.HOST} PeerTube instance.\n\n` +
318 'Cheers,\n' +
319 `${CONFIG.EMAIL.BODY.SIGNATURE}`
320
321 const emailPayload: EmailPayload = {
322 to,
323 subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'New user registration on ' + WEBSERVER.HOST,
324 text
325 }
326
327 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
328 }
329
330 addVideoBlacklistNotification (to: string[], videoBlacklist: MVideoBlacklistVideo) {
331 const videoName = videoBlacklist.Video.name
332 const videoUrl = WEBSERVER.URL + videoBlacklist.Video.getWatchStaticPath()
333
334 const reasonString = videoBlacklist.reason ? ` for the following reason: ${videoBlacklist.reason}` : ''
335 const blockedString = `Your video ${videoName} (${videoUrl} on ${WEBSERVER.HOST} has been blacklisted${reasonString}.`
336
337 const text = 'Hi,\n\n' +
338 blockedString +
339 '\n\n' +
340 'Cheers,\n' +
341 `${CONFIG.EMAIL.BODY.SIGNATURE}`
342
343 const emailPayload: EmailPayload = {
344 to,
345 subject: CONFIG.EMAIL.SUBJECT.PREFIX + `Video ${videoName} blacklisted`,
346 text
347 }
348
349 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
350 }
351
352 addVideoUnblacklistNotification (to: string[], video: MVideo) {
353 const videoUrl = WEBSERVER.URL + video.getWatchStaticPath()
354
355 const text = 'Hi,\n\n' +
356 `Your video ${video.name} (${videoUrl}) on ${WEBSERVER.HOST} has been unblacklisted.` +
357 '\n\n' +
358 'Cheers,\n' +
359 `${CONFIG.EMAIL.BODY.SIGNATURE}`
360
361 const emailPayload: EmailPayload = {
362 to,
363 subject: CONFIG.EMAIL.SUBJECT.PREFIX + `Video ${video.name} unblacklisted`,
364 text
365 }
366
367 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
368 }
369
370 addPasswordResetEmailJob (to: string, resetPasswordUrl: string) {
371 const text = 'Hi dear user,\n\n' +
372 `A reset password procedure for your account ${to} has been requested on ${WEBSERVER.HOST} ` +
373 `Please follow this link to reset it: ${resetPasswordUrl} (the link will expire within 1 hour)\n\n` +
374 'If you are not the person who initiated this request, please ignore this email.\n\n' +
375 'Cheers,\n' +
376 `${CONFIG.EMAIL.BODY.SIGNATURE}`
377
378 const emailPayload: EmailPayload = {
379 to: [ to ],
380 subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'Reset your password',
381 text
382 }
383
384 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
385 }
386
387 addVerifyEmailJob (to: string, verifyEmailUrl: string) {
388 const text = 'Welcome to PeerTube,\n\n' +
389 `To start using PeerTube on ${WEBSERVER.HOST} you must verify your email! ` +
390 `Please follow this link to verify this email belongs to you: ${verifyEmailUrl}\n\n` +
391 'If you are not the person who initiated this request, please ignore this email.\n\n' +
392 'Cheers,\n' +
393 `${CONFIG.EMAIL.BODY.SIGNATURE}`
394
395 const emailPayload: EmailPayload = {
396 to: [ to ],
397 subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'Verify your email',
398 text
399 }
400
401 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
402 }
403
404 addUserBlockJob (user: MUser, blocked: boolean, reason?: string) {
405 const reasonString = reason ? ` for the following reason: ${reason}` : ''
406 const blockedWord = blocked ? 'blocked' : 'unblocked'
407 const blockedString = `Your account ${user.username} on ${WEBSERVER.HOST} has been ${blockedWord}${reasonString}.`
408
409 const text = 'Hi,\n\n' +
410 blockedString +
411 '\n\n' +
412 'Cheers,\n' +
413 `${CONFIG.EMAIL.BODY.SIGNATURE}`
414
415 const to = user.email
416 const emailPayload: EmailPayload = {
417 to: [ to ],
418 subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'Account ' + blockedWord,
419 text
420 }
421
422 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
423 }
424
425 addContactFormJob (fromEmail: string, fromName: string, subject: string, body: string) {
426 const text = 'Hello dear admin,\n\n' +
427 fromName + ' sent you a message' +
428 '\n\n---------------------------------------\n\n' +
429 body +
430 '\n\n---------------------------------------\n\n' +
431 'Cheers,\n' +
432 'PeerTube.'
433
434 const emailPayload: EmailPayload = {
435 fromDisplayName: fromEmail,
436 replyTo: fromEmail,
437 to: [ CONFIG.ADMIN.EMAIL ],
438 subject: CONFIG.EMAIL.SUBJECT.PREFIX + subject,
439 text
440 }
441
442 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
443 }
444
445 async sendMail (options: EmailPayload) {
446 if (!Emailer.isEnabled()) {
447 throw new Error('Cannot send mail because SMTP is not configured.')
448 }
449
450 const fromDisplayName = options.fromDisplayName
451 ? options.fromDisplayName
452 : WEBSERVER.HOST
453
454 for (const to of options.to) {
455 await this.transporter.sendMail({
456 from: `"${fromDisplayName}" <${CONFIG.SMTP.FROM_ADDRESS}>`,
457 replyTo: options.replyTo,
458 to,
459 subject: options.subject,
460 text: options.text
461 })
462 }
463 }
464
465 private dieOnConnectionFailure (err?: Error) {
466 logger.error('Failed to connect to SMTP %s:%d.', CONFIG.SMTP.HOSTNAME, CONFIG.SMTP.PORT, { err })
467 process.exit(-1)
468 }
469
470 static get Instance () {
471 return this.instance || (this.instance = new this())
472 }
473 }
474
475 // ---------------------------------------------------------------------------
476
477 export {
478 Emailer,
479 SendEmailOptions
480 }