From 47aa3d94aa46984b8340df1fc0d3578c235c1d88 Mon Sep 17 00:00:00 2001 From: Neil Lalonde Date: Wed, 23 Nov 2016 17:46:57 -0500 Subject: [PATCH] FEATURE: send digest preview to an email address --- .../admin-email-preview-digest.js.es6 | 22 +++++++++++++++++++ .../admin/models/email-preview.js.es6 | 14 ++++++++++++ .../admin/templates/email-preview-digest.hbs | 18 +++++++++++++++ app/controllers/admin/email_controller.rb | 19 ++++++++++++++++ config/locales/client.en.yml | 3 +++ config/routes.rb | 1 + 6 files changed, 77 insertions(+) diff --git a/app/assets/javascripts/admin/controllers/admin-email-preview-digest.js.es6 b/app/assets/javascripts/admin/controllers/admin-email-preview-digest.js.es6 index 160f998365b..b46fc4c2804 100644 --- a/app/assets/javascripts/admin/controllers/admin-email-preview-digest.js.es6 +++ b/app/assets/javascripts/admin/controllers/admin-email-preview-digest.js.es6 @@ -1,7 +1,12 @@ import EmailPreview from 'admin/models/email-preview'; +import { popupAjaxError } from 'discourse/lib/ajax-error'; export default Ember.Controller.extend({ + emailEmpty: Em.computed.empty('email'), + sendEmailDisabled: Em.computed.or('emailEmpty', 'sendingEmail'), + showSendEmailForm: Em.computed.notEmpty('model.html_content'), + actions: { refresh() { const model = this.get('model'); @@ -15,6 +20,23 @@ export default Ember.Controller.extend({ toggleShowHtml() { this.toggleProperty('showHtml'); + }, + + sendEmail() { + this.set('sendingEmail', true); + this.set('sentEmail', false); + + const self = this; + + EmailPreview.sendDigest(this.get('lastSeen'), this.get('username'), this.get('email')).then(result => { + if (result.errors) { + bootbox.alert(result.errors); + } else { + self.set('sentEmail', true); + } + }).catch(popupAjaxError).finally(function() { + self.set('sendingEmail', false); + }); } } diff --git a/app/assets/javascripts/admin/models/email-preview.js.es6 b/app/assets/javascripts/admin/models/email-preview.js.es6 index f992bf250d8..cfe96c6e0d5 100644 --- a/app/assets/javascripts/admin/models/email-preview.js.es6 +++ b/app/assets/javascripts/admin/models/email-preview.js.es6 @@ -17,6 +17,20 @@ EmailPreview.reopenClass({ }).then(function (result) { return EmailPreview.create(result); }); + }, + + sendDigest: function(lastSeenAt, username, email) { + if (Em.isEmpty(lastSeenAt)) { + lastSeenAt = moment().subtract(7, 'days').format('YYYY-MM-DD'); + } + + if (Em.isEmpty(username)) { + username = Discourse.User.current().username; + } + + return ajax("/admin/email/send-digest.json", { + data: { last_seen_at: lastSeenAt, username: username, email: email } + }); } }); diff --git a/app/assets/javascripts/admin/templates/email-preview-digest.hbs b/app/assets/javascripts/admin/templates/email-preview-digest.hbs index 7bd2a4455f1..f64245c154c 100644 --- a/app/assets/javascripts/admin/templates/email-preview-digest.hbs +++ b/app/assets/javascripts/admin/templates/email-preview-digest.hbs @@ -19,6 +19,24 @@ {{#conditional-loading-spinner condition=loading}} + + {{#if showSendEmailForm}} +
+
+ {{#if sendingEmail}} + {{i18n 'admin.email.sending_test'}} + {{else}} + + {{text-field value=email placeholderKey="admin.email.test_email_address"}} + + {{#if sentEmail}} + {{i18n 'admin.email.sent_test'}} + {{/if}} + {{/if}} +
+
+ {{/if}} + {{#if showHtml}} {{{model.html_content}}} {{else}} diff --git a/app/controllers/admin/email_controller.rb b/app/controllers/admin/email_controller.rb index 287b0ffdac0..06dfe3ad551 100644 --- a/app/controllers/admin/email_controller.rb +++ b/app/controllers/admin/email_controller.rb @@ -50,6 +50,25 @@ class Admin::EmailController < Admin::AdminController render json: MultiJson.dump(html_content: renderer.html, text_content: renderer.text) end + def send_digest + params.require(:last_seen_at) + params.require(:username) + params.require(:email) + user = User.find_by_username(params[:username]) + message, skip_reason = UserNotifications.send(:digest, user, {since: params[:last_seen_at]}) + if message + message.to = params[:email] + begin + Email::Sender.new(message, :digest).send + render nothing: true + rescue => e + render json: {errors: [e.message]}, status: 422 + end + else + render json: {errors: skip_reason} + end + end + def handle_mail params.require(:email) Email::Processor.process!(params[:email]) diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index 9753f742240..3212a03275a 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -2762,6 +2762,9 @@ en: delivery_method: "Delivery Method" preview_digest_desc: "Preview the content of the digest emails sent to inactive users." refresh: "Refresh" + send_digest_label: "Send this result to:" + send_digest: "Send" + sending_email: "Sending email..." format: "Format" html: "html" text: "text" diff --git a/config/routes.rb b/config/routes.rb index b511a21036b..35645a8fbc0 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -151,6 +151,7 @@ Discourse::Application.routes.draw do get "/incoming/:id/raw" => "email#raw_email" get "/incoming/:id" => "email#incoming" get "preview-digest" => "email#preview_digest" + get "send-digest" => "email#send_digest" post "handle_mail" end end