]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/emailer.ts
Add notification on new instance follower (server side)
[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 addNewInstanceFollowerNotification (to: string[], actorFollow: ActorFollowModel) {
133 const awaitingApproval = actorFollow.state === 'pending' ? ' awaiting manual approval.' : ''
134
135 const text = `Hi dear admin,\n\n` +
136 `Your instance has a new follower: ${actorFollow.ActorFollower.url}${awaitingApproval}` +
137 `\n\n` +
138 `Cheers,\n` +
139 `PeerTube.`
140
141 const emailPayload: EmailPayload = {
142 to,
143 subject: 'New instance follower',
144 text
145 }
146
147 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
148 }
149
150 myVideoPublishedNotification (to: string[], video: VideoModel) {
151 const videoUrl = CONFIG.WEBSERVER.URL + video.getWatchStaticPath()
152
153 const text = `Hi dear user,\n\n` +
154 `Your video ${video.name} has been published.` +
155 `\n\n` +
156 `You can view it on ${videoUrl} ` +
157 `\n\n` +
158 `Cheers,\n` +
159 `PeerTube.`
160
161 const emailPayload: EmailPayload = {
162 to,
163 subject: `Your video ${video.name} is published`,
164 text
165 }
166
167 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
168 }
169
170 myVideoImportSuccessNotification (to: string[], videoImport: VideoImportModel) {
171 const videoUrl = CONFIG.WEBSERVER.URL + videoImport.Video.getWatchStaticPath()
172
173 const text = `Hi dear user,\n\n` +
174 `Your video import ${videoImport.getTargetIdentifier()} is finished.` +
175 `\n\n` +
176 `You can view the imported video on ${videoUrl} ` +
177 `\n\n` +
178 `Cheers,\n` +
179 `PeerTube.`
180
181 const emailPayload: EmailPayload = {
182 to,
183 subject: `Your video import ${videoImport.getTargetIdentifier()} is finished`,
184 text
185 }
186
187 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
188 }
189
190 myVideoImportErrorNotification (to: string[], videoImport: VideoImportModel) {
191 const importUrl = CONFIG.WEBSERVER.URL + '/my-account/video-imports'
192
193 const text = `Hi dear user,\n\n` +
194 `Your video import ${videoImport.getTargetIdentifier()} encountered an error.` +
195 `\n\n` +
196 `See your videos import dashboard for more information: ${importUrl}` +
197 `\n\n` +
198 `Cheers,\n` +
199 `PeerTube.`
200
201 const emailPayload: EmailPayload = {
202 to,
203 subject: `Your video import ${videoImport.getTargetIdentifier()} encountered an error`,
204 text
205 }
206
207 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
208 }
209
210 addNewCommentOnMyVideoNotification (to: string[], comment: VideoCommentModel) {
211 const accountName = comment.Account.getDisplayName()
212 const video = comment.Video
213 const commentUrl = CONFIG.WEBSERVER.URL + comment.getCommentStaticPath()
214
215 const text = `Hi dear user,\n\n` +
216 `A new comment has been posted by ${accountName} on your video ${video.name}` +
217 `\n\n` +
218 `You can view it on ${commentUrl} ` +
219 `\n\n` +
220 `Cheers,\n` +
221 `PeerTube.`
222
223 const emailPayload: EmailPayload = {
224 to,
225 subject: 'New comment on your video ' + video.name,
226 text
227 }
228
229 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
230 }
231
232 addNewCommentMentionNotification (to: string[], comment: VideoCommentModel) {
233 const accountName = comment.Account.getDisplayName()
234 const video = comment.Video
235 const commentUrl = CONFIG.WEBSERVER.URL + comment.getCommentStaticPath()
236
237 const text = `Hi dear user,\n\n` +
238 `${accountName} mentioned you on video ${video.name}` +
239 `\n\n` +
240 `You can view the comment on ${commentUrl} ` +
241 `\n\n` +
242 `Cheers,\n` +
243 `PeerTube.`
244
245 const emailPayload: EmailPayload = {
246 to,
247 subject: 'Mention on video ' + video.name,
248 text
249 }
250
251 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
252 }
253
254 addVideoAbuseModeratorsNotification (to: string[], videoAbuse: VideoAbuseModel) {
255 const videoUrl = CONFIG.WEBSERVER.URL + videoAbuse.Video.getWatchStaticPath()
256
257 const text = `Hi,\n\n` +
258 `${CONFIG.WEBSERVER.HOST} received an abuse for the following video ${videoUrl}\n\n` +
259 `Cheers,\n` +
260 `PeerTube.`
261
262 const emailPayload: EmailPayload = {
263 to,
264 subject: '[PeerTube] Received a video abuse',
265 text
266 }
267
268 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
269 }
270
271 addVideoAutoBlacklistModeratorsNotification (to: string[], video: VideoModel) {
272 const VIDEO_AUTO_BLACKLIST_URL = CONFIG.WEBSERVER.URL + '/admin/moderation/video-auto-blacklist/list'
273 const videoUrl = CONFIG.WEBSERVER.URL + video.getWatchStaticPath()
274
275 const text = `Hi,\n\n` +
276 `A recently added video was auto-blacklisted and requires moderator review before publishing.` +
277 `\n\n` +
278 `You can view it and take appropriate action on ${videoUrl}` +
279 `\n\n` +
280 `A full list of auto-blacklisted videos can be reviewed here: ${VIDEO_AUTO_BLACKLIST_URL}` +
281 `\n\n` +
282 `Cheers,\n` +
283 `PeerTube.`
284
285 const emailPayload: EmailPayload = {
286 to,
287 subject: '[PeerTube] An auto-blacklisted video is awaiting review',
288 text
289 }
290
291 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
292 }
293
294 addNewUserRegistrationNotification (to: string[], user: UserModel) {
295 const text = `Hi,\n\n` +
296 `User ${user.username} just registered on ${CONFIG.WEBSERVER.HOST} PeerTube instance.\n\n` +
297 `Cheers,\n` +
298 `PeerTube.`
299
300 const emailPayload: EmailPayload = {
301 to,
302 subject: '[PeerTube] New user registration on ' + CONFIG.WEBSERVER.HOST,
303 text
304 }
305
306 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
307 }
308
309 addVideoBlacklistNotification (to: string[], videoBlacklist: VideoBlacklistModel) {
310 const videoName = videoBlacklist.Video.name
311 const videoUrl = CONFIG.WEBSERVER.URL + videoBlacklist.Video.getWatchStaticPath()
312
313 const reasonString = videoBlacklist.reason ? ` for the following reason: ${videoBlacklist.reason}` : ''
314 const blockedString = `Your video ${videoName} (${videoUrl} on ${CONFIG.WEBSERVER.HOST} has been blacklisted${reasonString}.`
315
316 const text = 'Hi,\n\n' +
317 blockedString +
318 '\n\n' +
319 'Cheers,\n' +
320 `PeerTube.`
321
322 const emailPayload: EmailPayload = {
323 to,
324 subject: `[PeerTube] Video ${videoName} blacklisted`,
325 text
326 }
327
328 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
329 }
330
331 addVideoUnblacklistNotification (to: string[], video: VideoModel) {
332 const videoUrl = CONFIG.WEBSERVER.URL + video.getWatchStaticPath()
333
334 const text = 'Hi,\n\n' +
335 `Your video ${video.name} (${videoUrl}) on ${CONFIG.WEBSERVER.HOST} has been unblacklisted.` +
336 '\n\n' +
337 'Cheers,\n' +
338 `PeerTube.`
339
340 const emailPayload: EmailPayload = {
341 to,
342 subject: `[PeerTube] Video ${video.name} unblacklisted`,
343 text
344 }
345
346 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
347 }
348
349 addPasswordResetEmailJob (to: string, resetPasswordUrl: string) {
350 const text = `Hi dear user,\n\n` +
351 `A reset password procedure for your account ${to} has been requested on ${CONFIG.WEBSERVER.HOST} ` +
352 `Please follow this link to reset it: ${resetPasswordUrl}\n\n` +
353 `If you are not the person who initiated this request, please ignore this email.\n\n` +
354 `Cheers,\n` +
355 `PeerTube.`
356
357 const emailPayload: EmailPayload = {
358 to: [ to ],
359 subject: 'Reset your PeerTube password',
360 text
361 }
362
363 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
364 }
365
366 addVerifyEmailJob (to: string, verifyEmailUrl: string) {
367 const text = `Welcome to PeerTube,\n\n` +
368 `To start using PeerTube on ${CONFIG.WEBSERVER.HOST} you must verify your email! ` +
369 `Please follow this link to verify this email belongs to you: ${verifyEmailUrl}\n\n` +
370 `If you are not the person who initiated this request, please ignore this email.\n\n` +
371 `Cheers,\n` +
372 `PeerTube.`
373
374 const emailPayload: EmailPayload = {
375 to: [ to ],
376 subject: 'Verify your PeerTube email',
377 text
378 }
379
380 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
381 }
382
383 addUserBlockJob (user: UserModel, blocked: boolean, reason?: string) {
384 const reasonString = reason ? ` for the following reason: ${reason}` : ''
385 const blockedWord = blocked ? 'blocked' : 'unblocked'
386 const blockedString = `Your account ${user.username} on ${CONFIG.WEBSERVER.HOST} has been ${blockedWord}${reasonString}.`
387
388 const text = 'Hi,\n\n' +
389 blockedString +
390 '\n\n' +
391 'Cheers,\n' +
392 `PeerTube.`
393
394 const to = user.email
395 const emailPayload: EmailPayload = {
396 to: [ to ],
397 subject: '[PeerTube] Account ' + blockedWord,
398 text
399 }
400
401 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
402 }
403
404 addContactFormJob (fromEmail: string, fromName: string, body: string) {
405 const text = 'Hello dear admin,\n\n' +
406 fromName + ' sent you a message' +
407 '\n\n---------------------------------------\n\n' +
408 body +
409 '\n\n---------------------------------------\n\n' +
410 'Cheers,\n' +
411 'PeerTube.'
412
413 const emailPayload: EmailPayload = {
414 fromDisplayName: fromEmail,
415 replyTo: fromEmail,
416 to: [ CONFIG.ADMIN.EMAIL ],
417 subject: '[PeerTube] Contact form submitted',
418 text
419 }
420
421 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
422 }
423
424 sendMail (options: EmailPayload) {
425 if (!Emailer.isEnabled()) {
426 throw new Error('Cannot send mail because SMTP is not configured.')
427 }
428
429 const fromDisplayName = options.fromDisplayName
430 ? options.fromDisplayName
431 : CONFIG.WEBSERVER.HOST
432
433 return this.transporter.sendMail({
434 from: `"${fromDisplayName}" <${CONFIG.SMTP.FROM_ADDRESS}>`,
435 replyTo: options.replyTo,
436 to: options.to.join(','),
437 subject: options.subject,
438 text: options.text
439 })
440 }
441
442 private dieOnConnectionFailure (err?: Error) {
443 logger.error('Failed to connect to SMTP %s:%d.', CONFIG.SMTP.HOSTNAME, CONFIG.SMTP.PORT, { err })
444 process.exit(-1)
445 }
446
447 static get Instance () {
448 return this.instance || (this.instance = new this())
449 }
450 }
451
452 // ---------------------------------------------------------------------------
453
454 export {
455 Emailer,
456 SendEmailOptions
457 }