2018-01-11 01:57:43 +03:00
/* eslint-disable ghost/ember/alias-model-in-controller */
2017-06-02 00:02:03 +03:00
import $ from 'jquery' ;
2017-08-22 10:53:26 +03:00
import Controller from '@ember/controller' ;
2019-06-17 13:41:04 +03:00
import generatePassword from 'ghost-admin/utils/password-generator' ;
2019-01-22 16:09:38 +03:00
import validator from 'validator' ;
2017-05-23 11:50:04 +03:00
import {
2017-05-29 21:50:03 +03:00
IMAGE _EXTENSIONS ,
IMAGE _MIME _TYPES
2017-05-23 11:50:04 +03:00
} from 'ghost-admin/components/gh-image-uploader' ;
2017-08-22 10:53:26 +03:00
import { computed } from '@ember/object' ;
2017-10-30 12:38:01 +03:00
import { inject as service } from '@ember/service' ;
2020-05-11 13:37:35 +03:00
import { task } from 'ember-concurrency' ;
2015-05-11 18:35:55 +03:00
2019-06-17 13:41:04 +03:00
function randomPassword ( ) {
let word = generatePassword ( 6 ) ;
let randomN = Math . floor ( Math . random ( ) * 1000 ) ;
return word + randomN ;
}
2017-03-08 16:55:35 +03:00
export default Controller . extend ( {
2017-10-30 12:38:01 +03:00
config : service ( ) ,
ghostPaths : service ( ) ,
notifications : service ( ) ,
session : service ( ) ,
2018-01-11 01:57:43 +03:00
settings : service ( ) ,
2019-05-20 18:16:19 +03:00
ui : service ( ) ,
2015-06-13 17:34:09 +03:00
2017-05-23 11:50:04 +03:00
availableTimezones : null ,
imageExtensions : IMAGE _EXTENSIONS ,
imageMimeTypes : IMAGE _MIME _TYPES ,
_scratchFacebook : null ,
_scratchTwitter : null ,
2017-01-26 14:17:34 +03:00
2018-01-11 01:57:43 +03:00
privateRSSUrl : computed ( 'config.blogUrl' , 'settings.publicHash' , function ( ) {
2017-10-05 12:59:14 +03:00
let blogUrl = this . get ( 'config.blogUrl' ) ;
2018-01-11 01:57:43 +03:00
let publicHash = this . get ( 'settings.publicHash' ) ;
2017-10-05 12:59:14 +03:00
return ` ${ blogUrl } / ${ publicHash } /rss ` ;
} ) ,
2015-08-10 18:43:49 +03:00
actions : {
2017-05-18 13:48:37 +03:00
save ( ) {
2019-03-06 16:53:54 +03:00
this . save . perform ( ) ;
2017-05-18 13:48:37 +03:00
} ,
Timezones: Always use the timezone of blog setting
closes TryGhost/Ghost#6406
follow-up PR of #2
- adds a `timeZone` Service to provide the offset (=timezone reg. moment-timezone) of the users blog settings
- `gh-datetime-input` will read the offset of the timezone now and adjust the `publishedAt` date with it. This is the date which will be shown in the PSM 'Publish Date' field. When the user writes a new date/time, the offset is considered and will be deducted again before saving it to the model. This way, we always work with a UTC publish date except for this input field.
- gets `availableTimezones` from `configuration/timezones` API endpoint
- adds a `moment-utc` transform on all date attr (`createdAt`, `updatedAt`, `publishedAt`, `unsubscribedAt` and `lastLogin`) to only work with UTC times on serverside
- when switching the timezone in the select box, the user will be shown the local time of the selected timezone
- `createdAt`-property in `gh-user-invited` returns now `moment(createdAt).fromNow()` as `createdAt` is a moment date already
- added clock service to show actual time ticking below select box
- default timezone is '(GMT) Greenwich Mean Time : Dublin, Edinburgh, London'
- if no timezone is saved in the settings yet, the default value will be used
- shows the local time in 'Publish Date' in PSM by default, until user overwrites it
- adds dependency `moment-timezone 0.5.4` to `bower.json`
---------
**Tests:**
- sets except for clock service in test env
- adds fixtures to mirage
- adds `service.ajax` and `service:ghostPaths` to navigation-test.js
- adds unit test for `gh-format-timeago` helper
- updates acceptance test `general-setting`
- adds acceptance test for `editor`
- adds integration tests for `services/config` and `services/time-zone`
---------
**Todos:**
- [ ] Integration tests: ~~`services/config`~~, ~~`services/time-zone`~~, `components/gh-datetime-input`
- [x] Acceptance test: `editor`
- [ ] Unit tests: `utils/date-formatting`
- [ ] write issue for renaming date properties (e. g. `createdAt` to `createdAtUTC`) and translate those for server side with serializers
2016-02-02 10:04:40 +03:00
setTimezone ( timezone ) {
2020-06-24 17:34:59 +03:00
this . set ( 'settings.timezone' , timezone . name ) ;
Timezones: Always use the timezone of blog setting
closes TryGhost/Ghost#6406
follow-up PR of #2
- adds a `timeZone` Service to provide the offset (=timezone reg. moment-timezone) of the users blog settings
- `gh-datetime-input` will read the offset of the timezone now and adjust the `publishedAt` date with it. This is the date which will be shown in the PSM 'Publish Date' field. When the user writes a new date/time, the offset is considered and will be deducted again before saving it to the model. This way, we always work with a UTC publish date except for this input field.
- gets `availableTimezones` from `configuration/timezones` API endpoint
- adds a `moment-utc` transform on all date attr (`createdAt`, `updatedAt`, `publishedAt`, `unsubscribedAt` and `lastLogin`) to only work with UTC times on serverside
- when switching the timezone in the select box, the user will be shown the local time of the selected timezone
- `createdAt`-property in `gh-user-invited` returns now `moment(createdAt).fromNow()` as `createdAt` is a moment date already
- added clock service to show actual time ticking below select box
- default timezone is '(GMT) Greenwich Mean Time : Dublin, Edinburgh, London'
- if no timezone is saved in the settings yet, the default value will be used
- shows the local time in 'Publish Date' in PSM by default, until user overwrites it
- adds dependency `moment-timezone 0.5.4` to `bower.json`
---------
**Tests:**
- sets except for clock service in test env
- adds fixtures to mirage
- adds `service.ajax` and `service:ghostPaths` to navigation-test.js
- adds unit test for `gh-format-timeago` helper
- updates acceptance test `general-setting`
- adds acceptance test for `editor`
- adds integration tests for `services/config` and `services/time-zone`
---------
**Todos:**
- [ ] Integration tests: ~~`services/config`~~, ~~`services/time-zone`~~, `components/gh-datetime-input`
- [x] Acceptance test: `editor`
- [ ] Unit tests: `utils/date-formatting`
- [ ] write issue for renaming date properties (e. g. `createdAt` to `createdAtUTC`) and translate those for server side with serializers
2016-02-02 10:04:40 +03:00
} ,
2016-08-17 18:01:46 +03:00
2017-05-23 11:50:04 +03:00
removeImage ( image ) {
// setting `null` here will error as the server treats it as "null"
2019-03-06 16:53:54 +03:00
this . settings . set ( image , '' ) ;
2015-11-18 13:50:48 +03:00
} ,
2017-05-23 11:50:04 +03:00
/ * *
* Opens a file selection dialog - Triggered by "Upload Image" buttons ,
* searches for the hidden file input within the . gh - setting element
* containing the clicked button then simulates a click
* @ param { MouseEvent } event - MouseEvent fired by the button click
* /
triggerFileDialog ( event ) {
2017-11-22 20:04:48 +03:00
// simulate click to open file dialog
// using jQuery because IE11 doesn't support MouseEvent
$ ( event . target )
2017-11-30 10:00:25 +03:00
. closest ( '.gh-setting-action' )
2017-11-22 20:04:48 +03:00
. find ( 'input[type="file"]' )
. click ( ) ;
2016-03-03 11:52:27 +03:00
} ,
2017-05-23 11:50:04 +03:00
/ * *
* Fired after an image upload completes
2018-01-11 01:57:43 +03:00
* @ param { string } property - Property name to be set on ` this.settings `
2017-05-23 11:50:04 +03:00
* @ param { UploadResult [ ] } results - Array of UploadResult objects
2018-01-11 01:57:43 +03:00
* @ return { string } The URL that was set on ` this.settings.property `
2017-05-23 11:50:04 +03:00
* /
imageUploaded ( property , results ) {
if ( results [ 0 ] ) {
2019-03-06 16:53:54 +03:00
return this . settings . set ( property , results [ 0 ] . url ) ;
2017-05-23 11:50:04 +03:00
}
2017-01-26 14:17:34 +03:00
} ,
2018-01-11 17:16:42 +03:00
toggleIsPrivate ( isPrivate ) {
2019-03-06 16:53:54 +03:00
let settings = this . settings ;
2018-03-09 17:42:27 +03:00
settings . set ( 'isPrivate' , isPrivate ) ;
settings . get ( 'errors' ) . remove ( 'password' ) ;
let changedAttrs = settings . changedAttributes ( ) ;
2018-01-11 17:16:42 +03:00
// set a new random password when isPrivate is enabled
2018-03-09 17:42:27 +03:00
if ( isPrivate && changedAttrs . isPrivate ) {
settings . set ( 'password' , randomPassword ( ) ) ;
// reset the password when isPrivate is disabled
} else if ( changedAttrs . password ) {
settings . set ( 'password' , changedAttrs . password [ 0 ] ) ;
2018-01-11 17:16:42 +03:00
}
} ,
2017-10-04 13:49:30 +03:00
toggleLeaveSettingsModal ( transition ) {
2019-03-06 16:53:54 +03:00
let leaveTransition = this . leaveSettingsTransition ;
2017-10-04 13:49:30 +03:00
2019-03-06 16:53:54 +03:00
if ( ! transition && this . showLeaveSettingsModal ) {
2017-10-04 13:49:30 +03:00
this . set ( 'leaveSettingsTransition' , null ) ;
this . set ( 'showLeaveSettingsModal' , false ) ;
return ;
}
if ( ! leaveTransition || transition . targetName === leaveTransition . targetName ) {
this . set ( 'leaveSettingsTransition' , transition ) ;
// if a save is running, wait for it to finish then transition
2020-05-11 13:37:35 +03:00
if ( this . save . isRunning ) {
return this . save . last . then ( ( ) => {
2017-10-04 13:49:30 +03:00
transition . retry ( ) ;
} ) ;
}
// we genuinely have unsaved data, show the modal
this . set ( 'showLeaveSettingsModal' , true ) ;
}
} ,
leaveSettings ( ) {
2019-03-06 16:53:54 +03:00
let transition = this . leaveSettingsTransition ;
let settings = this . settings ;
2017-10-04 13:49:30 +03:00
if ( ! transition ) {
2019-03-06 16:53:54 +03:00
this . notifications . showAlert ( 'Sorry, there was an error in the application. Please let the Ghost team know what happened.' , { type : 'error' } ) ;
2017-10-04 13:49:30 +03:00
return ;
}
2018-01-11 01:57:43 +03:00
// roll back changes on settings props
settings . rollbackAttributes ( ) ;
2017-10-04 13:49:30 +03:00
return transition . retry ( ) ;
} ,
2016-03-03 11:52:27 +03:00
validateFacebookUrl ( ) {
2019-03-06 16:53:54 +03:00
let newUrl = this . _scratchFacebook ;
2018-01-11 01:57:43 +03:00
let oldUrl = this . get ( 'settings.facebook' ) ;
2016-03-03 11:52:27 +03:00
let errMessage = '' ;
2017-11-10 19:38:30 +03:00
// reset errors and validation
2018-01-11 01:57:43 +03:00
this . get ( 'settings.errors' ) . remove ( 'facebook' ) ;
this . get ( 'settings.hasValidated' ) . removeObject ( 'facebook' ) ;
2017-11-10 19:38:30 +03:00
2016-05-17 21:14:14 +03:00
if ( newUrl === '' ) {
2016-03-03 11:52:27 +03:00
// Clear out the Facebook url
2018-01-11 01:57:43 +03:00
this . set ( 'settings.facebook' , '' ) ;
2016-03-03 11:52:27 +03:00
return ;
}
2016-05-17 21:14:14 +03:00
// _scratchFacebook will be null unless the user has input something
if ( ! newUrl ) {
newUrl = oldUrl ;
}
2017-11-10 19:38:30 +03:00
try {
// strip any facebook URLs out
newUrl = newUrl . replace ( /(https?:\/\/)?(www\.)?facebook\.com/i , '' ) ;
2016-03-03 11:52:27 +03:00
2017-11-10 19:38:30 +03:00
// don't allow any non-facebook urls
if ( newUrl . match ( /^(http|\/\/)/i ) ) {
throw 'invalid url' ;
2016-05-16 21:16:40 +03:00
}
2016-03-03 11:52:27 +03:00
2017-11-10 19:38:30 +03:00
// strip leading / if we have one then concat to full facebook URL
newUrl = newUrl . replace ( /^\// , '' ) ;
newUrl = ` https://www.facebook.com/ ${ newUrl } ` ;
2016-03-03 11:52:27 +03:00
2017-11-10 19:38:30 +03:00
// don't allow URL if it's not valid
if ( ! validator . isURL ( newUrl ) ) {
throw 'invalid url' ;
2016-03-03 11:52:27 +03:00
}
2016-05-16 21:16:40 +03:00
2020-09-23 14:28:48 +03:00
this . settings . set ( 'facebook' , newUrl ) ;
this . settings . notifyPropertyChange ( 'facebook' ) ;
2017-11-10 19:38:30 +03:00
} catch ( e ) {
if ( e === 'invalid url' ) {
errMessage = 'The URL must be in a format like '
+ 'https://www.facebook.com/yourPage' ;
2018-01-11 01:57:43 +03:00
this . get ( 'settings.errors' ) . add ( 'facebook' , errMessage ) ;
2017-11-10 19:38:30 +03:00
return ;
}
throw e ;
} finally {
2018-01-11 01:57:43 +03:00
this . get ( 'settings.hasValidated' ) . pushObject ( 'facebook' ) ;
2016-03-03 11:52:27 +03:00
}
} ,
validateTwitterUrl ( ) {
2019-03-06 16:53:54 +03:00
let newUrl = this . _scratchTwitter ;
2018-01-11 01:57:43 +03:00
let oldUrl = this . get ( 'settings.twitter' ) ;
2016-03-03 11:52:27 +03:00
let errMessage = '' ;
2017-11-10 19:38:30 +03:00
// reset errors and validation
2018-01-11 01:57:43 +03:00
this . get ( 'settings.errors' ) . remove ( 'twitter' ) ;
this . get ( 'settings.hasValidated' ) . removeObject ( 'twitter' ) ;
2017-11-10 19:38:30 +03:00
2016-05-17 21:14:14 +03:00
if ( newUrl === '' ) {
// Clear out the Twitter url
2018-01-11 01:57:43 +03:00
this . set ( 'settings.twitter' , '' ) ;
2016-03-03 11:52:27 +03:00
return ;
}
2016-05-17 21:14:14 +03:00
// _scratchTwitter will be null unless the user has input something
if ( ! newUrl ) {
newUrl = oldUrl ;
}
2017-09-11 10:56:11 +03:00
if ( newUrl . match ( /(?:twitter\.com\/)(\S+)/ ) || newUrl . match ( /([a-z\d.]+)/i ) ) {
2016-05-16 21:16:40 +03:00
let username = [ ] ;
2016-03-03 11:52:27 +03:00
2016-05-16 21:16:40 +03:00
if ( newUrl . match ( /(?:twitter\.com\/)(\S+)/ ) ) {
2016-11-14 16:16:51 +03:00
[ , username ] = newUrl . match ( /(?:twitter\.com\/)(\S+)/ ) ;
2016-05-16 21:16:40 +03:00
} else {
[ username ] = newUrl . match ( /([^/]+)\/?$/mi ) ;
}
2016-03-03 11:52:27 +03:00
2016-05-16 21:16:40 +03:00
// check if username starts with http or www and show error if so
2017-09-11 10:56:11 +03:00
if ( username . match ( /^(http|www)|(\/)/ ) || ! username . match ( /^[a-z\d._]{1,15}$/mi ) ) {
errMessage = ! username . match ( /^[a-z\d._]{1,15}$/mi ) ? 'Your Username is not a valid Twitter Username' : 'The URL must be in a format like https://twitter.com/yourUsername' ;
2016-03-03 11:52:27 +03:00
2018-01-11 01:57:43 +03:00
this . get ( 'settings.errors' ) . add ( 'twitter' , errMessage ) ;
this . get ( 'settings.hasValidated' ) . pushObject ( 'twitter' ) ;
2016-03-03 11:52:27 +03:00
return ;
}
2016-05-16 21:16:40 +03:00
newUrl = ` https://twitter.com/ ${ username } ` ;
2020-09-23 14:28:48 +03:00
this . settings . get ( 'hasValidated' ) . pushObject ( 'twitter' ) ;
this . settings . set ( 'twitter' , newUrl ) ;
this . settings . notifyPropertyChange ( 'twitter' ) ;
2016-05-16 21:16:40 +03:00
} else {
2016-11-14 16:16:51 +03:00
errMessage = 'The URL must be in a format like '
+ 'https://twitter.com/yourUsername' ;
2018-01-11 01:57:43 +03:00
this . get ( 'settings.errors' ) . add ( 'twitter' , errMessage ) ;
this . get ( 'settings.hasValidated' ) . pushObject ( 'twitter' ) ;
2016-05-16 21:16:40 +03:00
return ;
2016-03-03 11:52:27 +03:00
}
2014-08-19 02:56:28 +04:00
}
2018-01-11 20:43:23 +03:00
} ,
_deleteTheme ( ) {
2019-03-06 16:53:54 +03:00
let theme = this . store . peekRecord ( 'theme' , this . themeToDelete . name ) ;
2018-01-11 20:43:23 +03:00
if ( ! theme ) {
return ;
}
return theme . destroyRecord ( ) . catch ( ( error ) => {
2019-03-06 16:53:54 +03:00
this . notifications . showAPIError ( error ) ;
2018-01-11 20:43:23 +03:00
} ) ;
} ,
2020-05-11 13:37:35 +03:00
save : task ( function * ( ) {
2019-03-06 16:53:54 +03:00
let notifications = this . notifications ;
let config = this . config ;
2018-01-11 20:43:23 +03:00
2020-09-23 14:28:48 +03:00
if ( this . settings . get ( 'twitter' ) !== this . _scratchTwitter ) {
this . send ( 'validateTwitterUrl' ) ;
}
if ( this . settings . get ( 'facebook' ) !== this . _scratchFacebook ) {
this . send ( 'validateFacebookUrl' ) ;
}
2018-01-11 20:43:23 +03:00
try {
2019-03-06 16:53:54 +03:00
let settings = yield this . settings . save ( ) ;
2018-01-11 20:43:23 +03:00
config . set ( 'blogTitle' , settings . get ( 'title' ) ) ;
2019-05-20 18:16:19 +03:00
// this forces the document title to recompute after a blog title change
this . ui . updateDocumentTitle ( ) ;
2018-01-11 20:43:23 +03:00
return settings ;
} catch ( error ) {
if ( error ) {
notifications . showAPIError ( error , { key : 'settings.save' } ) ;
}
throw error ;
}
} )
2014-03-21 06:55:32 +04:00
} ) ;