mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-03 03:55:26 +03:00
9bdb25d184
refs https://github.com/TryGhost/Team/issues/2110 - dynamically defined properties on the config service did not have autotracking set up properly if they were accessed in any way before the property was defined, this caused problems in a number of areas because we have both "unauthed" and "authed" sets of config and when not logged in we had parts of the app checking for authed config properties that don't exist until after sign-in and subsequent config re-fetch - renamed `config` service to `configManager` and updated to only contain methods for fetching config data - added a `config` instance initializer that sets up a `TrackedObject` instance with some custom properties/methods and registers it on `config:main` - uses application instance initializer rather than a standard initializer because standard initializers are only called once when setting up the test suite so we'd end up with config leaking across tests - added an `@inject` decorator that when used takes the property name and injects whatever is registered at `${propertyName}:main`, this allows us to use dependency injection for any object rather than just services or controllers - using `application.inject()` in the initializer was initially used but that only works for objects that extend from `EmberObject`, the injections weren't available in native-class glimmer components so this decorator keeps the injection syntax consistent - swapped all `@service config` uses to `@inject config`
144 lines
4.3 KiB
JavaScript
144 lines
4.3 KiB
JavaScript
import Component from '@ember/component';
|
|
import EmailFailedError from 'ghost-admin/errors/email-failed-error';
|
|
import classic from 'ember-classic-decorator';
|
|
import validator from 'validator';
|
|
import {action, computed} from '@ember/object';
|
|
import {alias, not, oneWay, or} from '@ember/object/computed';
|
|
import {htmlSafe} from '@ember/template';
|
|
import {inject} from 'ghost-admin/decorators/inject';
|
|
import {inject as service} from '@ember/service';
|
|
import {task, timeout} from 'ember-concurrency';
|
|
|
|
const RETRY_EMAIL_POLL_LENGTH = 1000;
|
|
const RETRY_EMAIL_MAX_POLL_LENGTH = 15 * 1000;
|
|
|
|
@classic
|
|
export default class Email extends Component {
|
|
@service ajax;
|
|
@service ghostPaths;
|
|
@service notifications;
|
|
@service session;
|
|
@service settings;
|
|
|
|
@inject config;
|
|
|
|
post = null;
|
|
sendTestEmailError = '';
|
|
savePostTask = null;
|
|
close() {}
|
|
|
|
@or('emailSubjectScratch', 'post.title')
|
|
emailSubject;
|
|
|
|
@alias('post.emailSubjectScratch')
|
|
emailSubjectScratch;
|
|
|
|
@oneWay('session.user.email')
|
|
testEmailAddress;
|
|
|
|
@not('mailgunIsEnabled')
|
|
mailgunError;
|
|
|
|
@computed(
|
|
'settings.{mailgunApiKey,mailgunDomain,mailgunBaseUrl}',
|
|
'config.mailgunIsConfigured'
|
|
)
|
|
get mailgunIsEnabled() {
|
|
return this.settings.mailgunApiKey && this.settings.mailgunDomain && this.settings.mailgunBaseUrl || this.config.mailgunIsConfigured;
|
|
}
|
|
|
|
@action
|
|
setEmailSubject(emailSubject) {
|
|
// Grab the post and current stored email subject
|
|
let post = this.post;
|
|
let currentEmailSubject = post.get('emailSubject');
|
|
|
|
// If the subject entered matches the stored email subject, do nothing
|
|
if (currentEmailSubject === emailSubject) {
|
|
return;
|
|
}
|
|
|
|
// If the subject entered is different, set it as the new email subject
|
|
post.set('emailSubject', emailSubject);
|
|
|
|
// Make sure the email subject is valid and if so, save it into the post
|
|
return post.validate({property: 'emailSubject'}).then(() => {
|
|
if (post.get('isNew')) {
|
|
return;
|
|
}
|
|
|
|
return this.savePostTask.perform();
|
|
});
|
|
}
|
|
|
|
@action
|
|
discardEnter() {
|
|
return false;
|
|
}
|
|
|
|
@(task(function* () {
|
|
try {
|
|
const resourceId = this.post.id;
|
|
const testEmail = this.testEmailAddress.trim();
|
|
if (!validator.isEmail(testEmail)) {
|
|
this.set('sendTestEmailError', 'Please enter a valid email');
|
|
return false;
|
|
}
|
|
if (!this.mailgunIsEnabled) {
|
|
this.set('sendTestEmailError', 'Please verify your email settings');
|
|
return false;
|
|
}
|
|
this.set('sendTestEmailError', '');
|
|
const url = this.ghostPaths.url.api('/email_previews/posts', resourceId);
|
|
const data = {emails: [testEmail]};
|
|
const options = {
|
|
data,
|
|
dataType: 'json'
|
|
};
|
|
yield this.ajax.post(url, options);
|
|
return true;
|
|
} 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.set('sendTestEmailError', message);
|
|
}
|
|
}
|
|
}).drop())
|
|
sendTestEmail;
|
|
|
|
@task(function* () {
|
|
let {email} = this.post;
|
|
|
|
if (email && email.status === 'failed') {
|
|
// trigger the retry
|
|
yield email.retry();
|
|
|
|
// poll for success/failure state
|
|
let pollTimeout = 0;
|
|
while (pollTimeout < RETRY_EMAIL_MAX_POLL_LENGTH) {
|
|
yield timeout(RETRY_EMAIL_POLL_LENGTH);
|
|
yield email.reload();
|
|
|
|
if (email.status === 'submitted') {
|
|
break;
|
|
}
|
|
if (email.status === 'failed') {
|
|
throw new EmailFailedError(email.error);
|
|
}
|
|
pollTimeout += RETRY_EMAIL_POLL_LENGTH;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
})
|
|
retryEmail;
|
|
}
|