]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/emailer.ts
Merge branch 'release/v1.2.0'
[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 addNewFollowNotification (to: string[], actorFollow: ActorFollowModel, followType: 'account' | 'channel') {
105 const followerName = actorFollow.ActorFollower.Account.getDisplayName()
106 const followingName = (actorFollow.ActorFollowing.VideoChannel || actorFollow.ActorFollowing.Account).getDisplayName()
107
108 const text = `Hi dear user,\n\n` +
109 `Your ${followType} ${followingName} has a new subscriber: ${followerName}` +
110 `\n\n` +
111 `Cheers,\n` +
112 `PeerTube.`
113
114 const emailPayload: EmailPayload = {
115 to,
116 subject: 'New follower on your channel ' + followingName,
117 text
118 }
119
120 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
121 }
122
123 myVideoPublishedNotification (to: string[], video: VideoModel) {
124 const videoUrl = CONFIG.WEBSERVER.URL + video.getWatchStaticPath()
125
126 const text = `Hi dear user,\n\n` +
127 `Your video ${video.name} has been published.` +
128 `\n\n` +
129 `You can view it on ${videoUrl} ` +
130 `\n\n` +
131 `Cheers,\n` +
132 `PeerTube.`
133
134 const emailPayload: EmailPayload = {
135 to,
136 subject: `Your video ${video.name} is published`,
137 text
138 }
139
140 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
141 }
142
143 myVideoImportSuccessNotification (to: string[], videoImport: VideoImportModel) {
144 const videoUrl = CONFIG.WEBSERVER.URL + videoImport.Video.getWatchStaticPath()
145
146 const text = `Hi dear user,\n\n` +
147 `Your video import ${videoImport.getTargetIdentifier()} is finished.` +
148 `\n\n` +
149 `You can view the imported video on ${videoUrl} ` +
150 `\n\n` +
151 `Cheers,\n` +
152 `PeerTube.`
153
154 const emailPayload: EmailPayload = {
155 to,
156 subject: `Your video import ${videoImport.getTargetIdentifier()} is finished`,
157 text
158 }
159
160 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
161 }
162
163 myVideoImportErrorNotification (to: string[], videoImport: VideoImportModel) {
164 const importUrl = CONFIG.WEBSERVER.URL + '/my-account/video-imports'
165
166 const text = `Hi dear user,\n\n` +
167 `Your video import ${videoImport.getTargetIdentifier()} encountered an error.` +
168 `\n\n` +
169 `See your videos import dashboard for more information: ${importUrl}` +
170 `\n\n` +
171 `Cheers,\n` +
172 `PeerTube.`
173
174 const emailPayload: EmailPayload = {
175 to,
176 subject: `Your video import ${videoImport.getTargetIdentifier()} encountered an error`,
177 text
178 }
179
180 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
181 }
182
183 addNewCommentOnMyVideoNotification (to: string[], comment: VideoCommentModel) {
184 const accountName = comment.Account.getDisplayName()
185 const video = comment.Video
186 const commentUrl = CONFIG.WEBSERVER.URL + comment.getCommentStaticPath()
187
188 const text = `Hi dear user,\n\n` +
189 `A new comment has been posted by ${accountName} on your video ${video.name}` +
190 `\n\n` +
191 `You can view it on ${commentUrl} ` +
192 `\n\n` +
193 `Cheers,\n` +
194 `PeerTube.`
195
196 const emailPayload: EmailPayload = {
197 to,
198 subject: 'New comment on your video ' + video.name,
199 text
200 }
201
202 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
203 }
204
205 addNewCommentMentionNotification (to: string[], comment: VideoCommentModel) {
206 const accountName = comment.Account.getDisplayName()
207 const video = comment.Video
208 const commentUrl = CONFIG.WEBSERVER.URL + comment.getCommentStaticPath()
209
210 const text = `Hi dear user,\n\n` +
211 `${accountName} mentioned you on video ${video.name}` +
212 `\n\n` +
213 `You can view the comment on ${commentUrl} ` +
214 `\n\n` +
215 `Cheers,\n` +
216 `PeerTube.`
217
218 const emailPayload: EmailPayload = {
219 to,
220 subject: 'Mention on video ' + video.name,
221 text
222 }
223
224 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
225 }
226
227 addVideoAbuseModeratorsNotification (to: string[], videoAbuse: VideoAbuseModel) {
228 const videoUrl = CONFIG.WEBSERVER.URL + videoAbuse.Video.getWatchStaticPath()
229
230 const text = `Hi,\n\n` +
231 `${CONFIG.WEBSERVER.HOST} received an abuse for the following video ${videoUrl}\n\n` +
232 `Cheers,\n` +
233 `PeerTube.`
234
235 const emailPayload: EmailPayload = {
236 to,
237 subject: '[PeerTube] Received a video abuse',
238 text
239 }
240
241 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
242 }
243
244 addNewUserRegistrationNotification (to: string[], user: UserModel) {
245 const text = `Hi,\n\n` +
246 `User ${user.username} just registered on ${CONFIG.WEBSERVER.HOST} PeerTube instance.\n\n` +
247 `Cheers,\n` +
248 `PeerTube.`
249
250 const emailPayload: EmailPayload = {
251 to,
252 subject: '[PeerTube] New user registration on ' + CONFIG.WEBSERVER.HOST,
253 text
254 }
255
256 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
257 }
258
259 addVideoBlacklistNotification (to: string[], videoBlacklist: VideoBlacklistModel) {
260 const videoName = videoBlacklist.Video.name
261 const videoUrl = CONFIG.WEBSERVER.URL + videoBlacklist.Video.getWatchStaticPath()
262
263 const reasonString = videoBlacklist.reason ? ` for the following reason: ${videoBlacklist.reason}` : ''
264 const blockedString = `Your video ${videoName} (${videoUrl} on ${CONFIG.WEBSERVER.HOST} has been blacklisted${reasonString}.`
265
266 const text = 'Hi,\n\n' +
267 blockedString +
268 '\n\n' +
269 'Cheers,\n' +
270 `PeerTube.`
271
272 const emailPayload: EmailPayload = {
273 to,
274 subject: `[PeerTube] Video ${videoName} blacklisted`,
275 text
276 }
277
278 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
279 }
280
281 addVideoUnblacklistNotification (to: string[], video: VideoModel) {
282 const videoUrl = CONFIG.WEBSERVER.URL + video.getWatchStaticPath()
283
284 const text = 'Hi,\n\n' +
285 `Your video ${video.name} (${videoUrl}) on ${CONFIG.WEBSERVER.HOST} has been unblacklisted.` +
286 '\n\n' +
287 'Cheers,\n' +
288 `PeerTube.`
289
290 const emailPayload: EmailPayload = {
291 to,
292 subject: `[PeerTube] Video ${video.name} unblacklisted`,
293 text
294 }
295
296 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
297 }
298
299 addForgetPasswordEmailJob (to: string, resetPasswordUrl: string) {
300 const text = `Hi dear user,\n\n` +
301 `It seems you forgot your password on ${CONFIG.WEBSERVER.HOST}! ` +
302 `Please follow this link to reset it: ${resetPasswordUrl}\n\n` +
303 `If you are not the person who initiated this request, please ignore this email.\n\n` +
304 `Cheers,\n` +
305 `PeerTube.`
306
307 const emailPayload: EmailPayload = {
308 to: [ to ],
309 subject: 'Reset your PeerTube password',
310 text
311 }
312
313 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
314 }
315
316 addVerifyEmailJob (to: string, verifyEmailUrl: string) {
317 const text = `Welcome to PeerTube,\n\n` +
318 `To start using PeerTube on ${CONFIG.WEBSERVER.HOST} you must verify your email! ` +
319 `Please follow this link to verify this email belongs to you: ${verifyEmailUrl}\n\n` +
320 `If you are not the person who initiated this request, please ignore this email.\n\n` +
321 `Cheers,\n` +
322 `PeerTube.`
323
324 const emailPayload: EmailPayload = {
325 to: [ to ],
326 subject: 'Verify your PeerTube email',
327 text
328 }
329
330 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
331 }
332
333 addUserBlockJob (user: UserModel, blocked: boolean, reason?: string) {
334 const reasonString = reason ? ` for the following reason: ${reason}` : ''
335 const blockedWord = blocked ? 'blocked' : 'unblocked'
336 const blockedString = `Your account ${user.username} on ${CONFIG.WEBSERVER.HOST} has been ${blockedWord}${reasonString}.`
337
338 const text = 'Hi,\n\n' +
339 blockedString +
340 '\n\n' +
341 'Cheers,\n' +
342 `PeerTube.`
343
344 const to = user.email
345 const emailPayload: EmailPayload = {
346 to: [ to ],
347 subject: '[PeerTube] Account ' + blockedWord,
348 text
349 }
350
351 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
352 }
353
354 addContactFormJob (fromEmail: string, fromName: string, body: string) {
355 const text = 'Hello dear admin,\n\n' +
356 fromName + ' sent you a message' +
357 '\n\n---------------------------------------\n\n' +
358 body +
359 '\n\n---------------------------------------\n\n' +
360 'Cheers,\n' +
361 'PeerTube.'
362
363 const emailPayload: EmailPayload = {
364 fromDisplayName: fromEmail,
365 replyTo: fromEmail,
366 to: [ CONFIG.ADMIN.EMAIL ],
367 subject: '[PeerTube] Contact form submitted',
368 text
369 }
370
371 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
372 }
373
374 sendMail (options: EmailPayload) {
375 if (!Emailer.isEnabled()) {
376 throw new Error('Cannot send mail because SMTP is not configured.')
377 }
378
379 const fromDisplayName = options.fromDisplayName
380 ? options.fromDisplayName
381 : CONFIG.WEBSERVER.HOST
382
383 return this.transporter.sendMail({
384 from: `"${fromDisplayName}" <${CONFIG.SMTP.FROM_ADDRESS}>`,
385 replyTo: options.replyTo,
386 to: options.to.join(','),
387 subject: options.subject,
388 text: options.text
389 })
390 }
391
392 private dieOnConnectionFailure (err?: Error) {
393 logger.error('Failed to connect to SMTP %s:%d.', CONFIG.SMTP.HOSTNAME, CONFIG.SMTP.PORT, { err })
394 process.exit(-1)
395 }
396
397 static get Instance () {
398 return this.instance || (this.instance = new this())
399 }
400 }
401
402 // ---------------------------------------------------------------------------
403
404 export {
405 Emailer
406 }