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