FEATURE: Add 'Advanced Test' for admin panel.

FEATURE: Add 'Advanced Test' for admin panel.
From 56890efd7a464cbc5ffa7d3e99846c42a94dcc26 Mon Sep 17 00:00:00 2001
From: Bianca Nenciu <nenciu.bianca@gmail.com>
Date: Thu, 29 Nov 2018 01:03:50 +0200
Subject: [PATCH] FEATURE: Add 'Advanced Test' for admin panel.


diff --git a/app/assets/javascripts/admin/controllers/admin-email-advanced-test.js.es6 b/app/assets/javascripts/admin/controllers/admin-email-advanced-test.js.es6
new file mode 100644
index 0000000..2f9084b
--- /dev/null
+++ b/app/assets/javascripts/admin/controllers/admin-email-advanced-test.js.es6
@@ -0,0 +1,29 @@
+import { ajax } from "discourse/lib/ajax";
+import { popupAjaxError } from "discourse/lib/ajax-error";
+
+export default Ember.Controller.extend({
+  email: null,
+  text: null,
+  elided: null,
+  format: null,
+
+  actions: {
+    run() {
+      this.set("loading", true);
+
+      ajax("/admin/email/advanced-test", {
+        type: "POST",
+        data: { email: this.get("email") }
+      })
+        .then(data => {
+          this.setProperties({
+            text: data.text,
+            elided: data.elided,
+            format: data.format
+          });
+        })
+        .catch(popupAjaxError)
+        .finally(() => this.set("loading", false));
+    }
+  }
+});
diff --git a/app/assets/javascripts/admin/routes/admin-route-map.js.es6 b/app/assets/javascripts/admin/routes/admin-route-map.js.es6
index 110ec62..89adcfb 100644
--- a/app/assets/javascripts/admin/routes/admin-route-map.js.es6
+++ b/app/assets/javascripts/admin/routes/admin-route-map.js.es6
@@ -31,6 +31,7 @@ export default function() {
         this.route("received");
         this.route("rejected");
         this.route("previewDigest", { path: "/preview-digest" });
+        this.route("advancedTest", { path: "/advanced-test" });
       }
     );
 
diff --git a/app/assets/javascripts/admin/templates/email-advanced-test.hbs b/app/assets/javascripts/admin/templates/email-advanced-test.hbs
new file mode 100644
index 0000000..2fc02ad
--- /dev/null
+++ b/app/assets/javascripts/admin/templates/email-advanced-test.hbs
@@ -0,0 +1,25 @@
+<p>{{i18n 'admin.email.advanced_test.desc'}}</p>
+
+<div class='email-advanced-test'>
+  <label for="email">{{i18n 'admin.email.advanced_test.email'}}</label>
+  {{textarea name="email" value=email class="advanced-test"}}
+  <button class='btn' {{action "run"}}>{{i18n 'admin.email.advanced_test.run'}}</button>
+</div>
+
+{{#conditional-loading-spinner condition=loading}}
+
+{{#if format}}
+  <hr/>
+  <div class="text">
+    <h3>{{i18n 'admin.email.advanced_test.text'}}</h3>
+    <pre>{{{text}}}</pre>
+  </div>
+
+  <hr/>
+  <div class="elided">
+    <h3>{{i18n 'admin.email.advanced_test.elided'}}</h3>
+    <pre>{{{elided}}}</pre>
+  </div>
+{{/if}}
+
+{{/conditional-loading-spinner}}
diff --git a/app/assets/javascripts/admin/templates/email.hbs b/app/assets/javascripts/admin/templates/email.hbs
index 509d5d2..b8a379d 100644
--- a/app/assets/javascripts/admin/templates/email.hbs
+++ b/app/assets/javascripts/admin/templates/email.hbs
@@ -1,6 +1,7 @@
 {{#admin-nav}}
   {{nav-item route='adminEmail.index' label='admin.email.settings'}}
   {{nav-item route='adminEmail.previewDigest' label='admin.email.preview_digest'}}
+  {{nav-item route='adminEmail.advancedTest' label='admin.email.advanced_test.title'}}
   {{nav-item route='adminCustomizeEmailTemplates' label='admin.email.templates'}}
   {{nav-item route='adminEmail.sent' label='admin.email.sent'}}
   {{nav-item route='adminEmail.skipped' label='admin.email.skipped'}}
diff --git a/app/assets/stylesheets/common/admin/emails.scss b/app/assets/stylesheets/common/admin/emails.scss
index d7bb83c..6be0f2b 100644
--- a/app/assets/stylesheets/common/admin/emails.scss
+++ b/app/assets/stylesheets/common/admin/emails.scss
@@ -83,3 +83,15 @@
     border-width: 1px;
   }
 }
+
+.email-advanced-test {
+  .admin-controls {
+    display: block;
+  }
+
+  textarea {
+    width: 95%;
+    height: 150px;
+    font-family: monospace;
+  }
+}
diff --git a/app/controllers/admin/email_controller.rb b/app/controllers/admin/email_controller.rb
index 59d91fb..a3a5499 100644
--- a/app/controllers/admin/email_controller.rb
+++ b/app/controllers/admin/email_controller.rb
@@ -89,6 +89,19 @@ class Admin::EmailController < Admin::AdminController
     render json: MultiJson.dump(html_content: renderer.html, text_content: renderer.text)
   end
 
+  def advanced_test
+    params.require(:email)
+
+    receiver = Email::Receiver.new(params['email'])
+    text, elided, format = receiver.select_body
+
+    render json: success_json.merge!(
+      text: text,
+      elided: elided,
+      format: format
+    )
+  end
+
   def send_digest
     params.require(:last_seen_at)
     params.require(:username)
diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml
index de7874f..e1cad51 100644
--- a/config/locales/client.en.yml
+++ b/config/locales/client.en.yml
@@ -3413,6 +3413,13 @@ en:
         settings: "Settings"
         templates: "Templates"
         preview_digest: "Preview Summary"
+        advanced_test:
+          title: "Advanced Test"
+          desc: "See how Discourse processes recieved emails. To be able to correctly process the email, please paste below the whole original email message."
+          email: "Original message"
+          run: "Run Test"
+          text: "Selected Text Body"
+          elided: "Elided Text"
         sending_test: "Sending test Email..."
         error: "<b>ERROR</b> - %{server_error}"
         test_error: "There was a problem sending the test email. Please double-check your mail settings, verify that your host is not blocking mail connections, and try again."
diff --git a/config/routes.rb b/config/routes.rb
index c83a0eb..96cee53 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -159,6 +159,8 @@ Discourse::Application.routes.draw do
         get "send-digest" => "email#send_digest"
         get "smtp_should_reject"
         post "handle_mail"
+        get "advanced-test"
+        post "advanced-test" => "email#advanced_test"
       end
     end

GitHub