Skip to content
This repository was archived by the owner on Jul 6, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions packages/auth/src/Domain/Subscription/Subscription.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { SubscriptionName } from './SubscriptionName'

export type Subscription = {
planName: SubscriptionName,
endsAt: number,
createdAt: number,
updatedAt: number,
cancelled: boolean,
}
1 change: 1 addition & 0 deletions packages/auth/src/Domain/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './Role/Role'
export * from './Role/RoleName'
export * from './Subscription/Subscription'
export * from './Subscription/SubscriptionName'
export * from './Token/Token'
export * from './User/KeyParams'
Expand Down
25 changes: 24 additions & 1 deletion packages/snjs/lib/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ import { ProtocolVersion, compareVersions } from './protocol/versions';
import { KeyParamsOrigination } from './protocol/key_params';
import { SNLog } from './log';
import { SNPreferencesService } from './services/preferences_service';
import { HttpResponse, SignInResponse, User } from './services/api/responses';
import { AvailableSubscriptions, GetAvailableSubscriptionsResponse, GetSubscriptionResponse, HttpResponse, SignInResponse, User } from './services/api/responses';
import { PayloadFormat } from './protocol/payloads';
import { ProtectionEvent } from './services/protection_service';
import { RemoteSession } from '.';
Expand All @@ -106,6 +106,7 @@ import { SettingName } from '@standardnotes/settings';
import { SNSettingsService } from './services/settings_service';
import { SNMfaService } from './services/mfa_service';
import { SensitiveSettingName } from './services/settings_service/SensitiveSettingName';
import { Subscription } from '@standardnotes/auth';
import { FeatureDescription, FeatureIdentifier } from '@standardnotes/features';

/** How often to automatically sync, in milliseconds */
Expand Down Expand Up @@ -559,6 +560,28 @@ export class SNApplication {
return compareVersions(userVersion, ProtocolVersion.V004) >= 0;
}

public async getUserSubscription(): Promise<Subscription | undefined> {
const response = await this.sessionManager.getSubscription();
if (response.error) {
throw new Error(response.error.message)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm why are we throwing? We should propagate this to the client so the user can take action. Otherwise this might block execution of other essential tasks.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When throwing an error in an async function it triggers the catch/error of the Promise. That's how it's propagated towards the client.

}
if (response.data) {
return (response as GetSubscriptionResponse).data!.subscription
}
return undefined;
}

public async getAvailableSubscriptions(): Promise<AvailableSubscriptions | undefined> {
const response = await this.apiService.getAvailableSubscriptions();
if (response.error) {
throw new Error(response.error.message)
}
if (response.data) {
return (response as GetAvailableSubscriptionsResponse).data!
}
return undefined;
}

