Просмотр исходного кода

implement method to test connection to esa

Ryu Sato 7 лет назад
Родитель
Сommit
74f329f5a5
4 измененных файлов с 118 добавлено и 44 удалено
  1. 13 3
      lib/routes/admin.js
  2. 2 2
      lib/routes/index.js
  3. 54 0
      lib/util/importer.js
  4. 49 39
      lib/views/admin/importer.html

+ 13 - 3
lib/routes/admin.js

@@ -15,6 +15,7 @@ module.exports = function(crowi, app) {
     , pluginUtils = new PluginUtils()
     , ApiResponse = require('../util/apiResponse')
     , recommendedXssWhiteList = require('../util/recommendedXssWhiteList')
+    , importer = require('../util/importer')(crowi)
 
     , MAX_PAGE_LIST = 50
     , actions = {};
@@ -1100,9 +1101,19 @@ module.exports = function(crowi, app) {
     }
   };
 
-  // app.get('/_api/importer/testEsaAPI');
+  /**
+   * Test connection to esa and response result with json
+   *
+   * @param {*} req
+   * @param {*} res
+   */
   actions.api.testEsaAPI = function(req, res) {
-    return res.json(ApiResponse.error());
+    importer.testConnectionToEsa(function(data) {
+      if (!data.status) {
+        return res.json({ status: false, message: req.form.errors.join('\n') });
+      }
+      return res.json({ status: true });
+    });
   }
 
   /**
@@ -1165,7 +1176,6 @@ module.exports = function(crowi, app) {
     }, callback);
   }
 
-
   return actions;
 };
 

+ 2 - 2
lib/routes/index.js

@@ -134,10 +134,10 @@ module.exports = function(crowi, app) {
   app.post('/admin/user-group-relation/create', loginRequired(crowi, app), middleware.adminRequired(), csrf, admin.userGroupRelation.create);
   app.post('/admin/user-group-relation/:id/remove-relation/:relationId', loginRequired(crowi, app), middleware.adminRequired(), csrf, admin.userGroupRelation.remove);
 
-  // importer management admin
+  // importer management for admin
   app.get('/admin/importer'                , loginRequired(crowi, app) , middleware.adminRequired() , admin.importer.index);
   app.post('/_api/admin/settings/importer' , loginRequired(crowi, app) , middleware.adminRequired() , csrf , form.admin.importer , admin.api.importerSetting);
-  app.post('/_api/imoprt/testEsaAPI'       , loginRequired(crowi, app) , form.admin.importer ,        admin.api.testEsaAPI);
+  app.post('/_api/admin/imoprt/testEsaAPI' , loginRequired(crowi, app) , middleware.adminRequired() , csrf , form.admin.importer , admin.api.testEsaAPI);
 
   app.get('/me'                       , loginRequired(crowi, app) , me.index);
   app.get('/me/password'              , loginRequired(crowi, app) , me.password);

+ 54 - 0
lib/util/importer.js

@@ -0,0 +1,54 @@
+/**
+ * importer
+ */
+
+module.exports = function(crowi) {
+  'use strict';
+
+  var debug = require('debug')('growi:lib:importer')
+    , esa = require('esa-nodejs')
+    , esaClient = {}
+    , config = crowi.getConfig()
+    , util = {}
+    ;
+
+  /**
+   * Initialize importer
+   */
+  function initialize() {
+    esaClient = esa({
+      team:        config.crowi['importer:esa:team_name'],
+      accessToken: config.crowi['importer:esa:access_token'],
+    });
+    debug('esa client is initialized');
+  }
+
+  /**
+   * Import post data from esa to GROWI
+   */
+  util.importAllPostsFromEsa = function() {
+    // [TODO] TBD
+  }
+
+  /**
+   * Import post data from esa to GROWI
+   */
+  util.testConnectionToEsa = function(callback) {
+    esaClient.api.team(function(err, res) {
+      if (err) {
+        console.log(`team: ${config.crowi['importer:esa:team_name']}, access_token: ${config.crowi['importer:esa:access_token']}`);
+        var errMessage = `Test connection to esa failed. 'esa-nodejs' return ${err}`;
+        console.log(errMessage);
+        debug(errMessage);
+        return callback({ status: false, message: errMessage });
+      }
+      console.log("test Connection To esa success");
+      return callback({ status: true });
+    })
+  }
+
+  initialize();
+  util.esaClient = esaClient;
+
+  return util;
+};

