Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: refetch subscription when prefs menu opens #2360

Merged
merged 3 commits into from
Jul 17, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
"@standardnotes/domain-core": "^1.22.0",
"@standardnotes/models": "workspace:*",
"@standardnotes/responses": "workspace:*",
"@standardnotes/security": "^1.7.6",
"@standardnotes/utils": "workspace:*",
"reflect-metadata": "^0.1.13"
}
Expand Down
23 changes: 18 additions & 5 deletions packages/api/src/Domain/Http/FetchRequestHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,26 @@ export class FetchRequestHandler implements RequestHandlerInterface {
}

private async runRequest<T>(request: Request, body?: string | Uint8Array | undefined): Promise<HttpResponse<T>> {
const fetchResponse = await fetch(request, {
body,
})
try {
const fetchResponse = await fetch(request, {
body,
})

const response = await this.handleFetchResponse<T>(fetchResponse)
const response = await this.handleFetchResponse<T>(fetchResponse)

return response
return response
} catch (error) {
return {
status: HttpStatusCode.InternalServerError,
headers: new Map<string, string | null>(),
data: {
error: {
message:
'message' in (error as { message: string }) ? (error as { message: string }).message : 'Unknown error',
},
},
}
}
}

private async handleFetchResponse<T>(fetchResponse: Response): Promise<HttpResponse<T>> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { ValetTokenOperation } from '@standardnotes/responses'
import { SharedVaultMoveType } from './SharedVaultMoveType'
import { SharedVaultMoveType, ValetTokenOperation } from '@standardnotes/responses'

