Refactor email enabled function
authorChocobozzz <me@florianbigard.com>
Mon, 17 Feb 2020 09:27:00 +0000 (10:27 +0100)
committerChocobozzz <me@florianbigard.com>
Mon, 17 Feb 2020 09:27:00 +0000 (10:27 +0100)
client/src/app/+video-channels/video-channels.component.scss
client/src/app/header/search-typeahead.component.ts
client/src/sass/include/_variables.scss
server/controllers/api/config.ts
server/controllers/static.ts
server/helpers/custom-validators/users.ts
server/initializers/checker-after-init.ts
server/initializers/config.ts
server/lib/emailer.ts
server/middlewares/validators/config.ts
server/middlewares/validators/server.ts

index aa26a7e7b7fc4b977e3d09a8b46131b349c46f5b..6470629f853d200beb805158b52f39d25cc515b9 100644 (file)
     display: grid !important;
     grid-template-columns: 1fr auto;
     grid-template-rows: 1fr auto / 1fr auto;
-    grid-template-areas: "name buttons"
-                         "lower buttons";
+    grid-template-areas: "name buttons" "lower buttons";
 
     @media screen and (max-width: #{map-get($grid-breakpoints, lg)}) {
-      grid-template-areas: "name name"
-                           "lower buttons";
+      grid-template-areas: "name name" "lower buttons";
     }
   }
 
@@ -53,4 +51,4 @@
   my-subscribe-button {
     height: min-content;
   }
-}
\ No newline at end of file
+}
index 210a1474cbfa5aca191ece43a9e825bac437e20b..372601fa82c2c6a5fee8332d73143dc77aacce87 100644 (file)
@@ -1,17 +1,9 @@
-import {
-  Component,
-  OnInit,
-  OnDestroy,
-  QueryList,
-  ViewChild,
-  ElementRef
-} from '@angular/core'
-import { Router, Params, ActivatedRoute } from '@angular/router'
+import { Component, ElementRef, OnDestroy, OnInit, QueryList, ViewChild } from '@angular/core'
+import { ActivatedRoute, Params, Router } from '@angular/router'
 import { AuthService, ServerService } from '@app/core'
 import { first, tap } from 'rxjs/operators'
 import { ListKeyManager } from '@angular/cdk/a11y'
-import { UP_ARROW, DOWN_ARROW, ENTER } from '@angular/cdk/keycodes'
-import { SuggestionComponent, Result } from './suggestion.component'
+import { Result, SuggestionComponent } from './suggestion.component'
 import { of } from 'rxjs'
 import { ServerConfig } from '@shared/models'
 
@@ -145,13 +137,13 @@ export class SearchTypeaheadComponent implements OnInit, OnDestroy {
   handleKeyUp (event: KeyboardEvent) {
     event.stopImmediatePropagation()
     if (!this.keyboardEventsManager) return
-    
+
     switch (event.key) {
-      case "ArrowDown":
-      case "ArrowUp":
+      case 'ArrowDown':
+      case 'ArrowUp':
         this.keyboardEventsManager.onKeydown(event)
         break
-      case "Enter":
+      case 'Enter':
         this.newSearch = false
         this.doSearch()
         break
index d8db3f3f84de1ca80a21be5b4fc275e501cb074b..3fb8bb625d290c623996463fd8bc21d79130ff6d 100644 (file)
@@ -103,8 +103,8 @@ $variables: (
 
 $zindex: (
   header       :  1000,
-    /* header context */
-    headerLeft :    10,
+  /* header context */
+  headerLeft :    10,
   menu         : 11000,
   dropdown     : 12000,
   loadbar      : 13000,
index a383a723f8e42581015dad775763672eb536887d..06fe30371dc47f6f2bb7bcfeb88aa0faef7e9d1f 100644 (file)
@@ -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
index 75d1a816bb7bbd339ae33ac3dfc4764b4875665b..271b788f6688885301e972ba94b2937e09c072cc 100644 (file)
@@ -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
index 63673bee217e2b07616a1824840cc31f6022b450..1ddbe08153c6ff278108c11e43f15cb2078647b4 100644 (file)
@@ -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)
 }
 
index 97802312933e804a0894a7b903ed2bfe2bba03b4..e01609eefcc49e7ec4d855e365e7a91c5aa0b5c5 100644 (file)
@@ -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.'
     }
index 75372fa4eaa7f4208957fcbec510b443ad4cd708..950ca61bd336ed0034d8854571ccc483c053ef1c 100644 (file)
@@ -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
 }
 
 // ---------------------------------------------------------------------------
index 0f74d2a8c2bbf242ff3baf72ba9c64f8f4a2ed01..d0874ab209160f85a64f16063fe78c44adc7b8d1 100644 (file)
@@ -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.')
     }
 
index ceab646c0e9a7d750b889b71c8bee23c0604f582..dfa549e763dc6508a8da2527fecc1981b2e6498d 100644 (file)
@@ -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)
index f6812647bf3cc8cce20f6b902d98ef2742e1e4ee..6158c3363d9f6a6f7d041ef11498d89cc6797440 100644 (file)
@@ -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.' })