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