export type CreateSharedVaultValetTokenParams = {
sharedVaultUuid: string
Expand Down
1 change: 0 additions & 1 deletion packages/api/src/Domain/Request/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,3 @@ export * from './User/UserRegistrationRequestParams'
export * from './User/UserUpdateRequestParams'
export * from './UserRequest/UserRequestRequestParams'
export * from './WebSocket/WebSocketConnectionTokenRequestParams'
export * from './SharedVault/SharedVaultMoveType'
1 change: 0 additions & 1 deletion packages/features/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
"dependencies": {
"@standardnotes/common": "^1.50.0",
"@standardnotes/domain-core": "^1.22.0",
"@standardnotes/security": "^1.7.6",
"reflect-metadata": "^0.1.13"
},
"devDependencies": {
Expand Down
1 change: 0 additions & 1 deletion packages/responses/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
"dependencies": {
"@standardnotes/common": "^1.50.0",
"@standardnotes/features": "workspace:*",
"@standardnotes/security": "^1.7.6",
"reflect-metadata": "^0.1.13"
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ValetTokenOperation } from './ValetTokenOperation'
import { ValetTokenOperation } from '../Temp/ValetTokenOperation'

export type CreateValetTokenPayload = {
operation: ValetTokenOperation
Expand Down
1 change: 0 additions & 1 deletion packages/responses/src/Domain/Files/ValetTokenOperation.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Role } from '@standardnotes/security'
import { Role } from '../Temp/Role'

export type DeprecatedResponseMeta = {
auth: {
Expand Down
2 changes: 1 addition & 1 deletion packages/responses/src/Domain/Http/HttpResponseMeta.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Role } from '@standardnotes/security'
import { Role } from '../Temp/Role'

export type HttpResponseMeta = {
auth: {
Expand Down
4 changes: 4 additions & 0 deletions packages/responses/src/Domain/Temp/Role.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export type Role = {
uuid: string
name: string
}
7 changes: 7 additions & 0 deletions packages/responses/src/Domain/Temp/Subscription.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export type Subscription = {
planName: string
endsAt: number
createdAt: number
updatedAt: number
cancelled: boolean
}
6 changes: 6 additions & 0 deletions packages/responses/src/Domain/Temp/ValetTokenOperation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export enum ValetTokenOperation {
Read = 'read',
Write = 'write',
Delete = 'delete',
Move = 'move',
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Subscription } from '@standardnotes/security'
import { Subscription } from '../Temp/Subscription'

export type GetSubscriptionResponse = {
subscription?: Subscription
Expand Down
7 changes: 6 additions & 1 deletion packages/responses/src/Domain/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ export * from './Auth/SignInResponse'
export * from './Auth/SignOutResponse'
export * from './Auth/User'

/** Temps are awaiting final publish state on server repo */
export * from './Temp/SharedVaultMoveType'
export * from './Temp/Subscription'
export * from './Temp/ValetTokenOperation'
export * from './Temp/Role'

export * from './Error/ClientDisplayableError'

export * from './Files/CloseUploadSessionResponse'
Expand All @@ -24,7 +30,6 @@ export * from './Files/DownloadFileChunkResponse'
export * from './Files/StartUploadSessionResponse'
export * from './Files/UploadFileChunkResponse'
export * from './Files/MoveFileResponse'
export * from './Files/ValetTokenOperation'

export * from './Http'

Expand Down
1 change: 0 additions & 1 deletion packages/services/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
"@standardnotes/files": "workspace:^",
"@standardnotes/models": "workspace:^",
"@standardnotes/responses": "workspace:*",
"@standardnotes/security": "^1.7.5",
"@standardnotes/sncrypto-common": "workspace:^",
"@standardnotes/utils": "workspace:*",
"reflect-metadata": "^0.1.13"
Expand Down
2 changes: 1 addition & 1 deletion packages/services/src/Domain/Api/MetaReceivedData.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Role } from '@standardnotes/security'
import { Role } from '@standardnotes/responses'

export type MetaReceivedData = {
userUuid: string
Expand Down
4 changes: 2 additions & 2 deletions packages/services/src/Domain/Backups/BackupService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
isNote,
NoteContent,
} from '@standardnotes/models'
import { ClientDisplayableError } from '@standardnotes/responses'
import { ClientDisplayableError, ValetTokenOperation } from '@standardnotes/responses'
import {
FilesApiInterface,
FileBackupMetadataFile,
Expand Down Expand Up @@ -520,7 +520,7 @@ export class FilesBackupService extends AbstractService implements BackupService
},
})

const token = await this.api.createUserFileValetToken(file.remoteIdentifier, 'read')
const token = await this.api.createUserFileValetToken(file.remoteIdentifier, ValetTokenOperation.Read)

if (token instanceof ClientDisplayableError) {
this.status.removeMessage(messageId)
Expand Down
28 changes: 12 additions & 16 deletions packages/services/src/Domain/Files/FileService.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { MutatorClientInterface } from './../Mutator/MutatorClientInterface'
import {
ClientDisplayableError,
ValetTokenOperation,
isClientDisplayableError,
isErrorResponse,
SharedVaultMoveType,
ValetTokenOperation,
} from '@standardnotes/responses'
import {
FileItem,
Expand Down Expand Up @@ -47,12 +48,7 @@ import { AbstractService } from '../Service/AbstractService'
import { SyncServiceInterface } from '../Sync/SyncServiceInterface'
import { DecryptItemsKeyWithUserFallback } from '../Encryption/Functions'
import { log, LoggingDomain } from '../Logging'
import {
SharedVaultMoveType,
SharedVaultServer,
SharedVaultServerInterface,
HttpServiceInterface,
} from '@standardnotes/api'
import { SharedVaultServer, SharedVaultServerInterface, HttpServiceInterface } from '@standardnotes/api'
import { ContentType } from '@standardnotes/domain-core'

const OneHundredMb = 100 * 1_000_000
Expand Down Expand Up @@ -111,7 +107,7 @@ export class FileService extends AbstractService implements FilesClientInterface
moveOperationType?: SharedVaultMoveType
sharedVaultToSharedVaultMoveTargetUuid?: string
}): Promise<string | ClientDisplayableError> {
if (params.operation !== 'write' && !params.fileUuidRequiredForExistingFiles) {
if (params.operation !== ValetTokenOperation.Write && !params.fileUuidRequiredForExistingFiles) {
throw new Error('File UUID is required for for non-write operations')
}

Expand Down Expand Up @@ -139,7 +135,7 @@ export class FileService extends AbstractService implements FilesClientInterface
const valetTokenResult = await this.createSharedVaultValetToken({
sharedVaultUuid: file.shared_vault_uuid ? file.shared_vault_uuid : sharedVault.sharing.sharedVaultUuid,
remoteIdentifier: file.remoteIdentifier,
operation: 'move',
operation: ValetTokenOperation.Move,
fileUuidRequiredForExistingFiles: file.uuid,
moveOperationType: file.shared_vault_uuid ? 'shared-vault-to-shared-vault' : 'user-to-shared-vault',
sharedVaultToSharedVaultMoveTargetUuid: file.shared_vault_uuid ? sharedVault.sharing.sharedVaultUuid : undefined,
Expand All @@ -164,7 +160,7 @@ export class FileService extends AbstractService implements FilesClientInterface
const valetTokenResult = await this.createSharedVaultValetToken({
sharedVaultUuid: file.shared_vault_uuid,
remoteIdentifier: file.remoteIdentifier,
operation: 'move',
operation: ValetTokenOperation.Move,
fileUuidRequiredForExistingFiles: file.uuid,
moveOperationType: 'shared-vault-to-user',
})
Expand All @@ -190,10 +186,10 @@ export class FileService extends AbstractService implements FilesClientInterface
? await this.createSharedVaultValetToken({
sharedVaultUuid: vault.sharing.sharedVaultUuid,
remoteIdentifier,
operation: 'write',
operation: ValetTokenOperation.Write,
unencryptedFileSizeForUpload: sizeInBytes,
})
: await this.createUserValetToken(remoteIdentifier, 'write', sizeInBytes)
: await this.createUserValetToken(remoteIdentifier, ValetTokenOperation.Write, sizeInBytes)

if (valetTokenResult instanceof ClientDisplayableError) {
return valetTokenResult
Expand Down Expand Up @@ -342,10 +338,10 @@ export class FileService extends AbstractService implements FilesClientInterface
? await this.createSharedVaultValetToken({
sharedVaultUuid: file.shared_vault_uuid,
remoteIdentifier: file.remoteIdentifier,
operation: 'read',
operation: ValetTokenOperation.Read,
fileUuidRequiredForExistingFiles: file.uuid,
})
: await this.createUserValetToken(file.remoteIdentifier, 'read')
: await this.createUserValetToken(file.remoteIdentifier, ValetTokenOperation.Read)

if (tokenResult instanceof ClientDisplayableError) {
return tokenResult
Expand Down Expand Up @@ -375,10 +371,10 @@ export class FileService extends AbstractService implements FilesClientInterface
? await this.createSharedVaultValetToken({
sharedVaultUuid: file.shared_vault_uuid,
remoteIdentifier: file.remoteIdentifier,
operation: 'delete',
operation: ValetTokenOperation.Delete,
fileUuidRequiredForExistingFiles: file.uuid,
})
: await this.createUserValetToken(file.remoteIdentifier, 'delete')
: await this.createUserValetToken(file.remoteIdentifier, ValetTokenOperation.Delete)

if (tokenResult instanceof ClientDisplayableError) {
return tokenResult
Expand Down
10 changes: 7 additions & 3 deletions packages/services/src/Domain/Subscription/SubscriptionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@ import { InternalEventBusInterface } from '../Internal/InternalEventBusInterface
import { AbstractService } from '../Service/AbstractService'
import { SubscriptionManagerInterface } from './SubscriptionManagerInterface'
import { AppleIAPReceipt } from './AppleIAPReceipt'
import { AvailableSubscriptions, getErrorFromErrorResponse, isErrorResponse } from '@standardnotes/responses'
import { Subscription } from '@standardnotes/security'
import {
AvailableSubscriptions,
getErrorFromErrorResponse,
isErrorResponse,
Subscription,
} from '@standardnotes/responses'
import { SubscriptionManagerEvent } from './SubscriptionManagerEvent'

export class SubscriptionManager
Expand Down Expand Up @@ -170,7 +174,7 @@ export class SubscriptionManager
}
}

private async fetchOnlineSubscription(): Promise<void> {
public async fetchOnlineSubscription(): Promise<void> {
if (!this.sessions.isSignedIn()) {
return
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { ApplicationServiceInterface } from './../Service/ApplicationServiceInterface'
import { Invitation } from '@standardnotes/models'
import { AppleIAPReceipt } from './AppleIAPReceipt'
import { AvailableSubscriptions } from '@standardnotes/responses'
import { Subscription } from '@standardnotes/security'
import { AvailableSubscriptions, Subscription } from '@standardnotes/responses'
import { SubscriptionManagerEvent } from './SubscriptionManagerEvent'

export interface SubscriptionManagerInterface extends ApplicationServiceInterface<SubscriptionManagerEvent, unknown> {
Expand All @@ -15,6 +14,7 @@ export interface SubscriptionManagerInterface extends ApplicationServiceInterfac
get isUserSubscriptionExpired(): boolean
get isUserSubscriptionCanceled(): boolean

fetchOnlineSubscription(): Promise<void>
listSubscriptionInvitations(): Promise<Invitation[]>
inviteToSubscription(inviteeEmail: string): Promise<boolean>
cancelInvitation(inviteUuid: string): Promise<boolean>
Expand Down
4 changes: 1 addition & 3 deletions packages/snjs/lib/Services/Api/ApiService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,13 @@ import {
HttpErrorResponse,
HttpSuccessResponse,
isErrorResponse,
ValetTokenOperation,
MoveFileResponse,
ValetTokenOperation,
} from '@standardnotes/responses'
import { LegacySession, MapperInterface, Session, SessionToken } from '@standardnotes/domain-core'
import { HttpServiceInterface } from '@standardnotes/api'
import { SNRootKeyParams } from '@standardnotes/encryption'

import { PureCryptoInterface } from '@standardnotes/sncrypto-common'

import { isUrlFirstParty, TRUSTED_FEATURE_HOSTS } from '@Lib/Hosts'
import { Paths } from './Paths'
import { DiskStorageService } from '../Storage/DiskStorageService'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jest.mock('@standardnotes/features', () => ({
}))

import { FindNativeFeature } from '@standardnotes/features'
import { Subscription } from '@standardnotes/security'
import { Subscription } from '@standardnotes/responses'

describe('GetFeatureStatusUseCase', () => {
let items: jest.Mocked<ItemManagerInterface>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AnyFeatureDescription, FeatureIdentifier, FindNativeFeature } from '@standardnotes/features'
import { DecryptedItemInterface } from '@standardnotes/models'
import { Subscription } from '@standardnotes/security'
import { Subscription } from '@standardnotes/responses'
import { FeatureStatus, ItemManagerInterface } from '@standardnotes/services'
import { convertTimestampToMilliseconds } from '@standardnotes/utils'

Expand Down
22 changes: 17 additions & 5 deletions packages/snjs/mocha/files.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe('files', function () {
await setup({ fakeCrypto: true, subscription: true })

const remoteIdentifier = Utils.generateUuid()
const token = await application.apiService.createUserFileValetToken(remoteIdentifier, 'write')
const token = await application.apiService.createUserFileValetToken(remoteIdentifier, ValetTokenOperation.Write)

expect(token.length).to.be.above(0)
})
Expand All @@ -60,7 +60,10 @@ describe('files', function () {
await setup({ fakeCrypto: true, subscription: false })

const remoteIdentifier = Utils.generateUuid()
const tokenOrError = await application.apiService.createUserFileValetToken(remoteIdentifier, 'write')
const tokenOrError = await application.apiService.createUserFileValetToken(
remoteIdentifier,
ValetTokenOperation.Write,
)

expect(isClientDisplayableError(tokenOrError)).to.equal(true)
})
Expand All @@ -76,20 +79,29 @@ describe('files', function () {
})

const remoteIdentifier = Utils.generateUuid()
const tokenOrError = await application.apiService.createUserFileValetToken(remoteIdentifier, 'write')
const tokenOrError = await application.apiService.createUserFileValetToken(
remoteIdentifier,
ValetTokenOperation.Write,
)

expect(isClientDisplayableError(tokenOrError)).to.equal(true)
})

it('creating two upload sessions successively should succeed', async function () {
await setup({ fakeCrypto: true, subscription: true })

const firstToken = await application.apiService.createUserFileValetToken(Utils.generateUuid(), 'write')
const firstToken = await application.apiService.createUserFileValetToken(
Utils.generateUuid(),
ValetTokenOperation.Write,
)
const firstSession = await application.apiService.startUploadSession(firstToken, 'user')

expect(firstSession.uploadId).to.be.ok

const secondToken = await application.apiService.createUserFileValetToken(Utils.generateUuid(), 'write')
const secondToken = await application.apiService.createUserFileValetToken(
Utils.generateUuid(),
ValetTokenOperation.Write,
)
const secondSession = await application.apiService.startUploadSession(secondToken, 'user')

expect(secondSession.uploadId).to.be.ok
Expand Down
1 change: 0 additions & 1 deletion packages/snjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
"@standardnotes/files": "workspace:*",
"@standardnotes/models": "workspace:*",
"@standardnotes/responses": "workspace:*",
"@standardnotes/security": "^1.7.6",
"@standardnotes/services": "workspace:*",
"@standardnotes/settings": "^1.20.0",
"@standardnotes/sncrypto-common": "workspace:*",
Expand Down