2021-01-22 12:24:33 +03:00
|
|
|
import Component from '@glimmer/component';
|
2021-01-27 01:49:05 +03:00
|
|
|
import validator from 'validator';
|
2021-01-22 12:24:33 +03:00
|
|
|
import {action} from '@ember/object';
|
2021-05-12 14:33:36 +03:00
|
|
|
import {htmlSafe} from '@ember/template';
|
2021-01-22 12:24:33 +03:00
|
|
|
import {inject as service} from '@ember/service';
|
2022-02-09 13:49:38 +03:00
|
|
|
import {task, timeout} from 'ember-concurrency';
|
2021-01-22 12:24:33 +03:00
|
|
|
import {tracked} from '@glimmer/tracking';
|
|
|
|
|
|
|
|
const INJECTED_CSS = `
|
|
|
|
html::-webkit-scrollbar {
|
|
|
|
display: none;
|
|
|
|
width: 0;
|
|
|
|
background: transparent
|
|
|
|
}
|
|
|
|
html {
|
|
|
|
scrollbar-width: none;
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
|
|
|
// TODO: remove duplication with <ModalPostEmailPreview>
|
|
|
|
export default class ModalPostPreviewEmailComponent extends Component {
|
|
|
|
@service ajax;
|
|
|
|
@service config;
|
2021-07-27 18:46:02 +03:00
|
|
|
@service feature;
|
2021-01-22 12:24:33 +03:00
|
|
|
@service ghostPaths;
|
2021-03-10 12:35:10 +03:00
|
|
|
@service session;
|
2021-01-22 12:24:33 +03:00
|
|
|
@service settings;
|
2022-04-28 12:52:42 +03:00
|
|
|
@service store;
|
2021-01-22 12:24:33 +03:00
|
|
|
|
|
|
|
@tracked html = '';
|
|
|
|
@tracked subject = '';
|
2021-07-27 18:31:26 +03:00
|
|
|
@tracked memberSegment = 'status:free';
|
2021-07-08 16:37:31 +03:00
|
|
|
@tracked previewEmailAddress = this.session.user.email;
|
2021-01-27 01:49:05 +03:00
|
|
|
@tracked sendPreviewEmailError = '';
|
2022-04-28 12:52:42 +03:00
|
|
|
@tracked newsletter = null;
|
2021-01-27 01:49:05 +03:00
|
|
|
|
|
|
|
get mailgunIsEnabled() {
|
|
|
|
return this.config.get('mailgunIsConfigured') ||
|
2021-02-01 11:59:08 +03:00
|
|
|
!!(this.settings.get('mailgunApiKey') && this.settings.get('mailgunDomain') && this.settings.get('mailgunBaseUrl'));
|
2021-01-27 01:49:05 +03:00
|
|
|
}
|
2021-01-22 12:24:33 +03:00
|
|
|
|
|
|
|
@action
|
|
|
|
async renderEmailPreview(iframe) {
|
2021-07-27 18:31:26 +03:00
|
|
|
this._previewIframe = iframe;
|
|
|
|
|
2021-01-22 12:24:33 +03:00
|
|
|
await this._fetchEmailData();
|
|
|
|
// avoid timing issues when _fetchEmailData didn't perform any async ops
|
|
|
|
await timeout(100);
|
|
|
|
|
|
|
|
if (iframe) {
|
|
|
|
iframe.contentWindow.document.open();
|
|
|
|
iframe.contentWindow.document.write(this.html);
|
|
|
|
iframe.contentWindow.document.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-27 18:31:26 +03:00
|
|
|
@action
|
|
|
|
changeMemberSegment(segment) {
|
|
|
|
this.memberSegment = segment;
|
|
|
|
|
|
|
|
if (this._previewIframe) {
|
|
|
|
this.renderEmailPreview(this._previewIframe);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-27 01:49:05 +03:00
|
|
|
@task({drop: true})
|
|
|
|
*sendPreviewEmailTask() {
|
|
|
|
try {
|
2021-02-01 11:59:08 +03:00
|
|
|
const resourceId = this.args.post.id;
|
|
|
|
const testEmail = this.previewEmailAddress.trim();
|
2021-01-27 01:49:05 +03:00
|
|
|
|
|
|
|
if (!validator.isEmail(testEmail)) {
|
|
|
|
this.sendPreviewEmailError = 'Please enter a valid email';
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!this.mailgunIsEnabled) {
|
|
|
|
this.sendPreviewEmailError = 'Please verify your email settings';
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
this.sendPreviewEmailError = '';
|
|
|
|
|
2022-04-25 17:38:56 +03:00
|
|
|
const url = this.ghostPaths.url.api('/email_previews/posts', resourceId);
|
2021-07-27 18:31:26 +03:00
|
|
|
const data = {emails: [testEmail], memberSegment: this.memberSegment};
|
2021-01-27 01:49:05 +03:00
|
|
|
const options = {
|
|
|
|
data,
|
|
|
|
dataType: 'json'
|
|
|
|
};
|
|
|
|
|
|
|
|
return yield this.ajax.post(url, options);
|
|
|
|
} catch (error) {
|
|
|
|
if (error) {
|
|
|
|
let message = 'Email could not be sent, verify mail settings';
|
|
|
|
|
|
|
|
// grab custom error message if present
|
|
|
|
if (
|
|
|
|
error.payload && error.payload.errors
|
|
|
|
&& error.payload.errors[0] && error.payload.errors[0].message) {
|
|
|
|
message = htmlSafe(error.payload.errors[0].message);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.sendPreviewEmailError = message;
|
2021-02-01 11:59:08 +03:00
|
|
|
throw error;
|
2021-01-27 01:49:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-22 12:24:33 +03:00
|
|
|
async _fetchEmailData() {
|
2021-07-27 18:31:26 +03:00
|
|
|
let {html, subject, memberSegment} = this;
|
2021-01-22 12:24:33 +03:00
|
|
|
let {post} = this.args;
|
|
|
|
|
2022-04-28 12:52:42 +03:00
|
|
|
// Fetch newsletter
|
|
|
|
if (!this.newsletter && post.newsletterId) {
|
|
|
|
const newsletters = (await this.store.query('newsletter', {filter: 'status:active+id:' + post.newsletterId, limit: 1})).toArray();
|
|
|
|
const defaultNewsletter = newsletters[0];
|
|
|
|
this.newsletter = defaultNewsletter;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.newsletter) {
|
|
|
|
const newsletters = (await this.store.query('newsletter', {filter: 'status:active', limit: 1})).toArray();
|
|
|
|
const defaultNewsletter = newsletters[0];
|
|
|
|
this.newsletter = defaultNewsletter;
|
|
|
|
}
|
|
|
|
|
2021-07-27 18:31:26 +03:00
|
|
|
if (html && subject && memberSegment === this._lastMemberSegment) {
|
2021-01-22 12:24:33 +03:00
|
|
|
return {html, subject};
|
|
|
|
}
|
|
|
|
|
2021-07-27 18:31:26 +03:00
|
|
|
this._lastMemberSegment = memberSegment;
|
|
|
|
|
2021-01-22 12:24:33 +03:00
|
|
|
// model is an email
|
|
|
|
if (post.html && post.subject) {
|
|
|
|
html = post.html;
|
|
|
|
subject = post.subject;
|
|
|
|
// model is a post with an existing email
|
|
|
|
} else if (post.email) {
|
|
|
|
html = post.email.html;
|
|
|
|
subject = post.email.subject;
|
|
|
|
// model is a post, fetch email preview
|
|
|
|
} else {
|
2022-04-25 17:38:56 +03:00
|
|
|
let url = new URL(this.ghostPaths.url.api('/email_previews/posts', post.id), window.location.href);
|
2021-09-02 15:11:15 +03:00
|
|
|
url.searchParams.set('memberSegment', this.memberSegment);
|
|
|
|
|
|
|
|
let response = await this.ajax.request(url.href);
|
2021-01-22 12:24:33 +03:00
|
|
|
let [emailPreview] = response.email_previews;
|
|
|
|
html = emailPreview.html;
|
|
|
|
subject = emailPreview.subject;
|
|
|
|
}
|
|
|
|
|
|
|
|
// inject extra CSS into the html for disabling links and scrollbars etc
|
|
|
|
let domParser = new DOMParser();
|
|
|
|
let htmlDoc = domParser.parseFromString(html, 'text/html');
|
|
|
|
let stylesheet = htmlDoc.querySelector('style');
|
|
|
|
let originalCss = stylesheet.innerHTML;
|
|
|
|
stylesheet.innerHTML = `${originalCss}\n\n${INJECTED_CSS}`;
|
2021-10-28 16:53:40 +03:00
|
|
|
|
|
|
|
const doctype = new XMLSerializer().serializeToString(htmlDoc.doctype);
|
|
|
|
html = doctype + htmlDoc.documentElement.outerHTML;
|
2021-01-22 12:24:33 +03:00
|
|
|
|
|
|
|
this.html = html;
|
|
|
|
this.subject = subject;
|
|
|
|
}
|
|
|
|
}
|