From 4c1c17093461b58d3ee3f23f239e340d8dac1149 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Mon, 17 Feb 2020 10:27:00 +0100 Subject: Refactor email enabled function --- server/controllers/api/config.ts | 5 ++--- server/controllers/static.ts | 11 +++++------ server/helpers/custom-validators/users.ts | 7 +++---- server/initializers/checker-after-init.ts | 5 ++--- server/initializers/config.ts | 7 ++++++- server/lib/emailer.ts | 6 +++--- server/middlewares/validators/config.ts | 4 ++-- server/middlewares/validators/server.ts | 5 ++--- 8 files changed, 25 insertions(+), 25 deletions(-) (limited to 'server') 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' import { auditLoggerFactory, CustomConfigAuditView, getAuditIdFromRes } from '../../helpers/audit-logger' import { remove, writeJSON } from 'fs-extra' import { getServerCommit } from '../../helpers/utils' -import { Emailer } from '../../lib/emailer' import validator from 'validator' import { objectConverter } from '../../helpers/core-utils' -import { CONFIG, reloadConfig } from '../../initializers/config' +import { CONFIG, isEmailEnabled, reloadConfig } from '../../initializers/config' import { PluginManager } from '../../lib/plugins/plugin-manager' import { getThemeOrDefault } from '../../lib/plugins/theme-utils' import { Hooks } from '@server/lib/plugins/hooks' @@ -87,7 +86,7 @@ async function getConfig (req: express.Request, res: express.Response) { default: defaultTheme }, email: { - enabled: Emailer.isEnabled() + enabled: isEmailEnabled() }, contactForm: { 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 @@ import * as cors from 'cors' import * as express from 'express' import { + CONSTRAINTS_FIELDS, + DEFAULT_THEME_NAME, HLS_STREAMING_PLAYLIST_DIRECTORY, PEERTUBE_VERSION, ROUTE_CACHE_LIFETIME, STATIC_DOWNLOAD_PATHS, STATIC_MAX_AGE, STATIC_PATHS, - WEBSERVER, - CONSTRAINTS_FIELDS, - DEFAULT_THEME_NAME + WEBSERVER } from '../initializers/constants' import { cacheRoute } from '../middlewares/cache' import { asyncMiddleware, videosDownloadValidator } from '../middlewares' @@ -19,8 +19,7 @@ import { VideoCommentModel } from '../models/video/video-comment' import { HttpNodeinfoDiasporaSoftwareNsSchema20 } from '../../shared/models/nodeinfo' import { join } from 'path' import { root } from '../helpers/core-utils' -import { CONFIG } from '../initializers/config' -import { Emailer } from '../lib/emailer' +import { CONFIG, isEmailEnabled } from '../initializers/config' import { getPreview, getVideoCaption } from './lazy-static' import { VideoStreamingPlaylistType } from '@shared/models/videos/video-streaming-playlist.type' import { MVideoFile, MVideoFullLight } from '@server/typings/models' @@ -249,7 +248,7 @@ async function generateNodeinfo (req: express.Request, res: express.Response) { default: getThemeOrDefault(CONFIG.THEME.DEFAULT, DEFAULT_THEME_NAME) }, email: { - enabled: Emailer.isEnabled() + enabled: isEmailEnabled() }, contactForm: { 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' import { CONSTRAINTS_FIELDS, NSFW_POLICY_TYPES } from '../../initializers/constants' import { exists, isArray, isBooleanValid, isFileValid } from './misc' import { values } from 'lodash' -import { CONFIG } from '../../initializers/config' +import { isEmailEnabled } from '../../initializers/config' const USERS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.USERS @@ -13,9 +13,8 @@ function isUserPasswordValid (value: string) { function isUserPasswordValidOrEmpty (value: string) { // Empty password is only possible if emailing is enabled. - if (value === '') { - return !!CONFIG.SMTP.HOSTNAME && !!CONFIG.SMTP.PORT - } + if (value === '') return isEmailEnabled() + return isUserPasswordValid(value) } 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' import { ApplicationModel } from '../models/application/application' import { OAuthClientModel } from '../models/oauth/oauth-client' import { URL } from 'url' -import { CONFIG } from './config' +import { CONFIG, isEmailEnabled } from './config' import { logger } from '../helpers/logger' import { getServerActor } from '../helpers/utils' import { RecentlyAddedStrategy } from '../../shared/models/redundancy' import { isArray } from '../helpers/custom-validators/misc' import { uniq } from 'lodash' -import { Emailer } from '../lib/emailer' import { WEBSERVER } from './constants' async function checkActivityPubUrls () { @@ -41,7 +40,7 @@ function checkConfig () { } // Email verification - if (!Emailer.isEnabled()) { + if (!isEmailEnabled()) { if (CONFIG.SIGNUP.ENABLED && CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION) { return 'Emailer is disabled but you require signup email verification.' } 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) { configChangedHandlers.push(fun) } +function isEmailEnabled () { + return !!CONFIG.SMTP.HOSTNAME && !!CONFIG.SMTP.PORT +} + // --------------------------------------------------------------------------- export { CONFIG, - registerConfigChangedHandler + registerConfigChangedHandler, + isEmailEnabled } // --------------------------------------------------------------------------- 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 @@ import { createTransport, Transporter } from 'nodemailer' import { isTestInstance } from '../helpers/core-utils' import { bunyanLogger, logger } from '../helpers/logger' -import { CONFIG } from '../initializers/config' +import { CONFIG, isEmailEnabled } from '../initializers/config' import { JobQueue } from './job-queue' import { EmailPayload } from './job-queue/handlers/email' import { readFileSync } from 'fs-extra' @@ -40,7 +40,7 @@ class Emailer { if (this.initialized === true) return this.initialized = true - if (Emailer.isEnabled()) { + if (isEmailEnabled) { logger.info('Using %s:%s as SMTP server.', CONFIG.SMTP.HOSTNAME, CONFIG.SMTP.PORT) let tls @@ -459,7 +459,7 @@ class Emailer { } async sendMail (options: EmailPayload) { - if (!Emailer.isEnabled()) { + if (!isEmailEnabled()) { throw new Error('Cannot send mail because SMTP is not configured.') } 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' import { isUserNSFWPolicyValid, isUserVideoQuotaDailyValid, isUserVideoQuotaValid } from '../../helpers/custom-validators/users' import { logger } from '../../helpers/logger' import { CustomConfig } from '../../../shared/models/server/custom-config.model' -import { Emailer } from '../../lib/emailer' import { areValidationErrors } from './utils' import { isThemeNameValid } from '../../helpers/custom-validators/plugins' import { isThemeRegistered } from '../../lib/plugins/theme-utils' +import { isEmailEnabled } from '@server/initializers/config' const customConfigUpdateValidator = [ body('instance.name').exists().withMessage('Should have a valid instance name'), @@ -73,7 +73,7 @@ export { } function checkInvalidConfigIfEmailDisabled (customConfig: CustomConfig, res: express.Response) { - if (Emailer.isEnabled()) return true + if (isEmailEnabled()) return true if (customConfig.signup.requiresEmailVerification === true) { 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 import { ServerModel } from '../../models/server/server' import { body } from 'express-validator' import { isUserDisplayNameValid } from '../../helpers/custom-validators/users' -import { Emailer } from '../../lib/emailer' import { Redis } from '../../lib/redis' -import { CONFIG } from '../../initializers/config' +import { CONFIG, isEmailEnabled } from '../../initializers/config' const serverGetValidator = [ body('host').custom(isHostValid).withMessage('Should have a valid host'), @@ -50,7 +49,7 @@ const contactAdministratorValidator = [ .end() } - if (Emailer.isEnabled() === false) { + if (isEmailEnabled() === false) { return res .status(409) .send({ error: 'Emailer is not enabled on this instance.' }) -- cgit v1.2.3