diff --git a/packages/services/src/Domain/Application/ApplicationInterface.ts b/packages/services/src/Domain/Application/ApplicationInterface.ts index bcd7c4bc2..35b83e41b 100644 --- a/packages/services/src/Domain/Application/ApplicationInterface.ts +++ b/packages/services/src/Domain/Application/ApplicationInterface.ts @@ -30,6 +30,7 @@ import { DeinitMode } from './DeinitMode' import { DeinitSource } from './DeinitSource' import { UserClientInterface } from '../User/UserClientInterface' import { SessionsClientInterface } from '../Session/SessionsClientInterface' +import { User } from '@standardnotes/responses' export interface ApplicationInterface { deinit(mode: DeinitMode, source: DeinitSource): void @@ -57,6 +58,8 @@ export interface ApplicationInterface { contentType: ContentType | ContentType[], stream: ItemStream, ): () => void + + getUser(): User | undefined hasAccount(): boolean importData(data: BackupFile, awaitSync?: boolean): Promise diff --git a/packages/web/src/javascripts/Application/DevMode.ts b/packages/web/src/javascripts/Application/DevMode.ts index f2387b5b6..8b1e2f0a6 100644 --- a/packages/web/src/javascripts/Application/DevMode.ts +++ b/packages/web/src/javascripts/Application/DevMode.ts @@ -1,8 +1,43 @@ import { InternalFeature, InternalFeatureService } from '@standardnotes/snjs' import { WebApplicationInterface } from '@standardnotes/ui-services' -export class DevModeHook { - load(_application: WebApplicationInterface) { +export class DevMode { + constructor(private application: WebApplicationInterface) { InternalFeatureService.get().enableFeature(InternalFeature.Vaults) } + + /** Valid only when running a mock event publisher on port 3124 */ + async purchaseMockSubscription() { + const subscriptionId = 2000 + const email = this.application.getUser()?.email + const response = await fetch('http://localhost:3124/events', { + method: 'POST', + headers: { + Accept: 'application/json', + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + eventType: 'SUBSCRIPTION_PURCHASED', + eventPayload: { + userEmail: email, + subscriptionId: subscriptionId, + subscriptionName: 'PRO_PLAN', + subscriptionExpiresAt: (new Date().getTime() + 3_600_000) * 1_000, + timestamp: Date.now(), + offline: false, + discountCode: null, + limitedDiscountPurchased: false, + newSubscriber: true, + totalActiveSubscriptionsCount: 1, + userRegisteredAt: 1, + billingFrequency: 12, + payAmount: 59.0, + }, + }), + }) + + if (!response.ok) { + console.error(`Failed to publish mocked event: ${response.status} ${response.statusText}`) + } + } } diff --git a/packages/web/src/javascripts/Application/WebApplication.ts b/packages/web/src/javascripts/Application/WebApplication.ts index d582c9914..2a9d7e5e4 100644 --- a/packages/web/src/javascripts/Application/WebApplication.ts +++ b/packages/web/src/javascripts/Application/WebApplication.ts @@ -51,19 +51,21 @@ import { FeatureName } from '@/Controllers/FeatureName' import { ItemGroupController } from '@/Components/NoteView/Controller/ItemGroupController' import { VisibilityObserver } from './VisibilityObserver' import { MomentsService } from '@/Controllers/Moments/MomentsService' -import { purchaseMockSubscription } from '@/Utils/Dev/PurchaseMockSubscription' -import { DevModeHook } from './DevMode' +import { DevMode } from './DevMode' export type WebEventObserver = (event: WebAppEvent, data?: unknown) => void export class WebApplication extends SNApplication implements WebApplicationInterface { - private webServices!: WebServices - private webEventObservers: WebEventObserver[] = [] - public itemControllerGroup: ItemGroupController - private mobileWebReceiver?: MobileWebReceiver - private androidBackHandler?: AndroidBackHandler + public readonly itemControllerGroup: ItemGroupController public readonly routeService: RouteServiceInterface - private visibilityObserver?: VisibilityObserver + + private readonly webServices!: WebServices + private readonly webEventObservers: WebEventObserver[] = [] + private readonly mobileWebReceiver?: MobileWebReceiver + private readonly androidBackHandler?: AndroidBackHandler + private readonly visibilityObserver?: VisibilityObserver + + public readonly devMode?: DevMode constructor( deviceInterface: WebOrDesktopDevice, @@ -93,7 +95,7 @@ export class WebApplication extends SNApplication implements WebApplicationInter }) if (isDev) { - new DevModeHook().load(this) + this.devMode = new DevMode(this) } makeObservable(this, { @@ -157,7 +159,7 @@ export class WebApplication extends SNApplication implements WebApplicationInter ;(service as { application?: WebApplication }).application = undefined } - this.webServices = {} as WebServices + ;(this.webServices as unknown) = undefined this.itemControllerGroup.deinit() ;(this.itemControllerGroup as unknown) = undefined @@ -170,7 +172,7 @@ export class WebApplication extends SNApplication implements WebApplicationInter if (this.visibilityObserver) { this.visibilityObserver.deinit() - this.visibilityObserver = undefined + ;(this.visibilityObserver as unknown) = undefined } } catch (error) { console.error('Error while deiniting application', error) @@ -463,12 +465,4 @@ export class WebApplication extends SNApplication implements WebApplicationInter generateUUID(): string { return this.options.crypto.generateUUID() } - - dev__purchaseMockSubscription() { - if (!isDev) { - throw new Error('This method is only available in dev mode') - } - - void purchaseMockSubscription(this.getUser()?.email as string, 2000) - } } diff --git a/packages/web/src/javascripts/Utils/Dev/PurchaseMockSubscription.ts b/packages/web/src/javascripts/Utils/Dev/PurchaseMockSubscription.ts deleted file mode 100644 index a1ce59efb..000000000 --- a/packages/web/src/javascripts/Utils/Dev/PurchaseMockSubscription.ts +++ /dev/null @@ -1,32 +0,0 @@ -/** Valid only when running a mock event publisher on port 3124 */ -export async function purchaseMockSubscription(email: string, subscriptionId: number) { - const response = await fetch('http://localhost:3124/events', { - method: 'POST', - headers: { - Accept: 'application/json', - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ - eventType: 'SUBSCRIPTION_PURCHASED', - eventPayload: { - userEmail: email, - subscriptionId: subscriptionId, - subscriptionName: 'PRO_PLAN', - subscriptionExpiresAt: (new Date().getTime() + 3_600_000) * 1_000, - timestamp: Date.now(), - offline: false, - discountCode: null, - limitedDiscountPurchased: false, - newSubscriber: true, - totalActiveSubscriptionsCount: 1, - userRegisteredAt: 1, - billingFrequency: 12, - payAmount: 59.0, - }, - }), - }) - - if (!response.ok) { - console.error(`Failed to publish mocked event: ${response.status} ${response.statusText}`) - } -}