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