aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2021-03-12 10:22:17 +0100
committerChocobozzz <me@florianbigard.com>2021-03-24 18:18:41 +0100
commitcae2df6bdc3c3590df32bf7431a617177be30429 (patch)
treef1f9680f7ca39454748302bf290c9ac01361a2f5
parent32a18cbf33a7cdbbe3d4885d32e4b67e19cdc1cf (diff)
downloadPeerTube-cae2df6bdc3c3590df32bf7431a617177be30429.tar.gz
PeerTube-cae2df6bdc3c3590df32bf7431a617177be30429.tar.zst
PeerTube-cae2df6bdc3c3590df32bf7431a617177be30429.zip
Stricter email options typings
-rw-r--r--server/lib/emailer.ts53
-rw-r--r--shared/models/server/emailer.model.ts53
2 files changed, 69 insertions, 37 deletions
diff --git a/server/lib/emailer.ts b/server/lib/emailer.ts
index 187d4e86d..ce4134d59 100644
--- a/server/lib/emailer.ts
+++ b/server/lib/emailer.ts
@@ -7,7 +7,7 @@ import { MVideoBlacklistLightVideo, MVideoBlacklistVideo } from '@server/types/m
7import { MVideoImport, MVideoImportVideo } from '@server/types/models/video/video-import' 7import { MVideoImport, MVideoImportVideo } from '@server/types/models/video/video-import'
8import { SANITIZE_OPTIONS, TEXT_WITH_HTML_RULES } from '@shared/core-utils' 8import { SANITIZE_OPTIONS, TEXT_WITH_HTML_RULES } from '@shared/core-utils'
9import { AbuseState, EmailPayload, UserAbuse } from '@shared/models' 9import { AbuseState, EmailPayload, UserAbuse } from '@shared/models'
10import { SendEmailOptions } from '../../shared/models/server/emailer.model' 10import { SendEmailDefaultOptions } from '../../shared/models/server/emailer.model'
11import { isTestInstance, root } from '../helpers/core-utils' 11import { isTestInstance, root } from '../helpers/core-utils'
12import { bunyanLogger, logger } from '../helpers/logger' 12import { bunyanLogger, logger } from '../helpers/logger'
13import { CONFIG, isEmailEnabled } from '../initializers/config' 13import { CONFIG, isEmailEnabled } from '../initializers/config'
@@ -473,13 +473,10 @@ class Emailer {
473 } 473 }
474 474
475 addNewPeerTubeVersionNotification (to: string[], latestVersion: string) { 475 addNewPeerTubeVersionNotification (to: string[], latestVersion: string) {
476 const subject = `A new PeerTube version is available: ${latestVersion}`
477
478 const emailPayload: EmailPayload = { 476 const emailPayload: EmailPayload = {
479 to, 477 to,
480 template: 'peertube-version-new', 478 template: 'peertube-version-new',
481 subject, 479 subject: `A new PeerTube version is available: ${latestVersion}`,
482 text: subject,
483 locals: { 480 locals: {
484 latestVersion 481 latestVersion
485 } 482 }
@@ -491,13 +488,10 @@ class Emailer {
491 addNewPlugionVersionNotification (to: string[], plugin: MPlugin) { 488 addNewPlugionVersionNotification (to: string[], plugin: MPlugin) {
492 const pluginUrl = WEBSERVER.URL + '/admin/plugins/list-installed?pluginType=' + plugin.type 489 const pluginUrl = WEBSERVER.URL + '/admin/plugins/list-installed?pluginType=' + plugin.type
493 490
494 const subject = `A new plugin/theme version is available: ${plugin.name}@${plugin.latestVersion}`
495
496 const emailPayload: EmailPayload = { 491 const emailPayload: EmailPayload = {
497 to, 492 to,
498 template: 'plugin-version-new', 493 template: 'plugin-version-new',
499 subject, 494 subject: `A new plugin/theme version is available: ${plugin.name}@${plugin.latestVersion}`,
500 text: subject,
501 locals: { 495 locals: {
502 pluginName: plugin.name, 496 pluginName: plugin.name,
503 latestVersion: plugin.latestVersion, 497 latestVersion: plugin.latestVersion,
@@ -605,26 +599,27 @@ class Emailer {
605 }) 599 })
606 600
607 for (const to of options.to) { 601 for (const to of options.to) {
608 await email 602 const baseOptions: SendEmailDefaultOptions = {
609 .send(merge( 603 template: 'common',
610 { 604 message: {
611 template: 'common', 605 to,
612 message: { 606 from: options.from,
613 to, 607 subject: options.subject,
614 from: options.from, 608 replyTo: options.replyTo
615 subject: options.subject, 609 },
616 replyTo: options.replyTo 610 locals: { // default variables available in all templates
617 }, 611 WEBSERVER,
618 locals: { // default variables available in all templates 612 EMAIL: CONFIG.EMAIL,
619 WEBSERVER, 613 instanceName: CONFIG.INSTANCE.NAME,
620 EMAIL: CONFIG.EMAIL, 614 text: options.text,
621 instanceName: CONFIG.INSTANCE.NAME, 615 subject: options.subject
622 text: options.text, 616 }
623 subject: options.subject 617 }
624 } 618
625 }, 619 // overriden/new variables given for a specific template in the payload
626 options // overriden/new variables given for a specific template in the payload 620 const sendOptions = merge(baseOptions, options)
627 ) as SendEmailOptions) 621
622 await email.send(sendOptions)
628 .then(res => logger.debug('Sent email.', { res })) 623 .then(res => logger.debug('Sent email.', { res }))
629 .catch(err => logger.error('Error in email sender.', { err })) 624 .catch(err => logger.error('Error in email sender.', { err }))
630 } 625 }
diff --git a/shared/models/server/emailer.model.ts b/shared/models/server/emailer.model.ts
index 069ef0bab..39512d306 100644
--- a/shared/models/server/emailer.model.ts
+++ b/shared/models/server/emailer.model.ts
@@ -1,12 +1,49 @@
1export type SendEmailOptions = { 1type From = string | { name?: string, address: string }
2 to: string[]
3 2
4 template?: string 3interface Base extends Partial<SendEmailDefaultMessageOptions> {
4 to: string[] | string
5}
6
7interface MailTemplate extends Base {
8 template: string
5 locals?: { [key: string]: any } 9 locals?: { [key: string]: any }
10 text?: undefined
11}
12
13interface MailText extends Base {
14 text: string
6 15
7 // override defaults 16 locals?: Partial<SendEmailDefaultLocalsOptions> & {
8 subject?: string 17 title?: string
9 text?: string 18 action?: {
10 from?: string | { name?: string, address: string } 19 url: string
11 replyTo?: string 20 text: string
21 }
22 }
12} 23}
24
25interface SendEmailDefaultLocalsOptions {
26 instanceName: string
27 text: string
28 subject: string
29}
30
31interface SendEmailDefaultMessageOptions {
32 to: string[] | string
33 from: From
34 subject: string
35 replyTo: string
36}
37
38export type SendEmailDefaultOptions = {
39 template: 'common'
40
41 message: SendEmailDefaultMessageOptions
42
43 locals: SendEmailDefaultLocalsOptions & {
44 WEBSERVER: any
45 EMAIL: any
46 }
47}
48
49export type SendEmailOptions = MailTemplate | MailText