/**
* @param isUserModified Whether to change the modified date the user
* sees of the item.
Expand Down
39 changes: 37 additions & 2 deletions packages/snjs/lib/services/api/api_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import {
GetSettingResponse,
DeleteSettingResponse,
MinimalHttpResponse,
GetSubscriptionResponse,
GetAvailableSubscriptionsResponse,
ChangeCredentialsResponse,
} from './responses';
import { Session, TokenSession } from './session';
Expand Down Expand Up @@ -57,10 +59,16 @@ type PathNamesV1 = {
userFeatures: (userUuid: string) => string;
settings: (userUuid: string) => string;
setting: (userUuid: string, settingName: string) => string;
subscription: (userUuid: string) => string;
};

type PathNamesV2 = {
subscriptions: string;
}

const Paths: {
v1: PathNamesV1;
v2: PathNamesV2;
} = {
v1: {
keyParams: '/v1/login-params',
Expand All @@ -79,6 +87,10 @@ const Paths: {
settings: (userUuid) => `/v1/users/${userUuid}/settings`,
setting: (userUuid, settingName) =>
`/v1/users/${userUuid}/settings/${settingName}`,
subscription: (userUuid) => `/v2/users/${userUuid}/subscription`,
},
v2: {
subscriptions: '/v2/subscriptions',
},
};

Expand Down Expand Up @@ -665,7 +677,7 @@ export class SNApiService extends PureService<
verb: HttpVerb.Put,
url: joinPaths(this.host, Paths.v1.settings(userUuid)),
authentication: this.session?.authorizationValue,
fallbackErrorMessage: messages.API_MESSAGE_FAILED_GET_SETTINGS,
fallbackErrorMessage: messages.API_MESSAGE_FAILED_UPDATE_SETTINGS,
params,
});
}
Expand All @@ -690,7 +702,7 @@ export class SNApiService extends PureService<
verb: HttpVerb.Delete,
url: joinPaths(this.host, Paths.v1.setting(userUuid, settingName)),
authentication: this.session?.authorizationValue,
fallbackErrorMessage: messages.API_MESSAGE_FAILED_GET_SETTINGS,
fallbackErrorMessage: messages.API_MESSAGE_FAILED_UPDATE_SETTINGS,
});
}

Expand All @@ -702,6 +714,29 @@ export class SNApiService extends PureService<
});
}

public async getSubscription(
userUuid: string
): Promise<HttpResponse | GetSubscriptionResponse> {
const url = joinPaths(this.host, Paths.v1.subscription(userUuid));
const response = await this.request({
verb: HttpVerb.Get,
url,
authentication: this.session?.authorizationValue,
fallbackErrorMessage: messages.API_MESSAGE_FAILED_SUBSCRIPTION_INFO,
});
return response;
}

public async getAvailableSubscriptions(): Promise<HttpResponse | GetAvailableSubscriptionsResponse> {
const url = joinPaths(this.host, Paths.v2.subscriptions);
const response = await this.request({
verb: HttpVerb.Get,
url,
fallbackErrorMessage: messages.API_MESSAGE_FAILED_SUBSCRIPTION_INFO,
});
return response;
}

private preprocessingError() {
if (this.refreshingSession) {
return this.createErrorResponse(
Expand Down
2 changes: 2 additions & 0 deletions packages/snjs/lib/services/api/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ export const API_MESSAGE_INVALID_SESSION =
export const API_MESSAGE_FAILED_GET_SETTINGS = 'Failed to get settings.';
export const API_MESSAGE_FAILED_UPDATE_SETTINGS = 'Failed to update settings.';

export const API_MESSAGE_FAILED_SUBSCRIPTION_INFO = 'Failed to get subscription\'s information.';

export const UNSUPPORTED_PROTOCOL_VERSION =
'This version of the application does not support your newer account type. Please upgrade to the latest version of Standard Notes to sign in.';

Expand Down
23 changes: 22 additions & 1 deletion packages/snjs/lib/services/api/responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
KeyParamsOrigination,
} from './../../protocol/key_params';
import { ProtocolVersion } from './../../protocol/versions';
import { Role } from '@standardnotes/auth';
import { Role, Subscription, SubscriptionName } from '@standardnotes/auth';
import { FeatureDescription } from '@standardnotes/features';
import { UuidString } from '@Lib/types';

Expand Down Expand Up @@ -232,3 +232,24 @@ export type GetSettingResponse = MinimalHttpResponse & {
};
export type UpdateSettingResponse = MinimalHttpResponse;
export type DeleteSettingResponse = MinimalHttpResponse;

export type GetSubscriptionResponse = MinimalHttpResponse & {
data?: {
subscription?: Subscription
}
}

export type AvailableSubscriptions = {
[key in SubscriptionName]: {
name: string;
pricing: {
price: number;
period: string;
}[];
features: FeatureDescription[];
}
};

export type GetAvailableSubscriptionsResponse = MinimalHttpResponse & {
data?: AvailableSubscriptions;
}
5 changes: 5 additions & 0 deletions packages/snjs/lib/services/api/session_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
import { ChallengeService } from './../challenge/challenge_service';
import { JwtSession, RemoteSession, TokenSession } from './session';
import {
GetSubscriptionResponse,
ChangeCredentialsResponse,
HttpResponse,
KeyParamsResponse,
Expand Down Expand Up @@ -214,6 +215,10 @@ export class SNSessionManager extends PureService<SessionEvent> {
});
}

public getSubscription(): Promise<HttpResponse | GetSubscriptionResponse> {
return this.apiService.getSubscription(this.user!.uuid)
}

private async promptForMfaValue() {
const challenge = new Challenge(
[
Expand Down
2 changes: 1 addition & 1 deletion packages/snjs/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@standardnotes/snjs",
"version": "2.13.0",
"version": "2.13.1",
"engines": {
"node": ">=14.0.0 <16.0.0"
},
Expand Down