+ 49 - 39
lib/views/admin/importer.html

@@ -19,6 +19,7 @@
     </div>
     <div class="col-md-9">
 
+      <!-- Flash message for success -->
       {% set smessage = req.flash('successMessage') %}
       {% if smessage.length %}
       <div class="alert alert-success">
@@ -28,6 +29,7 @@
       </div>
       {% endif %}
 
+      <!-- Flash message for error -->
       {% set emessage = req.flash('errorMessage') %}
       {% if emessage.length %}
       <div class="alert alert-danger">
@@ -37,6 +39,7 @@
       </div>
       {% endif %}
 
+      <!-- Importer management forms -->
       <form action="/_api/admin/settings/importer" method="post" class="form-horizontal" id="importerSettingForm" role="form">
         <fieldset>
           <!-- esa importer -->
@@ -58,17 +61,18 @@
 
           <div class="form-group">
             <div class="col-xs-offset-3 col-xs-6">
-            <button type="button" class="btn btn-primary" click="importFromEsa">
-              {{ t("importer_management.import") }}
-            </button>
-            <button type="submit" class="btn btn-secondary">{# the first element is the default button to submit #}
-              <input type="hidden" name="_csrf" value="{{ csrf() }}">
-              {{ t('Update') }}
-            </button>
-            <button type="button"
-                class="btn btn-default" click="test-connection-to-esa" data-toggle="modal">
-              {{ t("importer_management.test_connection") }}
-            </button>
+              <button type="button" class="btn btn-primary" onClick="importFromEsa()">
+                {{ t("importer_management.import") }}
+              </button>
+              <button type="submit" class="btn btn-secondary">{# the first element is the default button to submit #}
+                <input type="hidden" name="_csrf" value="{{ csrf() }}">
+                {{ t('Update') }}
+              </button>
+              <span class="col-xs-offset-1">
+                <button type="button" class="btn btn-default" onClick="testConnectionToEsa(this)">
+                  {{ t("importer_management.test_connection") }}
+                </button>
+              </span>
             </div>
           </div>
         </fieldset>
@@ -79,23 +83,52 @@
 </div>
 
 <script>
+  /**
+   * show flash message
+   */
+  function showMessage(formId, msg, status) {
+    $('#' + formId + ' .alert').remove();
+
+    if (!status) {
+      status = 'success';
+    }
+    var $message = $('<p class="alert"></p>');
+    $message.addClass('alert-' + status);
+    $message.html(msg.replace('\n', '<br>'));
+    $message.insertAfter('#' + formId + ' legend');
+
+    if (status == 'success') {
+      setTimeout(function()
+      {
+        $message.fadeOut({
+          complete: function() {
+            $message.remove();
+          }
+        });
+      }, 5000);
+    }
+  }
+
   /**
    * test connection to esa
    */
-  function testConnectionToEsa() {
+  function testConnectionToEsa(buttonClicked) {
+    var $action = '/_api/admin/imoprt/testEsaAPI';
     var $form = $('#importerSettingForm');
-    var $action = '/_api/import/testEsaAPI';
+    var $id = $form.attr('id');
+    var $button = $(buttonClicked);
+    $button.attr('disabled', 'disabled');
     var jqxhr = $.post($action, $form.serialize(), function(data)
       {
         if (!data.status) {
-          alert("import failed");
+          showMessage($id, "test connection to esa failed", 'danger');
         }
         else {
-          alert("import success");
+          showMessage($id, "test connection to esa success");
         }
       })
       .fail(function() {
-        alert("failed");
+        showMessage($id, "エラーが発生しました", 'danger');
       })
       .always(function() {
         $button.prop('disabled', false);
@@ -116,29 +149,6 @@
   $('#importerSettingForm').each(function() {
     $(this).submit(function()
     {
-      function showMessage(formId, msg, status) {
-        $('#' + formId + ' .alert').remove();
-
-        if (!status) {
-          status = 'success';
-        }
-        var $message = $('<p class="alert"></p>');
-        $message.addClass('alert-' + status);
-        $message.html(msg.replace('\n', '<br>'));
-        $message.insertAfter('#' + formId + ' legend');
-
-        if (status == 'success') {
-          setTimeout(function()
-          {
-            $message.fadeOut({
-              complete: function() {
-                $message.remove();
-              }
-            });
-          }, 5000);
-        }
-      }
-
       var $form = $(this);
       var $id = $form.attr('id');
       var $button = $('button', this);