2015-02-13 07:22:32 +03:00
|
|
|
import Ember from 'ember';
|
2017-05-29 21:50:03 +03:00
|
|
|
import Model from 'ember-data/model';
|
|
|
|
import ValidationEngine from 'ghost-admin/mixins/validation-engine';
|
|
|
|
import attr from 'ember-data/attr';
|
|
|
|
import boundOneWay from 'ghost-admin/utils/bound-one-way';
|
2016-06-30 13:21:47 +03:00
|
|
|
import computed, {equal, filterBy} from 'ember-computed';
|
|
|
|
import injectService from 'ember-service/inject';
|
2017-04-05 20:45:35 +03:00
|
|
|
import moment from 'moment';
|
2017-04-11 16:39:45 +03:00
|
|
|
import observer from 'ember-metal/observer';
|
2017-05-29 21:50:03 +03:00
|
|
|
import {BLANK_DOC} from 'ghost-admin/components/gh-markdown-editor';
|
2016-11-14 16:16:51 +03:00
|
|
|
import {belongsTo, hasMany} from 'ember-data/relationships';
|
2017-04-11 16:39:45 +03:00
|
|
|
import {isBlank} from 'ember-utils';
|
2014-06-21 01:36:44 +04:00
|
|
|
|
2016-06-30 13:21:47 +03:00
|
|
|
// ember-cli-shims doesn't export these so we must get them manually
|
|
|
|
const {Comparable, compare} = Ember;
|
2015-10-28 14:36:45 +03:00
|
|
|
|
2016-06-17 13:18:17 +03:00
|
|
|
function statusCompare(postA, postB) {
|
|
|
|
let status1 = postA.get('status');
|
|
|
|
let status2 = postB.get('status');
|
|
|
|
|
|
|
|
// if any of those is empty
|
|
|
|
if (!status1 && !status2) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!status1 && status2) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!status2 && status1) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We have to make sure, that scheduled posts will be listed first
|
|
|
|
// after that, draft and published will be sorted alphabetically and don't need
|
|
|
|
// any manual comparison.
|
|
|
|
|
|
|
|
if (status1 === 'scheduled' && (status2 === 'draft' || status2 === 'published')) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (status2 === 'scheduled' && (status1 === 'draft' || status1 === 'published')) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return compare(status1.valueOf(), status2.valueOf());
|
|
|
|
}
|
|
|
|
|
|
|
|
function publishedAtCompare(postA, postB) {
|
|
|
|
let published1 = postA.get('publishedAtUTC');
|
|
|
|
let published2 = postB.get('publishedAtUTC');
|
|
|
|
|
|
|
|
if (!published1 && !published2) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!published1 && published2) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!published2 && published1) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return compare(published1.valueOf(), published2.valueOf());
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Model.extend(Comparable, ValidationEngine, {
|
2017-04-11 16:39:45 +03:00
|
|
|
config: injectService(),
|
|
|
|
ghostPaths: injectService(),
|
|
|
|
clock: injectService(),
|
|
|
|
settings: injectService(),
|
|
|
|
|
2014-06-21 01:36:44 +04:00
|
|
|
validationType: 'post',
|
|
|
|
|
2017-08-01 11:24:46 +03:00
|
|
|
author: belongsTo('user', {async: true}),
|
|
|
|
authorId: attr('string'),
|
|
|
|
createdAtUTC: attr('moment-utc'),
|
|
|
|
createdBy: attr(),
|
|
|
|
customExcerpt: attr(),
|
2015-10-28 14:36:45 +03:00
|
|
|
featured: attr('boolean', {defaultValue: false}),
|
2017-08-01 11:24:46 +03:00
|
|
|
featureImage: attr('string'),
|
2017-08-02 12:32:51 +03:00
|
|
|
codeinjectionFoot: attr('string', {defaultValue: ''}),
|
|
|
|
codeinjectionHead: attr('string', {defaultValue: ''}),
|
2017-08-03 14:45:14 +03:00
|
|
|
ogImage: attr('string'),
|
|
|
|
ogTitle: attr('string'),
|
|
|
|
ogDescription: attr('string'),
|
|
|
|
twitterImage: attr('string'),
|
|
|
|
twitterTitle: attr('string'),
|
|
|
|
twitterDescription: attr('string'),
|
2017-08-01 11:24:46 +03:00
|
|
|
html: attr('string'),
|
2017-05-31 18:01:46 +03:00
|
|
|
locale: attr('string'),
|
2016-01-23 21:12:22 +03:00
|
|
|
metaDescription: attr('string'),
|
2017-08-01 11:24:46 +03:00
|
|
|
metaTitle: attr('string'),
|
|
|
|
mobiledoc: attr('json-string', {defaultValue: () => BLANK_DOC}),
|
|
|
|
page: attr('boolean', {defaultValue: false}),
|
|
|
|
plaintext: attr('string'),
|
2016-06-14 11:11:59 +03:00
|
|
|
publishedAtUTC: attr('moment-utc'),
|
2016-01-23 21:12:22 +03:00
|
|
|
publishedBy: belongsTo('user', {async: true}),
|
2017-08-01 11:24:46 +03:00
|
|
|
slug: attr('string'),
|
|
|
|
status: attr('string', {defaultValue: 'draft'}),
|
2015-10-28 14:36:45 +03:00
|
|
|
tags: hasMany('tag', {
|
2015-09-03 14:06:50 +03:00
|
|
|
embedded: 'always',
|
|
|
|
async: false
|
|
|
|
}),
|
2017-08-01 11:24:46 +03:00
|
|
|
title: attr('string', {defaultValue: ''}),
|
|
|
|
updatedAtUTC: attr('moment-utc'),
|
|
|
|
updatedBy: attr(),
|
2015-10-28 14:36:45 +03:00
|
|
|
url: attr('string'),
|
2017-08-01 11:24:46 +03:00
|
|
|
uuid: attr('string'),
|
2014-10-25 01:09:50 +04:00
|
|
|
|
2017-04-11 16:39:45 +03:00
|
|
|
scratch: null,
|
|
|
|
titleScratch: null,
|
|
|
|
|
|
|
|
// HACK: used for validation so that date/time can be validated based on
|
|
|
|
// eventual status rather than current status
|
|
|
|
statusScratch: null,
|
|
|
|
|
|
|
|
// For use by date/time pickers - will be validated then converted to UTC
|
|
|
|
// on save. Updated by an observer whenever publishedAtUTC changes.
|
|
|
|
// Everything that revolves around publishedAtUTC only cares about the saved
|
|
|
|
// value so this should be almost entirely internal
|
|
|
|
publishedAtBlogDate: '',
|
|
|
|
publishedAtBlogTime: '',
|
|
|
|
|
2017-08-01 11:24:46 +03:00
|
|
|
customExcerptScratch: boundOneWay('customExcerpt'),
|
2017-08-02 12:32:51 +03:00
|
|
|
codeinjectionFootScratch: boundOneWay('codeinjectionFoot'),
|
|
|
|
codeinjectionHeadScratch: boundOneWay('codeinjectionHead'),
|
2017-04-11 16:39:45 +03:00
|
|
|
metaDescriptionScratch: boundOneWay('metaDescription'),
|
2017-08-02 12:32:51 +03:00
|
|
|
metaTitleScratch: boundOneWay('metaTitle'),
|
2017-08-03 14:45:14 +03:00
|
|
|
ogDescriptionScratch: boundOneWay('ogDescription'),
|
|
|
|
ogTitleScratch: boundOneWay('ogTitle'),
|
|
|
|
twitterDescriptionScratch: boundOneWay('twitterDescription'),
|
|
|
|
twitterTitleScratch: boundOneWay('twitterTitle'),
|
2017-04-11 16:39:45 +03:00
|
|
|
|
|
|
|
isPublished: equal('status', 'published'),
|
|
|
|
isDraft: equal('status', 'draft'),
|
|
|
|
internalTags: filterBy('tags', 'isInternal', true),
|
|
|
|
isScheduled: equal('status', 'scheduled'),
|
2015-05-26 05:10:50 +03:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
absoluteUrl: computed('url', 'ghostPaths.url', 'config.blogUrl', function () {
|
|
|
|
let blogUrl = this.get('config.blogUrl');
|
|
|
|
let postUrl = this.get('url');
|
2015-04-09 00:29:26 +03:00
|
|
|
return this.get('ghostPaths.url').join(blogUrl, postUrl);
|
|
|
|
}),
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
previewUrl: computed('uuid', 'ghostPaths.url', 'config.blogUrl', 'config.routeKeywords.preview', function () {
|
|
|
|
let blogUrl = this.get('config.blogUrl');
|
|
|
|
let uuid = this.get('uuid');
|
|
|
|
let previewKeyword = this.get('config.routeKeywords.preview');
|
2015-04-16 22:40:32 +03:00
|
|
|
// New posts don't have a preview
|
|
|
|
if (!uuid) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
return this.get('ghostPaths.url').join(blogUrl, previewKeyword, uuid);
|
|
|
|
}),
|
|
|
|
|
2017-04-11 16:39:45 +03:00
|
|
|
// check every second to see if we're past the scheduled time
|
|
|
|
// will only re-compute if this property is being observed elsewhere
|
|
|
|
pastScheduledTime: computed('isScheduled', 'publishedAtUTC', 'clock.second', function () {
|
|
|
|
if (this.get('isScheduled')) {
|
|
|
|
let now = moment.utc();
|
|
|
|
let publishedAtUTC = this.get('publishedAtUTC') || now;
|
|
|
|
let pastScheduledTime = publishedAtUTC.diff(now, 'hours', true) < 0;
|
2014-12-30 05:11:24 +03:00
|
|
|
|
2017-04-11 16:39:45 +03:00
|
|
|
// force a recompute
|
|
|
|
this.get('clock.second');
|
2014-10-25 01:09:50 +04:00
|
|
|
|
2017-04-11 16:39:45 +03:00
|
|
|
return pastScheduledTime;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}),
|
Scheduler UI
refs TryGhost/Ghost#6413 and TryGhost/Ghost#6870
needs TryGhost/Ghost#6861
- **Post Settings Menu (PSM)**:'Publish Date' input accepts a date from now, min. 2 minutes to allow scheduler processing on the server. Also, there will always be some delay between typing the date and clicking on the 'Schedule Post' button. If the user types a future date for an already published post, the date will be reseted and he sees the message, that the post needs to be unpublished first. Once, the date is accepted, the label will change to 'Scheduled Date'.
- adds a CP 'timeScheduled' to post model, which will return `true` if the publish time is currently in the future.
- **Changes to the button flow in editor**:
- if the the CP `timeScheduled` returns true, a different drop-down-menu will be shown: 'Schedule Post' replaces 'Publish Now' and 'Unschedule' replaces 'Unpublish'.
- Covering the _edge cases_, especially when a scheduled post is about to be published, while the user is in the editor.
- First, a new CP `scheduleCountdown` will return the remaining time, when the estimated publish time is 15 minutes from now. A notification with this live-ticker is shown next to the save button. Once, we reach a 2 minutes limit, another CP `statusFreeze` will return true and causes the save button to only show `Unschedule` in a red state, until we reach the publish time
- Once the publish time is reached, a CP `scheduledWillPublish` causes the buttons and the existing code to pretend we're already dealing with a publish post. At the moment, there's no way to make a background-fetch of the now serverside-scheduled post model from the server, so Ember doesn't know about the changed state at that time.
- Changes in the editor, which are done during this 'status freeze'-process will be saved back correctly, once the user hits 'Update Post' after the buttons changed back. A click on 'Unpublish' will change the status back to a draft.
- The user will get a regular 'toaster' notification that the post has been published.
- adds CP `isScheduled` for scheduled posts
- adds CP `offset` to component `gh-posts-list-item` and helper `gh-format-time-scheduled` to show schedule date in content overview.
- sets timeout in `gh-spin-button` to 10ms for `Ember.testing`
- changes error message in `gh-editor-base-controller` to be in one line, seperated with a `:`
TODOs:
- [x] new sort order for posts (1. scheduled, 2. draft, 3. published) (refs TryGhost/Ghost#6932)
- [ ] Move posts sorting from posts controller to model and refactor to use `Ember.comparable` mixin
- [x] Flows for draft -> scheduled -> published like described in TryGhost/Ghost#6870 incl. edge cases and button behaviour
- [x] Tests
- [x] new PSM behaviour for time/date in future
- [x] display publishedAt date with timezone offset on posts overview
2016-02-02 10:04:40 +03:00
|
|
|
|
2017-04-11 16:39:45 +03:00
|
|
|
publishedAtBlogTZ: computed('publishedAtBlogDate', 'publishedAtBlogTime', 'settings.activeTimezone', {
|
|
|
|
get() {
|
|
|
|
return this._getPublishedAtBlogTZ();
|
|
|
|
},
|
|
|
|
set(key, value) {
|
|
|
|
let momentValue = value ? moment(value) : null;
|
|
|
|
this._setPublishedAtBlogStrings(momentValue);
|
|
|
|
return this._getPublishedAtBlogTZ();
|
|
|
|
}
|
Scheduler UI
refs TryGhost/Ghost#6413 and TryGhost/Ghost#6870
needs TryGhost/Ghost#6861
- **Post Settings Menu (PSM)**:'Publish Date' input accepts a date from now, min. 2 minutes to allow scheduler processing on the server. Also, there will always be some delay between typing the date and clicking on the 'Schedule Post' button. If the user types a future date for an already published post, the date will be reseted and he sees the message, that the post needs to be unpublished first. Once, the date is accepted, the label will change to 'Scheduled Date'.
- adds a CP 'timeScheduled' to post model, which will return `true` if the publish time is currently in the future.
- **Changes to the button flow in editor**:
- if the the CP `timeScheduled` returns true, a different drop-down-menu will be shown: 'Schedule Post' replaces 'Publish Now' and 'Unschedule' replaces 'Unpublish'.
- Covering the _edge cases_, especially when a scheduled post is about to be published, while the user is in the editor.
- First, a new CP `scheduleCountdown` will return the remaining time, when the estimated publish time is 15 minutes from now. A notification with this live-ticker is shown next to the save button. Once, we reach a 2 minutes limit, another CP `statusFreeze` will return true and causes the save button to only show `Unschedule` in a red state, until we reach the publish time
- Once the publish time is reached, a CP `scheduledWillPublish` causes the buttons and the existing code to pretend we're already dealing with a publish post. At the moment, there's no way to make a background-fetch of the now serverside-scheduled post model from the server, so Ember doesn't know about the changed state at that time.
- Changes in the editor, which are done during this 'status freeze'-process will be saved back correctly, once the user hits 'Update Post' after the buttons changed back. A click on 'Unpublish' will change the status back to a draft.
- The user will get a regular 'toaster' notification that the post has been published.
- adds CP `isScheduled` for scheduled posts
- adds CP `offset` to component `gh-posts-list-item` and helper `gh-format-time-scheduled` to show schedule date in content overview.
- sets timeout in `gh-spin-button` to 10ms for `Ember.testing`
- changes error message in `gh-editor-base-controller` to be in one line, seperated with a `:`
TODOs:
- [x] new sort order for posts (1. scheduled, 2. draft, 3. published) (refs TryGhost/Ghost#6932)
- [ ] Move posts sorting from posts controller to model and refactor to use `Ember.comparable` mixin
- [x] Flows for draft -> scheduled -> published like described in TryGhost/Ghost#6870 incl. edge cases and button behaviour
- [x] Tests
- [x] new PSM behaviour for time/date in future
- [x] display publishedAt date with timezone offset on posts overview
2016-02-02 10:04:40 +03:00
|
|
|
}),
|
2014-05-09 09:00:10 +04:00
|
|
|
|
2017-04-11 16:39:45 +03:00
|
|
|
_getPublishedAtBlogTZ() {
|
|
|
|
let publishedAtUTC = this.get('publishedAtUTC');
|
|
|
|
let publishedAtBlogDate = this.get('publishedAtBlogDate');
|
|
|
|
let publishedAtBlogTime = this.get('publishedAtBlogTime');
|
|
|
|
let blogTimezone = this.get('settings.activeTimezone');
|
|
|
|
|
|
|
|
if (!publishedAtUTC && isBlank(publishedAtBlogDate) && isBlank(publishedAtBlogTime)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (publishedAtBlogDate && publishedAtBlogTime) {
|
|
|
|
let publishedAtBlog = moment.tz(`${publishedAtBlogDate} ${publishedAtBlogTime}`, blogTimezone);
|
2017-06-20 11:12:57 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Note:
|
|
|
|
* If you create a post and publish it, we send seconds to the database.
|
|
|
|
* If you edit the post afterwards, ember would send the date without seconds, because
|
|
|
|
* the `publishedAtUTC` is based on `publishedAtBlogTime`, which is only in seconds.
|
|
|
|
* The date time picker doesn't use seconds.
|
|
|
|
*
|
|
|
|
* This condition prevents the case:
|
|
|
|
* - you edit a post, but you don't change the published_at time
|
|
|
|
* - we keep the original date with seconds
|
|
|
|
*
|
|
|
|
* See https://github.com/TryGhost/Ghost/issues/8603#issuecomment-309538395.
|
|
|
|
*/
|
2017-06-23 08:13:54 +03:00
|
|
|
if (publishedAtUTC && publishedAtBlog.diff(publishedAtUTC.clone().startOf('minutes')) === 0) {
|
2017-06-20 11:12:57 +03:00
|
|
|
return publishedAtUTC;
|
|
|
|
}
|
|
|
|
|
2017-04-11 16:39:45 +03:00
|
|
|
return publishedAtBlog;
|
|
|
|
} else {
|
|
|
|
return moment.tz(this.get('publishedAtUTC'), blogTimezone);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_setPublishedAtBlogTZ: observer('publishedAtUTC', 'settings.activeTimezone', function () {
|
|
|
|
let publishedAtUTC = this.get('publishedAtUTC');
|
|
|
|
this._setPublishedAtBlogStrings(publishedAtUTC);
|
|
|
|
}).on('init'),
|
|
|
|
|
|
|
|
_setPublishedAtBlogStrings(momentDate) {
|
|
|
|
if (momentDate) {
|
|
|
|
let blogTimezone = this.get('settings.activeTimezone');
|
|
|
|
let publishedAtBlog = moment.tz(momentDate, blogTimezone);
|
|
|
|
|
|
|
|
this.set('publishedAtBlogDate', publishedAtBlog.format('YYYY-MM-DD'));
|
|
|
|
this.set('publishedAtBlogTime', publishedAtBlog.format('HH:mm'));
|
|
|
|
} else {
|
|
|
|
this.set('publishedAtBlogDate', '');
|
|
|
|
this.set('publishedAtBlogTime', '');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-06-19 22:31:56 +04:00
|
|
|
// remove client-generated tags, which have `id: null`.
|
|
|
|
// Ember Data won't recognize/update them automatically
|
|
|
|
// when returned from the server with ids.
|
2015-08-26 01:52:45 +03:00
|
|
|
// https://github.com/emberjs/data/issues/1829
|
2015-10-28 14:36:45 +03:00
|
|
|
updateTags() {
|
|
|
|
let tags = this.get('tags');
|
|
|
|
let oldTags = tags.filterBy('id', null);
|
2014-06-19 22:31:56 +04:00
|
|
|
|
|
|
|
tags.removeObjects(oldTags);
|
|
|
|
oldTags.invoke('deleteRecord');
|
2014-07-31 12:29:05 +04:00
|
|
|
},
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
isAuthoredByUser(user) {
|
2016-11-17 23:08:11 +03:00
|
|
|
return user.get('id') === this.get('authorId');
|
2016-06-17 13:18:17 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
// a custom sort function is needed in order to sort the posts list the same way the server would:
|
|
|
|
// status: scheduled, draft, published
|
|
|
|
// publishedAt: DESC
|
|
|
|
// updatedAt: DESC
|
|
|
|
// id: DESC
|
|
|
|
compare(postA, postB) {
|
|
|
|
let updated1 = postA.get('updatedAtUTC');
|
|
|
|
let updated2 = postB.get('updatedAtUTC');
|
|
|
|
let idResult,
|
|
|
|
publishedAtResult,
|
|
|
|
statusResult,
|
|
|
|
updatedAtResult;
|
|
|
|
|
|
|
|
// when `updatedAt` is undefined, the model is still
|
|
|
|
// being written to with the results from the server
|
|
|
|
if (postA.get('isNew') || !updated1) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (postB.get('isNew') || !updated2) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-11-17 23:08:11 +03:00
|
|
|
// TODO: revisit the ID sorting because we no longer have auto-incrementing IDs
|
|
|
|
idResult = compare(postA.get('id'), postB.get('id'));
|
2016-06-17 13:18:17 +03:00
|
|
|
statusResult = statusCompare(postA, postB);
|
|
|
|
updatedAtResult = compare(updated1.valueOf(), updated2.valueOf());
|
|
|
|
publishedAtResult = publishedAtCompare(postA, postB);
|
|
|
|
|
|
|
|
if (statusResult === 0) {
|
|
|
|
if (publishedAtResult === 0) {
|
|
|
|
if (updatedAtResult === 0) {
|
|
|
|
// This should be DESC
|
|
|
|
return idResult * -1;
|
|
|
|
}
|
|
|
|
// This should be DESC
|
|
|
|
return updatedAtResult * -1;
|
|
|
|
}
|
|
|
|
// This should be DESC
|
|
|
|
return publishedAtResult * -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return statusResult;
|
2017-04-11 16:39:45 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
// this is a hook added by the ValidationEngine mixin and is called after
|
|
|
|
// successful validation and before this.save()
|
|
|
|
//
|
|
|
|
// the publishedAtBlog{Date/Time} strings are set separately so they can be
|
|
|
|
// validated, grab that time if it exists and set the publishedAtUTC
|
|
|
|
beforeSave() {
|
|
|
|
let publishedAtBlogTZ = this.get('publishedAtBlogTZ');
|
|
|
|
let publishedAtUTC = publishedAtBlogTZ ? publishedAtBlogTZ.utc() : null;
|
|
|
|
this.set('publishedAtUTC', publishedAtUTC);
|
2014-06-19 22:31:56 +04:00
|
|
|
}
|
2014-03-03 00:12:06 +04:00
|
|
|
});
|