@@ -4,6 +4,7 @@ import { IGetAppsFilter } from './IGetAppsFilter';
4
4
import {
5
5
AppAccessorManager ,
6
6
AppApiManager ,
7
+ AppLicenseManager ,
7
8
AppListenerManager ,
8
9
AppSettingsManager ,
9
10
AppSlashCommandManager ,
@@ -14,6 +15,8 @@ import { AppLogStorage, AppStorage, IAppStorageItem } from './storage';
14
15
15
16
import { AppStatus , AppStatusUtils } from '../definition/AppStatus' ;
16
17
import { AppMethod } from '../definition/metadata' ;
18
+ import { InvalidLicenseError } from './errors' ;
19
+ import { IMarketplaceInfo } from './marketplace' ;
17
20
18
21
export class AppManager {
19
22
public static Instance : AppManager ;
@@ -31,6 +34,7 @@ export class AppManager {
31
34
private readonly commandManager : AppSlashCommandManager ;
32
35
private readonly apiManager : AppApiManager ;
33
36
private readonly settingsManager : AppSettingsManager ;
37
+ private readonly licenseManager : AppLicenseManager ;
34
38
35
39
private isLoaded : boolean ;
36
40
@@ -67,6 +71,7 @@ export class AppManager {
67
71
this . commandManager = new AppSlashCommandManager ( this ) ;
68
72
this . apiManager = new AppApiManager ( this ) ;
69
73
this . settingsManager = new AppSettingsManager ( this ) ;
74
+ this . licenseManager = new AppLicenseManager ( this ) ;
70
75
71
76
this . isLoaded = false ;
72
77
AppManager . Instance = this ;
@@ -112,6 +117,10 @@ export class AppManager {
112
117
return this . commandManager ;
113
118
}
114
119
120
+ public getLicenseManager ( ) : AppLicenseManager {
121
+ return this . licenseManager ;
122
+ }
123
+
115
124
/** Gets the api manager's instance. */
116
125
public getApiManager ( ) : AppApiManager {
117
126
return this . apiManager ;
@@ -222,7 +231,9 @@ export class AppManager {
222
231
for ( const rl of this . apps . values ( ) ) {
223
232
if ( AppStatusUtils . isDisabled ( rl . getStatus ( ) ) ) {
224
233
continue ;
225
- } else if ( rl . getStatus ( ) === AppStatus . INITIALIZED ) {
234
+ }
235
+
236
+ if ( rl . getStatus ( ) === AppStatus . INITIALIZED ) {
226
237
this . listenerManager . unregisterListeners ( rl ) ;
227
238
this . commandManager . unregisterCommands ( rl . getID ( ) ) ;
228
239
this . apiManager . unregisterApis ( rl . getID ( ) ) ;
@@ -361,7 +372,7 @@ export class AppManager {
361
372
return true ;
362
373
}
363
374
364
- public async add ( zipContentsBase64d : string , enable = true ) : Promise < AppFabricationFulfillment > {
375
+ public async add ( zipContentsBase64d : string , enable = true , marketplaceInfo ?: IMarketplaceInfo ) : Promise < AppFabricationFulfillment > {
365
376
const aff = new AppFabricationFulfillment ( ) ;
366
377
const result = await this . getParser ( ) . parseZip ( this . getCompiler ( ) , zipContentsBase64d ) ;
367
378
@@ -382,10 +393,13 @@ export class AppManager {
382
393
languageContent : result . languageContent ,
383
394
settings : { } ,
384
395
implemented : result . implemented . getValues ( ) ,
396
+ marketplaceInfo,
385
397
} ) ;
386
398
387
399
if ( ! created ) {
388
- throw new Error ( 'Failed to create the App, the storage did not return it.' ) ;
400
+ aff . setStorageError ( 'Failed to create the App, the storage did not return it.' ) ;
401
+
402
+ return aff ;
389
403
}
390
404
391
405
// Now that is has all been compiled, let's get the
@@ -548,6 +562,51 @@ export class AppManager {
548
562
return rl ;
549
563
}
550
564
565
+ public async updateAppsMarketplaceInfo ( appsOverview : Array < { latest : IMarketplaceInfo } > ) : Promise < void > {
566
+ try {
567
+ appsOverview . forEach ( ( { latest : appInfo } ) => {
568
+ if ( ! appInfo . subscriptionInfo ) {
569
+ return ;
570
+ }
571
+
572
+ const app = this . apps . get ( appInfo . id ) ;
573
+
574
+ if ( ! app ) {
575
+ return ;
576
+ }
577
+
578
+ const appStorageItem = app . getStorageItem ( ) ;
579
+ const subscriptionInfo = appStorageItem . marketplaceInfo && appStorageItem . marketplaceInfo . subscriptionInfo ;
580
+
581
+ if ( subscriptionInfo && subscriptionInfo . startDate === appInfo . subscriptionInfo . startDate ) {
582
+ return ;
583
+ }
584
+
585
+ appStorageItem . marketplaceInfo . subscriptionInfo = appInfo . subscriptionInfo ;
586
+
587
+ this . storage . update ( appStorageItem ) . catch ( console . error ) ; // TODO: Figure out something better
588
+ } ) ;
589
+ } catch ( err ) {
590
+ // Errors here are not important
591
+ }
592
+
593
+ const queue = [ ] as Array < Promise < void > > ;
594
+
595
+ this . apps . forEach ( ( app ) => queue . push ( app . validateLicense ( ) . catch ( ( error ) => {
596
+ if ( ! ( error instanceof InvalidLicenseError ) ) {
597
+ console . error ( error ) ;
598
+ return ;
599
+ }
600
+
601
+ this . commandManager . unregisterCommands ( app . getID ( ) ) ;
602
+ this . apiManager . unregisterApis ( app . getID ( ) ) ;
603
+
604
+ return app . setStatus ( AppStatus . INVALID_LICENSE_DISABLED ) ;
605
+ } ) ) ) ;
606
+
607
+ await Promise . all ( queue ) ;
608
+ }
609
+
551
610
/**
552
611
* Goes through the entire loading up process. WARNING: Do not use. ;)
553
612
*
@@ -603,20 +662,29 @@ export class AppManager {
603
662
const envRead = this . getAccessorManager ( ) . getEnvironmentRead ( storageItem . id ) ;
604
663
605
664
try {
665
+ await app . validateLicense ( ) ;
666
+
606
667
await app . call ( AppMethod . INITIALIZE , configExtend , envRead ) ;
607
- result = true ;
608
668
await app . setStatus ( AppStatus . INITIALIZED , silenceStatus ) ;
669
+
670
+ result = true ;
609
671
} catch ( e ) {
672
+ let status = AppStatus . ERROR_DISABLED ;
673
+
610
674
if ( e . name === 'NotEnoughMethodArgumentsError' ) {
611
675
console . warn ( 'Please report the following error:' ) ;
612
676
}
613
677
678
+ if ( e instanceof InvalidLicenseError ) {
679
+ status = AppStatus . INVALID_LICENSE_DISABLED ;
680
+ }
681
+
614
682
console . error ( e ) ;
615
683
this . commandManager . unregisterCommands ( storageItem . id ) ;
616
684
this . apiManager . unregisterApis ( storageItem . id ) ;
617
685
result = false ;
618
686
619
- await app . setStatus ( AppStatus . ERROR_DISABLED , silenceStatus ) ;
687
+ await app . setStatus ( status , silenceStatus ) ;
620
688
}
621
689
622
690
if ( saveToDb ) {
@@ -657,19 +725,27 @@ export class AppManager {
657
725
let enable : boolean ;
658
726
659
727
try {
728
+ await app . validateLicense ( ) ;
729
+
660
730
enable = await app . call ( AppMethod . ONENABLE ,
661
731
this . getAccessorManager ( ) . getEnvironmentRead ( storageItem . id ) ,
662
732
this . getAccessorManager ( ) . getConfigurationModify ( storageItem . id ) ) as boolean ;
733
+
663
734
await app . setStatus ( isManual ? AppStatus . MANUALLY_ENABLED : AppStatus . AUTO_ENABLED , silenceStatus ) ;
664
735
} catch ( e ) {
665
736
enable = false ;
737
+ let status = AppStatus . ERROR_DISABLED ;
666
738
667
739
if ( e . name === 'NotEnoughMethodArgumentsError' ) {
668
740
console . warn ( 'Please report the following error:' ) ;
669
741
}
670
742
743
+ if ( e instanceof InvalidLicenseError ) {
744
+ status = AppStatus . INVALID_LICENSE_DISABLED ;
745
+ }
746
+
671
747
console . error ( e ) ;
672
- await app . setStatus ( AppStatus . ERROR_DISABLED , silenceStatus ) ;
748
+ await app . setStatus ( status , silenceStatus ) ;
673
749
}
674
750
675
751
if ( enable ) {
0 commit comments