2828define ( function ( require , exports , module ) {
2929 require ( "./setup-login-service" ) ; // this adds loginService to KernalModeTrust
3030 require ( "./promotions" ) ;
31+ require ( "./login-utils" ) ;
3132
32- const Metrics = require ( "utils/Metrics" ) ;
33- const LoginUtils = require ( "./login-utils " ) ;
33+ const Metrics = require ( "utils/Metrics" ) ,
34+ Strings = require ( "strings " ) ;
3435
3536 const MS_IN_DAY = 10 * 24 * 60 * 60 * 1000 ;
3637 const TEN_MINUTES = 10 * 60 * 1000 ;
38+ const FREE_PLAN_VALIDITY_DAYS = 10000 ;
3739
3840 // the fallback salt is always a constant as this will only fail in rare circumstatnces and it needs to
3941 // be exactly same across versions of the app. Changing this will not affect the large majority of users and
@@ -351,8 +353,8 @@ define(function (require, exports, module) {
351353 const current = await getEffectiveEntitlements ( false ) ; // Get effective entitlements
352354
353355 // Check if we need to refresh
354- const expiredPlanName = LoginUtils . validTillExpired ( current , lastRecordedState ) ;
355- const hasChanged = LoginUtils . haveEntitlementsChanged ( current , lastRecordedState ) ;
356+ const expiredPlanName = KernalModeTrust . LoginUtils . validTillExpired ( current , lastRecordedState ) ;
357+ const hasChanged = KernalModeTrust . LoginUtils . haveEntitlementsChanged ( current , lastRecordedState ) ;
356358
357359 if ( expiredPlanName || hasChanged ) {
358360 console . log ( `Entitlements monitor detected changes, Expired: ${ expiredPlanName } ,` +
@@ -376,6 +378,37 @@ define(function (require, exports, module) {
376378 console . log ( 'Entitlements monitor started (10-minute interval)' ) ;
377379 }
378380
381+ function _validateAndFilterEntitlements ( entitlements ) {
382+ if ( ! entitlements ) {
383+ return ;
384+ }
385+
386+ const currentDate = Date . now ( ) ;
387+
388+ if ( entitlements . plan && ( ! entitlements . plan . validTill || currentDate > entitlements . plan . validTill ) ) {
389+ entitlements . plan = {
390+ ...entitlements . plan ,
391+ paidSubscriber : false ,
392+ name : Strings . USER_FREE_PLAN_NAME ,
393+ validTill : Date . now ( ) + ( FREE_PLAN_VALIDITY_DAYS * MS_IN_DAY )
394+ } ;
395+ }
396+
397+ const featureEntitlements = entitlements . entitlements ;
398+ if ( ! featureEntitlements ) {
399+ return ;
400+ }
401+
402+ for ( const featureName in featureEntitlements ) {
403+ const feature = featureEntitlements [ featureName ] ;
404+ if ( feature && feature . validTill && currentDate > feature . validTill ) {
405+ feature . activated = false ;
406+ feature . upgradeToPlan = feature . upgradeToPlan || brackets . config . main_pro_plan ;
407+ feature . subscribeURL = feature . subscribeURL || brackets . config . purchase_url ;
408+ }
409+ }
410+ }
411+
379412 /**
380413 * Get effective entitlements for determining feature availability throughout the app.
381414 * This is the primary API that should be used across Phoenix to check entitlements and enable/disable features.
@@ -479,13 +512,12 @@ define(function (require, exports, module) {
479512 // User has active trial
480513 if ( serverEntitlements && serverEntitlements . plan ) {
481514 // Logged-in user with trial
515+ _validateAndFilterEntitlements ( serverEntitlements ) ; // will prune invalid entitlements
482516 if ( serverEntitlements . plan . paidSubscriber ) {
483517 // Already a paid subscriber, return as-is
484- // todo we need to check and filter valid till for each fields that we are interested in.
485518 return serverEntitlements ;
486519 }
487520 // Enhance entitlements for trial user
488- // todo we need to prune and filter serverEntitlements valid till for each fields that we are interested in.
489521 // ie if any entitlement has valid till expired, we need to deactivate that entitlement
490522 return {
491523 ...serverEntitlements ,
0 commit comments