]>
Commit | Line | Data |
---|---|---|
ecb4e35f C |
1 | import { createTransport, Transporter } from 'nodemailer' |
2 | import { isTestInstance } from '../helpers/core-utils' | |
05e67d62 | 3 | import { bunyanLogger, logger } from '../helpers/logger' |
ecb4e35f | 4 | import { CONFIG } from '../initializers' |
ba75d268 C |
5 | import { UserModel } from '../models/account/user' |
6 | import { VideoModel } from '../models/video/video' | |
ecb4e35f C |
7 | import { JobQueue } from './job-queue' |
8 | import { EmailPayload } from './job-queue/handlers/email' | |
c9d5c64f | 9 | import { readFileSync } from 'fs-extra' |
cef534ed C |
10 | import { VideoCommentModel } from '../models/video/video-comment' |
11 | import { VideoAbuseModel } from '../models/video/video-abuse' | |
12 | import { VideoBlacklistModel } from '../models/video/video-blacklist' | |
dc133480 | 13 | import { VideoImportModel } from '../models/video/video-import' |
f7cc67b4 | 14 | import { ActorFollowModel } from '../models/activitypub/actor-follow' |
ecb4e35f C |
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 | ||
d3e56c0c | 29 | if (Emailer.isEnabled()) { |
ecb4e35f C |
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 | ||
f076daa7 C |
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 | ||
ecb4e35f C |
47 | this.transporter = createTransport({ |
48 | host: CONFIG.SMTP.HOSTNAME, | |
49 | port: CONFIG.SMTP.PORT, | |
50 | secure: CONFIG.SMTP.TLS, | |
05e67d62 C |
51 | debug: CONFIG.LOG.LEVEL === 'debug', |
52 | logger: bunyanLogger as any, | |
bebf2d89 | 53 | ignoreTLS: CONFIG.SMTP.DISABLE_STARTTLS, |
ecb4e35f | 54 | tls, |
f076daa7 | 55 | auth |
ecb4e35f C |
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 | ||
d3e56c0c C |
64 | static isEnabled () { |
65 | return !!CONFIG.SMTP.HOSTNAME && !!CONFIG.SMTP.PORT | |
3b3b1820 C |
66 | } |
67 | ||
ecb4e35f C |
68 | async checkConnectionOrDie () { |
69 | if (!this.transporter) return | |
70 | ||
3d3441d6 C |
71 | logger.info('Testing SMTP server...') |
72 | ||
ecb4e35f C |
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 | ||
cef534ed C |
83 | addNewVideoFromSubscriberNotification (to: string[], video: VideoModel) { |
84 | const channelName = video.VideoChannel.getDisplayName() | |
85 | const videoUrl = CONFIG.WEBSERVER.URL + video.getWatchStaticPath() | |
86 | ||
ecb4e35f | 87 | const text = `Hi dear user,\n\n` + |
cef534ed C |
88 | `Your subscription ${channelName} just published a new video: ${video.name}` + |
89 | `\n\n` + | |
90 | `You can view it on ${videoUrl} ` + | |
91 | `\n\n` + | |
ecb4e35f C |
92 | `Cheers,\n` + |
93 | `PeerTube.` | |
94 | ||
95 | const emailPayload: EmailPayload = { | |
cef534ed C |
96 | to, |
97 | subject: channelName + ' just published a new video', | |
ecb4e35f C |
98 | text |
99 | } | |
100 | ||
101 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
102 | } | |
103 | ||
f7cc67b4 C |
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 | ||
dc133480 C |
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 | ||
cef534ed C |
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` + | |
d9eaee39 JM |
193 | `Cheers,\n` + |
194 | `PeerTube.` | |
195 | ||
196 | const emailPayload: EmailPayload = { | |
cef534ed C |
197 | to, |
198 | subject: 'New comment on your video ' + video.name, | |
d9eaee39 JM |
199 | text |
200 | } | |
201 | ||
202 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
203 | } | |
204 | ||
f7cc67b4 C |
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) { | |
cef534ed | 228 | const videoUrl = CONFIG.WEBSERVER.URL + videoAbuse.Video.getWatchStaticPath() |
ba75d268 C |
229 | |
230 | const text = `Hi,\n\n` + | |
cef534ed | 231 | `${CONFIG.WEBSERVER.HOST} received an abuse for the following video ${videoUrl}\n\n` + |
ba75d268 C |
232 | `Cheers,\n` + |
233 | `PeerTube.` | |
234 | ||
ba75d268 C |
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 | ||
f7cc67b4 C |
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) { | |
cef534ed C |
260 | const videoName = videoBlacklist.Video.name |
261 | const videoUrl = CONFIG.WEBSERVER.URL + videoBlacklist.Video.getWatchStaticPath() | |
26b7305a | 262 | |
cef534ed C |
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}.` | |
26b7305a C |
265 | |
266 | const text = 'Hi,\n\n' + | |
267 | blockedString + | |
268 | '\n\n' + | |
269 | 'Cheers,\n' + | |
270 | `PeerTube.` | |
271 | ||
26b7305a | 272 | const emailPayload: EmailPayload = { |
cef534ed C |
273 | to, |
274 | subject: `[PeerTube] Video ${videoName} blacklisted`, | |
26b7305a C |
275 | text |
276 | } | |
277 | ||
278 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
279 | } | |
280 | ||
f7cc67b4 | 281 | addVideoUnblacklistNotification (to: string[], video: VideoModel) { |
cef534ed | 282 | const videoUrl = CONFIG.WEBSERVER.URL + video.getWatchStaticPath() |
26b7305a C |
283 | |
284 | const text = 'Hi,\n\n' + | |
cef534ed | 285 | `Your video ${video.name} (${videoUrl}) on ${CONFIG.WEBSERVER.HOST} has been unblacklisted.` + |
26b7305a C |
286 | '\n\n' + |
287 | 'Cheers,\n' + | |
288 | `PeerTube.` | |
289 | ||
26b7305a | 290 | const emailPayload: EmailPayload = { |
cef534ed | 291 | to, |
26b7305a C |
292 | subject: `[PeerTube] Video ${video.name} unblacklisted`, |
293 | text | |
294 | } | |
295 | ||
296 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
297 | } | |
298 | ||
cef534ed C |
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 | ||
eacb25c4 C |
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 | ||
a4101923 C |
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 | from: fromEmail, | |
365 | to: [ CONFIG.ADMIN.EMAIL ], | |
366 | subject: '[PeerTube] Contact form submitted', | |
367 | text | |
368 | } | |
369 | ||
370 | return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) | |
371 | } | |
372 | ||
373 | sendMail (to: string[], subject: string, text: string, from?: string) { | |
d3e56c0c | 374 | if (!Emailer.isEnabled()) { |
ecb4e35f C |
375 | throw new Error('Cannot send mail because SMTP is not configured.') |
376 | } | |
377 | ||
378 | return this.transporter.sendMail({ | |
a4101923 | 379 | from: from || CONFIG.SMTP.FROM_ADDRESS, |
ecb4e35f C |
380 | to: to.join(','), |
381 | subject, | |
382 | text | |
383 | }) | |
384 | } | |
385 | ||
386 | private dieOnConnectionFailure (err?: Error) { | |
d5b7d911 | 387 | logger.error('Failed to connect to SMTP %s:%d.', CONFIG.SMTP.HOSTNAME, CONFIG.SMTP.PORT, { err }) |
ecb4e35f C |
388 | process.exit(-1) |
389 | } | |
390 | ||
391 | static get Instance () { | |
392 | return this.instance || (this.instance = new this()) | |
393 | } | |
394 | } | |
395 | ||
396 | // --------------------------------------------------------------------------- | |
397 | ||
398 | export { | |
399 | Emailer | |
400 | } |