aboutsummaryrefslogtreecommitdiffhomepage
path: root/server
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2020-02-17 10:27:00 +0100
committerChocobozzz <me@florianbigard.com>2020-02-17 10:27:00 +0100
commit4c1c17093461b58d3ee3f23f239e340d8dac1149 (patch)
tree236b724f1415a812a4274fd63b519124cba2e42f /server
parent45f1bd72a08998c60a9dd68ff069cea9de39161c (diff)
downloadPeerTube-4c1c17093461b58d3ee3f23f239e340d8dac1149.tar.gz
PeerTube-4c1c17093461b58d3ee3f23f239e340d8dac1149.tar.zst
PeerTube-4c1c17093461b58d3ee3f23f239e340d8dac1149.zip
Refactor email enabled function
Diffstat (limited to 'server')
-rw-r--r--server/controllers/api/config.ts5
-rw-r--r--server/controllers/static.ts11
-rw-r--r--server/helpers/custom-validators/users.ts7
-rw-r--r--server/initializers/checker-after-init.ts5
-rw-r--r--server/initializers/config.ts7
-rw-r--r--server/lib/emailer.ts6
-rw-r--r--server/middlewares/validators/config.ts4
-rw-r--r--server/middlewares/validators/server.ts5
8 files changed, 25 insertions, 25 deletions
diff --git a/server/controllers/api/config.ts b/server/controllers/api/config.ts
index a383a723f..06fe30371 100644
--- a/server/controllers/api/config.ts
+++ b/server/controllers/api/config.ts
@@ -11,10 +11,9 @@ import { ClientHtml } from '../../lib/client-html'
11import { auditLoggerFactory, CustomConfigAuditView, getAuditIdFromRes } from '../../helpers/audit-logger' 11import { auditLoggerFactory, CustomConfigAuditView, getAuditIdFromRes } from '../../helpers/audit-logger'
12import { remove, writeJSON } from 'fs-extra' 12import { remove, writeJSON } from 'fs-extra'
13import { getServerCommit } from '../../helpers/utils' 13import { getServerCommit } from '../../helpers/utils'
14import { Emailer } from '../../lib/emailer'
15import validator from 'validator' 14import validator from 'validator'
16import { objectConverter } from '../../helpers/core-utils' 15import { objectConverter } from '../../helpers/core-utils'
17import { CONFIG, reloadConfig } from '../../initializers/config' 16import { CONFIG, isEmailEnabled, reloadConfig } from '../../initializers/config'
18import { PluginManager } from '../../lib/plugins/plugin-manager' 17import { PluginManager } from '../../lib/plugins/plugin-manager'
19import { getThemeOrDefault } from '../../lib/plugins/theme-utils' 18import { getThemeOrDefault } from '../../lib/plugins/theme-utils'
20import { Hooks } from '@server/lib/plugins/hooks' 19import { Hooks } from '@server/lib/plugins/hooks'
@@ -87,7 +86,7 @@ async function getConfig (req: express.Request, res: express.Response) {
87 default: defaultTheme 86 default: defaultTheme
88 }, 87 },
89 email: { 88 email: {
90 enabled: Emailer.isEnabled() 89 enabled: isEmailEnabled()
91 }, 90 },
92 contactForm: { 91 contactForm: {
93 enabled: CONFIG.CONTACT_FORM.ENABLED 92 enabled: CONFIG.CONTACT_FORM.ENABLED
diff --git a/server/controllers/static.ts b/server/controllers/static.ts
index 75d1a816b..271b788f6 100644
--- a/server/controllers/static.ts
+++ b/server/controllers/static.ts
@@ -1,15 +1,15 @@
1import * as cors from 'cors' 1import * as cors from 'cors'
2import * as express from 'express' 2import * as express from 'express'
3import { 3import {
4 CONSTRAINTS_FIELDS,
5 DEFAULT_THEME_NAME,
4 HLS_STREAMING_PLAYLIST_DIRECTORY, 6 HLS_STREAMING_PLAYLIST_DIRECTORY,
5 PEERTUBE_VERSION, 7 PEERTUBE_VERSION,
6 ROUTE_CACHE_LIFETIME, 8 ROUTE_CACHE_LIFETIME,
7 STATIC_DOWNLOAD_PATHS, 9 STATIC_DOWNLOAD_PATHS,
8 STATIC_MAX_AGE, 10 STATIC_MAX_AGE,
9 STATIC_PATHS, 11 STATIC_PATHS,
10 WEBSERVER, 12 WEBSERVER
11 CONSTRAINTS_FIELDS,
12 DEFAULT_THEME_NAME
13} from '../initializers/constants' 13} from '../initializers/constants'
14import { cacheRoute } from '../middlewares/cache' 14import { cacheRoute } from '../middlewares/cache'
15import { asyncMiddleware, videosDownloadValidator } from '../middlewares' 15import { asyncMiddleware, videosDownloadValidator } from '../middlewares'
@@ -19,8 +19,7 @@ import { VideoCommentModel } from '../models/video/video-comment'
19import { HttpNodeinfoDiasporaSoftwareNsSchema20 } from '../../shared/models/nodeinfo' 19import { HttpNodeinfoDiasporaSoftwareNsSchema20 } from '../../shared/models/nodeinfo'
20import { join } from 'path' 20import { join } from 'path'
21import { root } from '../helpers/core-utils' 21import { root } from '../helpers/core-utils'
22import { CONFIG } from '../initializers/config' 22import { CONFIG, isEmailEnabled } from '../initializers/config'
23import { Emailer } from '../lib/emailer'
24import { getPreview, getVideoCaption } from './lazy-static' 23import { getPreview, getVideoCaption } from './lazy-static'
25import { VideoStreamingPlaylistType } from '@shared/models/videos/video-streaming-playlist.type' 24import { VideoStreamingPlaylistType } from '@shared/models/videos/video-streaming-playlist.type'
26import { MVideoFile, MVideoFullLight } from '@server/typings/models' 25import { MVideoFile, MVideoFullLight } from '@server/typings/models'
@@ -249,7 +248,7 @@ async function generateNodeinfo (req: express.Request, res: express.Response) {
249 default: getThemeOrDefault(CONFIG.THEME.DEFAULT, DEFAULT_THEME_NAME) 248 default: getThemeOrDefault(CONFIG.THEME.DEFAULT, DEFAULT_THEME_NAME)
250 }, 249 },
251 email: { 250 email: {
252 enabled: Emailer.isEnabled() 251 enabled: isEmailEnabled()
253 }, 252 },
254 contactForm: { 253 contactForm: {
255 enabled: CONFIG.CONTACT_FORM.ENABLED 254 enabled: CONFIG.CONTACT_FORM.ENABLED
diff --git a/server/helpers/custom-validators/users.ts b/server/helpers/custom-validators/users.ts
index 63673bee2..1ddbe0815 100644
--- a/server/helpers/custom-validators/users.ts
+++ b/server/helpers/custom-validators/users.ts
@@ -3,7 +3,7 @@ import { UserRole } from '../../../shared'
3import { CONSTRAINTS_FIELDS, NSFW_POLICY_TYPES } from '../../initializers/constants' 3import { CONSTRAINTS_FIELDS, NSFW_POLICY_TYPES } from '../../initializers/constants'
4import { exists, isArray, isBooleanValid, isFileValid } from './misc' 4import { exists, isArray, isBooleanValid, isFileValid } from './misc'
5import { values } from 'lodash' 5import { values } from 'lodash'
6import { CONFIG } from '../../initializers/config' 6import { isEmailEnabled } from '../../initializers/config'
7 7
8const USERS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.USERS 8const USERS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.USERS
9 9
@@ -13,9 +13,8 @@ function isUserPasswordValid (value: string) {
13 13
14function isUserPasswordValidOrEmpty (value: string) { 14function isUserPasswordValidOrEmpty (value: string) {
15 // Empty password is only possible if emailing is enabled. 15 // Empty password is only possible if emailing is enabled.
16 if (value === '') { 16 if (value === '') return isEmailEnabled()
17 return !!CONFIG.SMTP.HOSTNAME && !!CONFIG.SMTP.PORT 17
18 }
19 return isUserPasswordValid(value) 18 return isUserPasswordValid(value)
20} 19}
21 20
diff --git a/server/initializers/checker-after-init.ts b/server/initializers/checker-after-init.ts
index 978023129..e01609eef 100644
--- a/server/initializers/checker-after-init.ts
+++ b/server/initializers/checker-after-init.ts
@@ -4,13 +4,12 @@ import { UserModel } from '../models/account/user'
4import { ApplicationModel } from '../models/application/application' 4import { ApplicationModel } from '../models/application/application'
5import { OAuthClientModel } from '../models/oauth/oauth-client' 5import { OAuthClientModel } from '../models/oauth/oauth-client'
6import { URL } from 'url' 6import { URL } from 'url'
7import { CONFIG } from './config' 7import { CONFIG, isEmailEnabled } from './config'
8import { logger } from '../helpers/logger' 8import { logger } from '../helpers/logger'
9import { getServerActor } from '../helpers/utils' 9import { getServerActor } from '../helpers/utils'
10import { RecentlyAddedStrategy } from '../../shared/models/redundancy' 10import { RecentlyAddedStrategy } from '../../shared/models/redundancy'
11import { isArray } from '../helpers/custom-validators/misc' 11import { isArray } from '../helpers/custom-validators/misc'
12import { uniq } from 'lodash' 12import { uniq } from 'lodash'
13import { Emailer } from '../lib/emailer'
14import { WEBSERVER } from './constants' 13import { WEBSERVER } from './constants'
15 14
16async function checkActivityPubUrls () { 15async function checkActivityPubUrls () {
@@ -41,7 +40,7 @@ function checkConfig () {
41 } 40 }
42 41
43 // Email verification 42 // Email verification
44 if (!Emailer.isEnabled()) { 43 if (!isEmailEnabled()) {
45 if (CONFIG.SIGNUP.ENABLED && CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION) { 44 if (CONFIG.SIGNUP.ENABLED && CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION) {
46 return 'Emailer is disabled but you require signup email verification.' 45 return 'Emailer is disabled but you require signup email verification.'
47 } 46 }
diff --git a/server/initializers/config.ts b/server/initializers/config.ts
index 75372fa4e..950ca61bd 100644
--- a/server/initializers/config.ts
+++ b/server/initializers/config.ts
@@ -284,11 +284,16 @@ function registerConfigChangedHandler (fun: Function) {
284 configChangedHandlers.push(fun) 284 configChangedHandlers.push(fun)
285} 285}
286 286
287function isEmailEnabled () {
288 return !!CONFIG.SMTP.HOSTNAME && !!CONFIG.SMTP.PORT
289}
290
287// --------------------------------------------------------------------------- 291// ---------------------------------------------------------------------------
288 292
289export { 293export {
290 CONFIG, 294 CONFIG,
291 registerConfigChangedHandler 295 registerConfigChangedHandler,
296 isEmailEnabled
292} 297}
293 298
294// --------------------------------------------------------------------------- 299// ---------------------------------------------------------------------------
diff --git a/server/lib/emailer.ts b/server/lib/emailer.ts
index 0f74d2a8c..d0874ab20 100644
--- a/server/lib/emailer.ts
+++ b/server/lib/emailer.ts
@@ -1,7 +1,7 @@
1import { createTransport, Transporter } from 'nodemailer' 1import { createTransport, Transporter } from 'nodemailer'
2import { isTestInstance } from '../helpers/core-utils' 2import { isTestInstance } from '../helpers/core-utils'
3import { bunyanLogger, logger } from '../helpers/logger' 3import { bunyanLogger, logger } from '../helpers/logger'
4import { CONFIG } from '../initializers/config' 4import { CONFIG, isEmailEnabled } from '../initializers/config'
5import { JobQueue } from './job-queue' 5import { JobQueue } from './job-queue'
6import { EmailPayload } from './job-queue/handlers/email' 6import { EmailPayload } from './job-queue/handlers/email'
7import { readFileSync } from 'fs-extra' 7import { readFileSync } from 'fs-extra'
@@ -40,7 +40,7 @@ class Emailer {
40 if (this.initialized === true) return 40 if (this.initialized === true) return
41 this.initialized = true 41 this.initialized = true
42 42
43 if (Emailer.isEnabled()) { 43 if (isEmailEnabled) {
44 logger.info('Using %s:%s as SMTP server.', CONFIG.SMTP.HOSTNAME, CONFIG.SMTP.PORT) 44 logger.info('Using %s:%s as SMTP server.', CONFIG.SMTP.HOSTNAME, CONFIG.SMTP.PORT)
45 45
46 let tls 46 let tls
@@ -459,7 +459,7 @@ class Emailer {
459 } 459 }
460 460
461 async sendMail (options: EmailPayload) { 461 async sendMail (options: EmailPayload) {
462 if (!Emailer.isEnabled()) { 462 if (!isEmailEnabled()) {
463 throw new Error('Cannot send mail because SMTP is not configured.') 463 throw new Error('Cannot send mail because SMTP is not configured.')
464 } 464 }
465 465
diff --git a/server/middlewares/validators/config.ts b/server/middlewares/validators/config.ts
index ceab646c0..dfa549e76 100644
--- a/server/middlewares/validators/config.ts
+++ b/server/middlewares/validators/config.ts
@@ -3,10 +3,10 @@ import { body } from 'express-validator'
3import { isUserNSFWPolicyValid, isUserVideoQuotaDailyValid, isUserVideoQuotaValid } from '../../helpers/custom-validators/users' 3import { isUserNSFWPolicyValid, isUserVideoQuotaDailyValid, isUserVideoQuotaValid } from '../../helpers/custom-validators/users'
4import { logger } from '../../helpers/logger' 4import { logger } from '../../helpers/logger'
5import { CustomConfig } from '../../../shared/models/server/custom-config.model' 5import { CustomConfig } from '../../../shared/models/server/custom-config.model'
6import { Emailer } from '../../lib/emailer'
7import { areValidationErrors } from './utils' 6import { areValidationErrors } from './utils'
8import { isThemeNameValid } from '../../helpers/custom-validators/plugins' 7import { isThemeNameValid } from '../../helpers/custom-validators/plugins'
9import { isThemeRegistered } from '../../lib/plugins/theme-utils' 8import { isThemeRegistered } from '../../lib/plugins/theme-utils'
9import { isEmailEnabled } from '@server/initializers/config'
10 10
11const customConfigUpdateValidator = [ 11const customConfigUpdateValidator = [
12 body('instance.name').exists().withMessage('Should have a valid instance name'), 12 body('instance.name').exists().withMessage('Should have a valid instance name'),
@@ -73,7 +73,7 @@ export {
73} 73}
74 74
75function checkInvalidConfigIfEmailDisabled (customConfig: CustomConfig, res: express.Response) { 75function checkInvalidConfigIfEmailDisabled (customConfig: CustomConfig, res: express.Response) {
76 if (Emailer.isEnabled()) return true 76 if (isEmailEnabled()) return true
77 77
78 if (customConfig.signup.requiresEmailVerification === true) { 78 if (customConfig.signup.requiresEmailVerification === true) {
79 res.status(400) 79 res.status(400)
diff --git a/server/middlewares/validators/server.ts b/server/middlewares/validators/server.ts
index f6812647b..6158c3363 100644
--- a/server/middlewares/validators/server.ts
+++ b/server/middlewares/validators/server.ts
@@ -5,9 +5,8 @@ import { isHostValid, isValidContactBody } from '../../helpers/custom-validators
5import { ServerModel } from '../../models/server/server' 5import { ServerModel } from '../../models/server/server'
6import { body } from 'express-validator' 6import { body } from 'express-validator'
7import { isUserDisplayNameValid } from '../../helpers/custom-validators/users' 7import { isUserDisplayNameValid } from '../../helpers/custom-validators/users'
8import { Emailer } from '../../lib/emailer'
9import { Redis } from '../../lib/redis' 8import { Redis } from '../../lib/redis'
10import { CONFIG } from '../../initializers/config' 9import { CONFIG, isEmailEnabled } from '../../initializers/config'
11 10
12const serverGetValidator = [ 11const serverGetValidator = [
13 body('host').custom(isHostValid).withMessage('Should have a valid host'), 12 body('host').custom(isHostValid).withMessage('Should have a valid host'),
@@ -50,7 +49,7 @@ const contactAdministratorValidator = [
50 .end() 49 .end()
51 } 50 }
52 51
53 if (Emailer.isEnabled() === false) { 52 if (isEmailEnabled() === false) {
54 return res 53 return res
55 .status(409) 54 .status(409)
56 .send({ error: 'Emailer is not enabled on this instance.' }) 55 .send({ error: 'Emailer is not enabled on this instance.' })