2018-01-11 01:57:43 +03:00
/* eslint-disable ghost/ember/alias-model-in-controller */
2017-08-22 10:53:26 +03:00
import Controller from '@ember/controller' ;
2017-10-31 18:27:25 +03:00
import boundOneWay from 'ghost-admin/utils/bound-one-way' ;
2017-08-22 10:53:26 +03:00
import { empty } from '@ember/object/computed' ;
2017-05-29 21:50:03 +03:00
import { isInvalidError } from 'ember-ajax/errors' ;
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' ;
2016-03-29 11:40:44 +03:00
export default Controller . extend ( {
2017-10-30 12:38:01 +03:00
ghostPaths : service ( ) ,
ajax : service ( ) ,
notifications : service ( ) ,
settings : service ( ) ,
2016-03-29 11:40:44 +03:00
2018-03-19 14:54:54 +03:00
leaveSettingsTransition : null ,
slackArray : null ,
2017-11-24 21:53:19 +03:00
init ( ) {
this . _super ( ... arguments ) ;
this . slackArray = [ ] ;
} ,
2017-10-31 18:27:25 +03:00
2018-01-11 20:43:23 +03:00
slackSettings : boundOneWay ( 'settings.slack.firstObject' ) ,
testNotificationDisabled : empty ( 'slackSettings.url' ) ,
2016-03-29 11:40:44 +03:00
2017-03-08 16:55:35 +03:00
actions : {
save ( ) {
2019-03-06 16:53:54 +03:00
this . save . perform ( ) ;
2017-03-08 16:55:35 +03:00
} ,
2016-03-29 11:40:44 +03:00
2017-03-08 16:55:35 +03:00
updateURL ( value ) {
2018-01-08 13:31:53 +03:00
value = typeof value === 'string' ? value . trim ( ) : value ;
2018-01-11 01:57:43 +03:00
this . set ( 'slackSettings.url' , value ) ;
this . get ( 'slackSettings.errors' ) . clear ( ) ;
2017-10-31 18:27:25 +03:00
} ,
2019-01-21 15:26:21 +03:00
updateUsername ( value ) {
value = typeof value === 'string' ? value . trimLeft ( ) : value ;
this . set ( 'slackSettings.username' , value ) ;
this . get ( 'slackSettings.errors' ) . clear ( ) ;
} ,
2017-10-31 18:27:25 +03:00
triggerDirtyState ( ) {
2019-03-06 16:53:54 +03:00
let slack = this . slackSettings ;
let slackArray = this . slackArray ;
let settings = this . settings ;
2017-10-31 18:27:25 +03:00
// Hack to trigger the `isDirty` state on the settings model by setting a new Array
// for slack rather that replacing the existing one which would still point to the
// same reference and therfore not setting the model into a dirty state
slackArray . clear ( ) . pushObject ( slack ) ;
settings . set ( 'slack' , slackArray ) ;
} ,
toggleLeaveSettingsModal ( transition ) {
2019-03-06 16:53:54 +03:00
let leaveTransition = this . leaveSettingsTransition ;
2017-10-31 18:27:25 +03:00
2019-03-06 16:53:54 +03:00
if ( ! transition && this . showLeaveSettingsModal ) {
2017-10-31 18:27:25 +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-31 18:27:25 +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 ;
let slackArray = this . slackArray ;
2017-10-31 18:27:25 +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-31 18:27:25 +03:00
return ;
}
// roll back changes on model props
settings . rollbackAttributes ( ) ;
slackArray . clear ( ) ;
return transition . retry ( ) ;
2016-03-29 11:40:44 +03:00
}
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 slack = this . slackSettings ;
let settings = this . settings ;
let slackArray = this . slackArray ;
2018-01-11 20:43:23 +03:00
try {
yield slack . validate ( ) ;
// clear existing objects in slackArray to make sure we only push the validated one
slackArray . clear ( ) . pushObject ( slack ) ;
yield settings . set ( 'slack' , slackArray ) ;
return yield settings . save ( ) ;
} catch ( error ) {
if ( error ) {
2019-03-06 16:53:54 +03:00
this . notifications . showAPIError ( error ) ;
2018-01-11 20:43:23 +03:00
throw error ;
}
}
} ) . drop ( ) ,
sendTestNotification : task ( function * ( ) {
2019-03-06 16:53:54 +03:00
let notifications = this . notifications ;
2018-01-11 20:43:23 +03:00
let slackApi = this . get ( 'ghostPaths.url' ) . api ( 'slack' , 'test' ) ;
try {
2019-03-06 16:53:54 +03:00
yield this . save . perform ( ) ;
yield this . ajax . post ( slackApi ) ;
2020-02-27 12:19:29 +03:00
notifications . showNotification ( 'Test notification sent' , { type : 'info' , key : 'slack-test.send.success' , description : 'Check your Slack channel for the test message' } ) ;
2018-01-11 20:43:23 +03:00
return true ;
} catch ( error ) {
notifications . showAPIError ( error , { key : 'slack-test:send' } ) ;
if ( ! isInvalidError ( error ) ) {
throw error ;
}
}
} ) . drop ( )
2016-03-29 11:40:44 +03:00
} ) ;