@@ -7,37 +7,36 @@ import {task} from 'ember-concurrency';
77
88export default Service . extend ( {
99 session : service ( ) ,
10- store : service ( ) ,
11- response : null ,
1210
1311 entries : null ,
1412 changelogUrl : 'https://ghost.org/blog/' ,
1513 isShowingModal : false ,
1614
17- _user : null ,
15+ // We only want to request the changelog once, so we store the initial
16+ // response so we don't request it again.
17+ _changelog_response : null ,
18+
19+ // Track only the whatsNew slice of user.accessibility settings
20+ _whatsNewSettings : null ,
1821
1922 init ( ) {
2023 this . _super ( ...arguments ) ;
2124 this . entries = [ ] ;
25+ // Set sensible default for new users - they've "seen" everything up to now
26+ this . _whatsNewSettings = {
27+ lastSeenDate : moment . utc ( ) . toISOString ( )
28+ } ;
2229 } ,
2330
24- whatsNewSettings : computed ( '_user.accessibility' , function ( ) {
25- let settingsJson = this . get ( '_user.accessibility' ) || '{}' ;
26- let settings = JSON . parse ( settingsJson ) ;
27- return settings . whatsNew ;
28- } ) ,
29-
30- hasNew : computed ( 'whatsNewSettings.lastSeenDate' , 'entries.[]' , function ( ) {
31+ hasNew : computed ( '_whatsNewSettings.lastSeenDate' , 'entries.[]' , function ( ) {
3132 if ( isEmpty ( this . entries ) ) {
3233 return false ;
3334 }
3435
3536 let [ latestEntry ] = this . entries ;
37+ let lastSeenMoment = moment ( this . _whatsNewSettings . lastSeenDate ) ;
38+ let latestMoment = moment ( latestEntry . published_at ) ;
3639
37- let lastSeenDate = this . get ( 'whatsNewSettings.lastSeenDate' ) || '2019-01-01 00:00:00' ;
38- let lastSeenMoment = moment ( lastSeenDate ) ;
39- let latestDate = latestEntry . published_at ;
40- let latestMoment = moment ( latestDate || lastSeenDate ) ;
4140 return latestMoment . isAfter ( lastSeenMoment ) ;
4241 } ) ,
4342
@@ -64,45 +63,61 @@ export default Service.extend({
6463 } ) ,
6564
6665 fetchLatest : task ( function * ( ) {
66+ if ( this . _changelog_response ) {
67+ // We've already fetched the changelog so we don't fetch it again.
68+ return ;
69+ }
70+
6771 try {
68- if ( ! this . response ) {
69- // we should already be logged in at this point so lets grab the user
70- // record and store it locally so that we don't have to deal with
71- // session.user being a promise and causing issues with CPs
72- let user = yield this . session . user ;
73- this . set ( '_user' , user ) ;
74-
75- this . response = yield fetch ( 'https://ghost.org/changelog.json' ) ;
76- if ( ! this . response . ok ) {
77- // eslint-disable-next-line
78- return console . error ( 'Failed to fetch changelog' , { response} ) ;
79- }
80-
81- let result = yield this . response . json ( ) ;
82- this . set ( 'entries' , result . posts || [ ] ) ;
83- this . set ( 'changelogUrl' , result . changelogUrl ) ;
72+ // Load user's persisted settings
73+ let user = yield this . session . user ;
74+ let accessibility = JSON . parse ( user . accessibility || '{}' ) ;
75+ let whatsNewSettings = accessibility . whatsNew ;
76+
77+ if ( ! whatsNewSettings ?. lastSeenDate ) {
78+ // New user - persist the defaults from init()
79+ whatsNewSettings = this . _whatsNewSettings ;
80+ accessibility . whatsNew = whatsNewSettings ;
81+ user . set ( 'accessibility' , JSON . stringify ( accessibility ) ) ;
82+ yield user . save ( ) ;
8483 }
84+
85+ // Always set (either loaded existing settings or the defaults we just persisted)
86+ this . set ( '_whatsNewSettings' , whatsNewSettings ) ;
87+
88+ // Fetch changelog
89+ this . _changelog_response = yield fetch ( 'https://ghost.org/changelog.json' ) ;
90+ if ( ! this . _changelog_response . ok ) {
91+ // eslint-disable-next-line
92+ return console . error ( 'Failed to fetch changelog' , this . _changelog_response ) ;
93+ }
94+
95+ let result = yield this . _changelog_response . json ( ) ;
96+ this . set ( 'entries' , result . posts || [ ] ) ;
97+ this . set ( 'changelogUrl' , result . changelogUrl ) ;
8598 } catch ( e ) {
8699 console . error ( e ) ; // eslint-disable-line
87100 }
88101 } ) ,
89102
90103 updateLastSeen : task ( function * ( ) {
91- let settingsJson = this . _user . accessibility || '{}' ;
92- let settings = JSON . parse ( settingsJson ) ;
93104 let [ latestEntry ] = this . entries ;
94105
95106 if ( ! latestEntry ) {
96107 return ;
97108 }
98109
99- if ( ! settings . whatsNew ) {
100- settings . whatsNew = { } ;
101- }
102-
103- settings . whatsNew . lastSeenDate = latestEntry . published_at ;
104-
105- this . _user . set ( 'accessibility' , JSON . stringify ( settings ) ) ;
106- yield this . _user . save ( ) ;
110+ // Update our local whatsNew settings
111+ this . set ( '_whatsNewSettings' , {
112+ ...this . _whatsNewSettings ,
113+ lastSeenDate : latestEntry . published_at
114+ } ) ;
115+
116+ // Persist using read-merge-write pattern
117+ let user = yield this . session . user ;
118+ let accessibility = JSON . parse ( user . accessibility || '{}' ) ;
119+ accessibility . whatsNew = this . _whatsNewSettings ;
120+ user . set ( 'accessibility' , JSON . stringify ( accessibility ) ) ;
121+ yield user . save ( ) ;
107122 } )
108123} ) ;
0 commit comments