2017-08-22 10:53:26 +03:00
|
|
|
import Component from '@ember/component';
|
2019-11-21 02:23:23 +03:00
|
|
|
import EmailFailedError from 'ghost-admin/errors/email-failed-error';
|
2019-11-07 11:37:26 +03:00
|
|
|
import {action} from '@ember/object';
|
2017-08-22 10:53:26 +03:00
|
|
|
import {computed} from '@ember/object';
|
2020-11-23 15:20:26 +03:00
|
|
|
import {or, reads} from '@ember/object/computed';
|
2017-10-30 12:38:01 +03:00
|
|
|
import {inject as service} from '@ember/service';
|
2019-11-21 02:23:23 +03:00
|
|
|
import {task, timeout} from 'ember-concurrency';
|
|
|
|
|
|
|
|
const CONFIRM_EMAIL_POLL_LENGTH = 1000;
|
2019-11-22 18:09:48 +03:00
|
|
|
const CONFIRM_EMAIL_MAX_POLL_LENGTH = 15 * 1000;
|
2017-04-11 16:39:45 +03:00
|
|
|
|
|
|
|
export default Component.extend({
|
2017-10-30 12:38:01 +03:00
|
|
|
clock: service(),
|
2020-11-06 21:54:27 +03:00
|
|
|
feature: service(),
|
|
|
|
settings: service(),
|
|
|
|
config: service(),
|
2020-11-11 20:08:43 +03:00
|
|
|
session: service(),
|
|
|
|
store: service(),
|
2017-04-11 16:39:45 +03:00
|
|
|
|
2020-01-10 17:25:59 +03:00
|
|
|
backgroundTask: null,
|
2017-04-11 16:39:45 +03:00
|
|
|
classNames: 'gh-publishmenu',
|
2018-03-27 20:06:55 +03:00
|
|
|
displayState: 'draft',
|
2017-04-11 16:39:45 +03:00
|
|
|
post: null,
|
2018-03-27 20:06:55 +03:00
|
|
|
postStatus: 'draft',
|
2018-01-11 20:43:23 +03:00
|
|
|
runningText: null,
|
2020-01-10 17:25:59 +03:00
|
|
|
saveTask: null,
|
2020-11-06 21:54:27 +03:00
|
|
|
sendEmailWhenPublished: 'none',
|
2020-01-10 17:25:59 +03:00
|
|
|
typedDateError: null,
|
2017-04-11 16:39:45 +03:00
|
|
|
|
|
|
|
_publishedAtBlogTZ: null,
|
2018-01-11 20:43:23 +03:00
|
|
|
_previousStatus: null,
|
2017-04-11 16:39:45 +03:00
|
|
|
|
2017-05-23 16:30:00 +03:00
|
|
|
isClosing: null,
|
|
|
|
|
2019-11-07 11:37:26 +03:00
|
|
|
onClose() {},
|
|
|
|
|
2017-04-11 16:39:45 +03:00
|
|
|
forcePublishedMenu: reads('post.pastScheduledTime'),
|
|
|
|
|
2020-11-23 15:20:26 +03:00
|
|
|
hasEmailPermission: or('session.user.isOwner', 'session.user.isAdmin', 'session.user.isEditor'),
|
|
|
|
|
2021-01-28 18:25:21 +03:00
|
|
|
canSendEmail: computed('post.{isPost,email}', 'settings.{mailgunApiKey,mailgunDomain,mailgunBaseUrl}', 'config.mailgunIsConfigured', function () {
|
2020-11-06 21:54:27 +03:00
|
|
|
let mailgunIsConfigured = this.get('settings.mailgunApiKey') && this.get('settings.mailgunDomain') && this.get('settings.mailgunBaseUrl') || this.get('config.mailgunIsConfigured');
|
2021-01-25 12:23:03 +03:00
|
|
|
let isPost = this.post.isPost;
|
2020-11-06 21:54:27 +03:00
|
|
|
let hasSentEmail = !!this.post.email;
|
|
|
|
|
2021-01-28 18:25:21 +03:00
|
|
|
return mailgunIsConfigured && isPost && !hasSentEmail;
|
2020-11-06 21:54:27 +03:00
|
|
|
}),
|
|
|
|
|
2017-04-11 16:39:45 +03:00
|
|
|
postState: computed('post.{isPublished,isScheduled}', 'forcePublishedMenu', function () {
|
2019-03-06 16:53:54 +03:00
|
|
|
if (this.forcePublishedMenu || this.get('post.isPublished')) {
|
2017-04-11 16:39:45 +03:00
|
|
|
return 'published';
|
|
|
|
} else if (this.get('post.isScheduled')) {
|
|
|
|
return 'scheduled';
|
|
|
|
} else {
|
|
|
|
return 'draft';
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
|
|
|
|
triggerText: computed('postState', function () {
|
2019-03-06 16:53:54 +03:00
|
|
|
let state = this.postState;
|
2017-04-11 16:39:45 +03:00
|
|
|
|
|
|
|
if (state === 'published') {
|
|
|
|
return 'Update';
|
|
|
|
} else if (state === 'scheduled') {
|
|
|
|
return 'Scheduled';
|
|
|
|
} else {
|
|
|
|
return 'Publish';
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
|
2018-01-05 18:38:23 +03:00
|
|
|
_runningText: computed('postState', 'saveType', function () {
|
2019-03-06 16:53:54 +03:00
|
|
|
let saveType = this.saveType;
|
|
|
|
let postState = this.postState;
|
2017-07-20 13:46:31 +03:00
|
|
|
let runningText;
|
|
|
|
|
|
|
|
if (postState === 'draft') {
|
|
|
|
runningText = saveType === 'publish' ? 'Publishing' : 'Scheduling';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (postState === 'published') {
|
|
|
|
runningText = saveType === 'publish' ? 'Updating' : 'Unpublishing';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (postState === 'scheduled') {
|
|
|
|
runningText = saveType === 'schedule' ? 'Rescheduling' : 'Unscheduling';
|
|
|
|
}
|
|
|
|
|
|
|
|
return runningText || 'Publishing';
|
|
|
|
}),
|
|
|
|
|
2018-01-05 18:38:23 +03:00
|
|
|
buttonText: computed('postState', 'saveType', function () {
|
2019-03-06 16:53:54 +03:00
|
|
|
let saveType = this.saveType;
|
|
|
|
let postState = this.postState;
|
2017-06-15 20:35:23 +03:00
|
|
|
let buttonText;
|
|
|
|
|
|
|
|
if (postState === 'draft') {
|
|
|
|
buttonText = saveType === 'publish' ? 'Publish' : 'Schedule';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (postState === 'published') {
|
2017-07-20 13:46:31 +03:00
|
|
|
buttonText = saveType === 'publish' ? 'Update' : 'Unpublish';
|
2017-06-15 20:35:23 +03:00
|
|
|
}
|
2017-04-11 16:39:45 +03:00
|
|
|
|
2017-06-15 20:35:23 +03:00
|
|
|
if (postState === 'scheduled') {
|
2017-07-20 13:46:31 +03:00
|
|
|
buttonText = saveType === 'schedule' ? 'Reschedule' : 'Unschedule';
|
2017-06-15 20:35:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return buttonText || 'Publish';
|
|
|
|
}),
|
|
|
|
|
2018-01-05 18:38:23 +03:00
|
|
|
successText: computed('_previousStatus', 'postState', function () {
|
2019-03-06 16:53:54 +03:00
|
|
|
let postState = this.postState;
|
|
|
|
let previousStatus = this._previousStatus;
|
2017-06-15 20:35:23 +03:00
|
|
|
let buttonText;
|
|
|
|
|
|
|
|
if (previousStatus === 'draft') {
|
|
|
|
buttonText = postState === 'published' ? 'Published' : 'Scheduled';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (previousStatus === 'published') {
|
2017-07-20 13:46:31 +03:00
|
|
|
buttonText = postState === 'draft' ? 'Unpublished' : 'Updated';
|
2017-06-15 20:35:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (previousStatus === 'scheduled') {
|
2017-07-20 13:46:31 +03:00
|
|
|
buttonText = postState === 'draft' ? 'Unscheduled' : 'Rescheduled';
|
2017-06-15 20:35:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return buttonText;
|
2017-04-11 16:39:45 +03:00
|
|
|
}),
|
|
|
|
|
2018-03-27 20:06:55 +03:00
|
|
|
didReceiveAttrs() {
|
|
|
|
this._super(...arguments);
|
|
|
|
|
|
|
|
// update the displayState based on the post status but only after a
|
|
|
|
// save has finished to avoid swapping the menu prematurely and triggering
|
|
|
|
// calls to `setSaveType` due to the component re-rendering
|
|
|
|
// TODO: we should have a better way of dealing with this where we don't
|
|
|
|
// rely on the side-effect of component rendering calling setSaveType
|
2019-03-06 16:53:54 +03:00
|
|
|
let postStatus = this.postStatus;
|
2018-03-27 20:06:55 +03:00
|
|
|
if (postStatus !== this._postStatus) {
|
|
|
|
if (this.get('saveTask.isRunning')) {
|
|
|
|
this.get('saveTask.last').then(() => {
|
|
|
|
this.set('displayState', postStatus);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.set('displayState', postStatus);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-06 16:53:54 +03:00
|
|
|
this._postStatus = this.postStatus;
|
2020-11-23 15:20:26 +03:00
|
|
|
if (this.postStatus === 'draft' && this.canSendEmail && this.hasEmailPermission) {
|
2020-11-06 21:54:27 +03:00
|
|
|
// Set default newsletter recipients
|
|
|
|
if (this.post.visibility === 'public' || this.post.visibility === 'members') {
|
|
|
|
this.set('sendEmailWhenPublished', 'all');
|
|
|
|
} else {
|
|
|
|
this.set('sendEmailWhenPublished', 'paid');
|
|
|
|
}
|
|
|
|
}
|
2020-11-11 20:08:43 +03:00
|
|
|
this.countPaidMembers();
|
2018-03-27 20:06:55 +03:00
|
|
|
},
|
|
|
|
|
2017-04-11 16:39:45 +03:00
|
|
|
actions: {
|
|
|
|
setSaveType(saveType) {
|
2019-03-06 16:53:54 +03:00
|
|
|
let post = this.post;
|
2017-04-11 16:39:45 +03:00
|
|
|
|
|
|
|
this.set('saveType', saveType);
|
|
|
|
|
|
|
|
if (saveType === 'draft') {
|
|
|
|
post.set('statusScratch', 'draft');
|
|
|
|
} else if (saveType === 'schedule') {
|
|
|
|
post.set('statusScratch', 'scheduled');
|
|
|
|
} else if (saveType === 'publish') {
|
|
|
|
post.set('statusScratch', 'published');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-11-06 21:54:27 +03:00
|
|
|
setSendEmailWhenPublished(sendEmailWhenPublished) {
|
|
|
|
this.set('sendEmailWhenPublished', sendEmailWhenPublished);
|
|
|
|
},
|
|
|
|
|
2017-04-11 16:39:45 +03:00
|
|
|
open() {
|
|
|
|
this._cachePublishedAtBlogTZ();
|
2017-05-23 16:30:00 +03:00
|
|
|
this.set('isClosing', false);
|
2017-04-11 16:39:45 +03:00
|
|
|
this.get('post.errors').clear();
|
2019-03-06 16:53:54 +03:00
|
|
|
if (this.onOpen) {
|
|
|
|
this.onOpen();
|
2017-04-19 19:09:31 +03:00
|
|
|
}
|
2017-04-11 16:39:45 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
close(dropdown, e) {
|
2019-11-07 11:37:26 +03:00
|
|
|
// don't close the menu if the datepicker popup or confirm modal is clicked
|
|
|
|
if (e) {
|
|
|
|
let onDatepicker = !!e.target.closest('.ember-power-datepicker-content');
|
|
|
|
let onModal = !!e.target.closest('.fullscreen-modal-container');
|
|
|
|
|
|
|
|
if (onDatepicker || onModal) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-04-11 16:39:45 +03:00
|
|
|
}
|
|
|
|
|
2019-11-07 11:37:26 +03:00
|
|
|
if (!this._skipDropdownCloseCleanup) {
|
|
|
|
this._cleanup();
|
2017-04-19 19:09:31 +03:00
|
|
|
}
|
2019-11-07 11:37:26 +03:00
|
|
|
this._skipDropdownCloseCleanup = false;
|
2017-05-23 16:30:00 +03:00
|
|
|
|
2019-11-07 11:37:26 +03:00
|
|
|
this.onClose();
|
2017-05-23 16:30:00 +03:00
|
|
|
this.set('isClosing', true);
|
|
|
|
|
2017-04-11 16:39:45 +03:00
|
|
|
return true;
|
|
|
|
}
|
2018-01-11 20:43:23 +03:00
|
|
|
},
|
|
|
|
|
2020-11-11 20:08:43 +03:00
|
|
|
countPaidMembers: action(function () {
|
|
|
|
// TODO: remove editor conditional once editors can query member counts
|
|
|
|
if (!this.session.get('user.isEditor') && this.canSendEmail) {
|
|
|
|
this.countPaidMembersTask.perform();
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
|
|
|
|
countPaidMembersTask: task(function* () {
|
2021-01-28 19:32:21 +03:00
|
|
|
const result = yield this.store.query('member', {filter: 'subscribed:true+status:paid', limit: 1, page: 1});
|
2020-11-11 20:08:43 +03:00
|
|
|
const paidMemberCount = result.meta.pagination.total;
|
|
|
|
const freeMemberCount = this.memberCount - paidMemberCount;
|
|
|
|
this.set('paidMemberCount', paidMemberCount);
|
|
|
|
this.set('freeMemberCount', freeMemberCount);
|
|
|
|
|
2020-11-23 15:20:26 +03:00
|
|
|
if (this.postStatus === 'draft' && this.canSendEmail && this.hasEmailPermission) {
|
2020-11-11 20:08:43 +03:00
|
|
|
// Set default newsletter recipients
|
|
|
|
if (this.post.visibility === 'public' || this.post.visibility === 'members') {
|
|
|
|
if (paidMemberCount > 0 && freeMemberCount > 0) {
|
|
|
|
this.set('sendEmailWhenPublished', 'all');
|
|
|
|
} else if (!paidMemberCount && freeMemberCount > 0) {
|
|
|
|
this.set('sendEmailWhenPublished', 'free');
|
|
|
|
} else if (!freeMemberCount && paidMemberCount > 0) {
|
|
|
|
this.set('sendEmailWhenPublished', 'paid');
|
|
|
|
} else if (!freeMemberCount && !paidMemberCount) {
|
|
|
|
this.set('sendEmailWhenPublished', 'none');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const type = paidMemberCount > 0 ? 'paid' : 'none';
|
|
|
|
this.set('sendEmailWhenPublished', type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
|
2019-11-07 11:37:26 +03:00
|
|
|
// action is required because <GhFullscreenModal> only uses actions
|
|
|
|
confirmEmailSend: action(function () {
|
|
|
|
return this._confirmEmailSend.perform();
|
|
|
|
}),
|
|
|
|
|
|
|
|
_confirmEmailSend: task(function* () {
|
|
|
|
this.sendEmailConfirmed = true;
|
2019-11-21 02:23:23 +03:00
|
|
|
let post = yield this.save.perform();
|
|
|
|
|
|
|
|
// simulate a validation error if saving failed so that the confirm
|
|
|
|
// modal can react accordingly
|
|
|
|
if (!post || post.errors.length) {
|
|
|
|
throw null;
|
|
|
|
}
|
|
|
|
|
|
|
|
let pollTimeout = 0;
|
|
|
|
if (post.email && post.email.status !== 'submitted') {
|
|
|
|
while (pollTimeout < CONFIRM_EMAIL_MAX_POLL_LENGTH) {
|
|
|
|
yield timeout(CONFIRM_EMAIL_POLL_LENGTH);
|
|
|
|
post = yield post.reload();
|
|
|
|
|
|
|
|
if (post.email.status === 'submitted') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (post.email.status === 'failed') {
|
|
|
|
throw new EmailFailedError(post.email.error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return post;
|
2019-11-07 11:37:26 +03:00
|
|
|
}),
|
|
|
|
|
2019-11-22 18:09:48 +03:00
|
|
|
retryEmailSend: action(function () {
|
|
|
|
return this._retryEmailSend.perform();
|
|
|
|
}),
|
|
|
|
|
|
|
|
_retryEmailSend: task(function* () {
|
|
|
|
if (!this.post.email) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let email = yield this.post.email.retry();
|
|
|
|
|
|
|
|
let pollTimeout = 0;
|
|
|
|
if (email && email.status !== 'submitted') {
|
|
|
|
while (pollTimeout < CONFIRM_EMAIL_POLL_LENGTH) {
|
|
|
|
yield timeout(CONFIRM_EMAIL_POLL_LENGTH);
|
|
|
|
email = yield email.reload();
|
|
|
|
|
|
|
|
if (email.status === 'submitted') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (email.status === 'failed') {
|
|
|
|
throw new EmailFailedError(email.error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return email;
|
|
|
|
}),
|
|
|
|
|
2019-11-07 11:37:26 +03:00
|
|
|
openEmailConfirmationModal: action(function (dropdown) {
|
|
|
|
if (dropdown) {
|
|
|
|
this._skipDropdownCloseCleanup = true;
|
|
|
|
dropdown.actions.close();
|
|
|
|
}
|
|
|
|
this.set('showEmailConfirmationModal', true);
|
|
|
|
}),
|
|
|
|
|
|
|
|
closeEmailConfirmationModal: action(function () {
|
|
|
|
this.set('showEmailConfirmationModal', false);
|
|
|
|
this._cleanup();
|
|
|
|
}),
|
|
|
|
|
|
|
|
save: task(function* ({dropdown} = {}) {
|
2020-01-10 17:25:59 +03:00
|
|
|
let {
|
|
|
|
post,
|
|
|
|
sendEmailWhenPublished,
|
|
|
|
sendEmailConfirmed,
|
|
|
|
saveType,
|
|
|
|
typedDateError
|
|
|
|
} = this;
|
|
|
|
|
|
|
|
// don't allow save if an invalid schedule date is present
|
|
|
|
if (typedDateError) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-11-14 20:33:35 +03:00
|
|
|
|
2020-04-23 21:43:10 +03:00
|
|
|
// validate publishedAtBlog to avoid an alert when saving for already displayed errors
|
|
|
|
// important to do this before opening email confirmation modal too
|
|
|
|
try {
|
|
|
|
yield post.validate({property: 'publishedAtBlog'});
|
|
|
|
} catch (error) {
|
|
|
|
// re-throw if we don't have a validation error
|
|
|
|
if (error) {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-11-07 11:37:26 +03:00
|
|
|
if (
|
2019-11-14 20:33:35 +03:00
|
|
|
post.status === 'draft' &&
|
|
|
|
!post.email && // email sent previously
|
2020-11-06 21:54:27 +03:00
|
|
|
sendEmailWhenPublished && sendEmailWhenPublished !== 'none' &&
|
2019-11-14 20:33:35 +03:00
|
|
|
!sendEmailConfirmed // set once confirmed so normal save happens
|
2019-11-07 11:37:26 +03:00
|
|
|
) {
|
|
|
|
this.openEmailConfirmationModal(dropdown);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-21 02:23:23 +03:00
|
|
|
this.sendEmailConfirmed = false;
|
|
|
|
|
2018-01-11 20:43:23 +03:00
|
|
|
// runningText needs to be declared before the other states change during the
|
|
|
|
// save action.
|
2019-03-06 16:53:54 +03:00
|
|
|
this.set('runningText', this._runningText);
|
2018-01-11 20:43:23 +03:00
|
|
|
this.set('_previousStatus', this.get('post.status'));
|
2019-11-14 20:33:35 +03:00
|
|
|
this.setSaveType(saveType);
|
2019-11-04 13:25:24 +03:00
|
|
|
|
2018-01-11 20:43:23 +03:00
|
|
|
try {
|
2020-04-23 21:43:10 +03:00
|
|
|
// will show alert for non-date related failed validations
|
2019-11-14 20:33:35 +03:00
|
|
|
post = yield this.saveTask.perform({sendEmailWhenPublished});
|
|
|
|
|
2018-01-11 20:43:23 +03:00
|
|
|
this._cachePublishedAtBlogTZ();
|
|
|
|
return post;
|
|
|
|
} catch (error) {
|
|
|
|
// re-throw if we don't have a validation error
|
|
|
|
if (error) {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
|
|
|
|
_cachePublishedAtBlogTZ() {
|
|
|
|
this._publishedAtBlogTZ = this.get('post.publishedAtBlogTZ');
|
|
|
|
},
|
|
|
|
|
2019-11-07 11:37:26 +03:00
|
|
|
_cleanup() {
|
|
|
|
this.set('showConfirmEmailModal', false);
|
|
|
|
|
|
|
|
// when closing the menu we reset the publishedAtBlogTZ date so that the
|
|
|
|
// unsaved changes made to the scheduled date aren't reflected in the PSM
|
2019-03-06 16:53:54 +03:00
|
|
|
this.post.set('publishedAtBlogTZ', this._publishedAtBlogTZ);
|
2019-11-07 11:37:26 +03:00
|
|
|
|
|
|
|
this.post.set('statusScratch', null);
|
|
|
|
this.post.validate();
|
2017-04-11 16:39:45 +03:00
|
|
|
}
|
|
|
|
});
|