Browse Source

manage isMailerActive

yusuketk 5 years ago
parent
commit
48713efb7d
2 changed files with 28 additions and 28 deletions
  1. 6 21
      src/server/routes/apiv3/app-settings.js
  2. 22 7
      src/server/service/mail.js

+ 6 - 21
src/server/routes/apiv3/app-settings.js

@@ -182,6 +182,7 @@ module.exports = (crowi) => {
       fileUpload: crowi.configManager.getConfig('crowi', 'app:fileUpload'),
       siteUrl: crowi.configManager.getConfig('crowi', 'app:siteUrl'),
       envSiteUrl: crowi.configManager.getConfigFromEnvVars('crowi', 'app:siteUrl'),
+      isMailerActive: crowi.mailService.isMialerActive,
       fromAddress: crowi.configManager.getConfig('crowi', 'mail:from'),
       smtpHost: crowi.configManager.getConfig('crowi', 'mail:smtpHost'),
       smtpPort: crowi.configManager.getConfig('crowi', 'mail:smtpPort'),
@@ -294,22 +295,6 @@ module.exports = (crowi) => {
 
   });
 
-  /**
-   * send mail (Promise wrapper)
-   */
-  async function sendMailPromiseWrapper(smtpClient, options) {
-    return new Promise((resolve, reject) => {
-      smtpClient.sendMail(options, (err, res) => {
-        if (err) {
-          reject(err);
-        }
-        else {
-          resolve(res);
-        }
-      });
-    });
-  }
-
   /**
    * validate mail setting send test mail
    */
@@ -317,9 +302,9 @@ module.exports = (crowi) => {
 
     const { configManager, mailService } = crowi;
 
-    if (!mailService.isMailerSetup) {
-      throw Error('mailService is not setup');
-    }
+    // if (!mailService.isMailerSetup) {
+    //   throw Error('mailService is not setup');
+    // }
 
     const fromAddress = configManager.getConfig('crowi', 'mail:from');
     if (fromAddress == null) {
@@ -355,7 +340,7 @@ module.exports = (crowi) => {
       text: 'このメールは、WikiのSMTP設定のアップデートにより送信されています。',
     };
 
-    await sendMailPromiseWrapper(smtpClient, mailOptions);
+    await crowi.mailService.sendMailPromiseWrapper(smtpClient, mailOptions);
   }
 
   const updateMailSettinConfig = async function(requestMailSettingParams) {
@@ -371,7 +356,7 @@ module.exports = (crowi) => {
     mailService.publishUpdatedMessage();
 
     return {
-      isMailerSetup: mailService.isMailerSetup,
+      isMailerActive: mailService.isMailerActive,
       fromAddress: configManager.getConfig('crowi', 'mail:from'),
       smtpHost: configManager.getConfig('crowi', 'mail:smtpHost'),
       smtpPort: configManager.getConfig('crowi', 'mail:smtpPort'),

+ 22 - 7
src/server/service/mail.js

@@ -21,9 +21,9 @@ class MailService extends S2sMessageHandlable {
     this.mailer = {};
 
     /**
-     * the flag whether mailer is set up successfully
+     * the flag whether mailer is valid
      */
-    this.isMailerSetup = false;
+    this.isMailerActive = false;
 
     this.initialize();
   }
@@ -70,8 +70,6 @@ class MailService extends S2sMessageHandlable {
   initialize() {
     const { appService, configManager } = this;
 
-    this.isMailerSetup = false;
-
     if (!configManager.getConfig('crowi', 'mail:from')) {
       this.mailer = null;
       return;
@@ -81,11 +79,9 @@ class MailService extends S2sMessageHandlable {
 
     if (transmissionMethod === 'smtp') {
       this.mailer = this.createSMTPClient();
-      this.isMailerSetup = true;
     }
     else if (transmissionMethod === 'ses') {
       this.mailer = this.createSESClient();
-      this.isMailerSetup = true;
     }
     else {
       this.mailer = null;
@@ -170,7 +166,26 @@ class MailService extends S2sMessageHandlable {
     );
 
     config.text = output;
-    return this.mailer.sendMail(this.setupMailConfig(config));
+    return this.sendMailPromiseWrapper(this.mailer, this.setupMailConfig(config));
+  }
+
+
+  /**
+   * send mail (Promise wrapper)
+   */
+  async sendMailPromiseWrapper(mailer, options) {
+    return new Promise((resolve, reject) => {
+      mailer.sendMail(options, (err, res) => {
+        if (err) {
+          this.isMailerActive = false;
+          reject(err);
+        }
+        else {
+          this.isMailerActive = true;
+          resolve(res);
+        }
+      });
+    });
   }
 
 }