Skip to content

Commit

Permalink
feat(syncing-server): add extended revisions frequency for free users (
Browse files Browse the repository at this point in the history
  • Loading branch information
karolsojko authored Dec 12, 2023
1 parent c7d21b0 commit 398c10c
Show file tree
Hide file tree
Showing 16 changed files with 76 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { BaseMiddleware } from 'inversify-express-utils'
import { verify } from 'jsonwebtoken'
import { Logger } from 'winston'
import { ConnectionValidationResponse, IAuthClient, WebsocketConnectionAuthorizationHeader } from '@standardnotes/grpc'
import { RoleName } from '@standardnotes/domain-core'

export class GRPCWebSocketAuthMiddleware extends BaseMiddleware {
constructor(
Expand Down Expand Up @@ -96,6 +97,8 @@ export class GRPCWebSocketAuthMiddleware extends BaseMiddleware {
response.locals.user = decodedToken.user
response.locals.session = decodedToken.session
response.locals.roles = decodedToken.roles
response.locals.isFreeUser =
decodedToken.roles.length === 1 && decodedToken.roles[0].name === RoleName.NAMES.CoreUser
} catch (error) {
this.logger.error(
`Could not pass the request to websocket connection validation on underlying service: ${
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export class GRPCSyncingServerServiceProxy {
if (response.locals.session) {
metadata.set('x-session-uuid', response.locals.session.uuid)
}
metadata.set('x-is-free-user', response.locals.isFreeUser ? 'true' : 'false')

this.syncingClient.syncItems(syncRequest, metadata, (error, syncResponse) => {
if (error) {
Expand Down
4 changes: 4 additions & 0 deletions packages/syncing-server/src/Bootstrap/Container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,9 @@ export class ContainerConfigLoader {
container
.bind(TYPES.Sync_REVISIONS_FREQUENCY)
.toConstantValue(env.get('REVISIONS_FREQUENCY', true) ? +env.get('REVISIONS_FREQUENCY', true) : 300)
container
.bind(TYPES.Sync_FREE_REVISIONS_FREQUENCY)
.toConstantValue(env.get('FREE_REVISIONS_FREQUENCY', true) ? +env.get('FREE_REVISIONS_FREQUENCY', true) : 86_400)
container.bind(TYPES.Sync_VERSION).toConstantValue(env.get('VERSION', true) ?? 'development')
container
.bind(TYPES.Sync_CONTENT_SIZE_TRANSFER_LIMIT)
Expand Down Expand Up @@ -601,6 +604,7 @@ export class ContainerConfigLoader {
container.get<TimerInterface>(TYPES.Sync_Timer),
container.get<DomainEventPublisherInterface>(TYPES.Sync_DomainEventPublisher),
container.get<DomainEventFactoryInterface>(TYPES.Sync_DomainEventFactory),
container.get<number>(TYPES.Sync_FREE_REVISIONS_FREQUENCY),
container.get<number>(TYPES.Sync_REVISIONS_FREQUENCY),
container.get<DetermineSharedVaultOperationOnItem>(TYPES.Sync_DetermineSharedVaultOperationOnItem),
container.get<AddNotificationsForUsers>(TYPES.Sync_AddNotificationsForUsers),
Expand Down
1 change: 1 addition & 0 deletions packages/syncing-server/src/Bootstrap/Types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const TYPES = {
Sync_S3_BACKUP_BUCKET_NAME: Symbol.for('Sync_S3_BACKUP_BUCKET_NAME'),
Sync_EMAIL_ATTACHMENT_MAX_BYTE_SIZE: Symbol.for('Sync_EMAIL_ATTACHMENT_MAX_BYTE_SIZE'),
Sync_REVISIONS_FREQUENCY: Symbol.for('Sync_REVISIONS_FREQUENCY'),
Sync_FREE_REVISIONS_FREQUENCY: Symbol.for('Sync_FREE_REVISIONS_FREQUENCY'),
Sync_VERSION: Symbol.for('Sync_VERSION'),
Sync_CONTENT_SIZE_TRANSFER_LIMIT: Symbol.for('Sync_CONTENT_SIZE_TRANSFER_LIMIT'),
Sync_MAX_ITEMS_LIMIT: Symbol.for('Sync_MAX_ITEMS_LIMIT'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ describe('SaveItems', () => {
readOnlyAccess: false,
sessionUuid: 'session-uuid',
snjsVersion: '2.200.0',
isFreeUser: false,
})

expect(result.isFailed()).toBeFalsy()
Expand All @@ -137,6 +138,7 @@ describe('SaveItems', () => {
readOnlyAccess: false,
sessionUuid: 'session-uuid',
snjsVersion: '2.200.0',
isFreeUser: false,
})

expect(result.isFailed()).toBeFalsy()
Expand All @@ -161,6 +163,7 @@ describe('SaveItems', () => {
readOnlyAccess: false,
sessionUuid: 'session-uuid',
snjsVersion: '2.200.0',
isFreeUser: false,
})

expect(result.isFailed()).toBeFalsy()
Expand All @@ -182,6 +185,7 @@ describe('SaveItems', () => {
readOnlyAccess: true,
sessionUuid: 'session-uuid',
snjsVersion: '2.200.0',
isFreeUser: false,
})

expect(result.isFailed()).toBeFalsy()
Expand All @@ -204,6 +208,7 @@ describe('SaveItems', () => {
readOnlyAccess: false,
sessionUuid: 'session-uuid',
snjsVersion: '2.200.0',
isFreeUser: false,
})

expect(result.isFailed()).toBeFalsy()
Expand All @@ -222,6 +227,7 @@ describe('SaveItems', () => {
readOnlyAccess: false,
sessionUuid: 'session-uuid',
snjsVersion: '2.200.0',
isFreeUser: false,
})

expect(result.isFailed()).toBeFalsy()
Expand All @@ -240,10 +246,12 @@ describe('SaveItems', () => {
readOnlyAccess: false,
sessionUuid: 'session-uuid',
snjsVersion: '2.200.0',
isFreeUser: false,
})

expect(result.isFailed()).toBeFalsy()
expect(updateExistingItem.execute).toHaveBeenCalledWith({
isFreeUser: false,
itemHash: itemHash1,
existingItem: savedItem,
sessionUuid: 'session-uuid',
Expand Down Expand Up @@ -284,10 +292,12 @@ describe('SaveItems', () => {
readOnlyAccess: false,
sessionUuid: 'session-uuid',
snjsVersion: '2.200.0',
isFreeUser: false,
})

expect(result.isFailed()).toBeFalsy()
expect(updateExistingItem.execute).toHaveBeenCalledWith({
isFreeUser: false,
itemHash: itemHash1,
existingItem: savedItem,
sessionUuid: 'session-uuid',
Expand All @@ -310,6 +320,7 @@ describe('SaveItems', () => {
readOnlyAccess: false,
sessionUuid: 'session-uuid',
snjsVersion: '2.200.0',
isFreeUser: false,
})

expect(result.isFailed()).toBeFalsy()
Expand All @@ -334,6 +345,7 @@ describe('SaveItems', () => {
readOnlyAccess: false,
sessionUuid: 'session-uuid',
snjsVersion: '2.200.0',
isFreeUser: false,
})

expect(result.isFailed()).toBeFalsy()
Expand Down Expand Up @@ -379,6 +391,7 @@ describe('SaveItems', () => {
readOnlyAccess: false,
sessionUuid: 'session-uuid',
snjsVersion: '2.200.0',
isFreeUser: false,
})

expect(result.isFailed()).toBeFalsy()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export class SaveItems implements UseCaseInterface<SaveItemsResult> {
itemHash,
sessionUuid: dto.sessionUuid,
performingUserUuid: dto.userUuid,
isFreeUser: dto.isFreeUser,
})
if (udpatedItemOrError.isFailed()) {
this.logger.error(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ export interface SaveItemsDTO {
readOnlyAccess: boolean
sessionUuid: string | null
snjsVersion: string
isFreeUser: boolean
}
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ describe('SyncItems', () => {
apiVersion: ApiVersion.v20200115,
sessionUuid: null,
snjsVersion: '1.2.3',
isFreeUser: false,
})
expect(result.getValue()).toEqual({
conflicts: [],
Expand All @@ -181,6 +182,7 @@ describe('SyncItems', () => {
userUuid: '1-2-3',
apiVersion: '20200115',
snjsVersion: '1.2.3',
isFreeUser: false,
readOnlyAccess: false,
sessionUuid: null,
})
Expand All @@ -205,6 +207,7 @@ describe('SyncItems', () => {
apiVersion: ApiVersion.v20200115,
sessionUuid: null,
snjsVersion: '1.2.3',
isFreeUser: false,
})
} catch (error) {
caughtError = error
Expand All @@ -224,6 +227,7 @@ describe('SyncItems', () => {
contentType: 'Note',
apiVersion: ApiVersion.v20200115,
snjsVersion: '1.2.3',
isFreeUser: false,
})
expect(result.getValue()).toEqual({
conflicts: [],
Expand All @@ -249,6 +253,7 @@ describe('SyncItems', () => {
contentType: 'Note',
apiVersion: ApiVersion.v20200115,
snjsVersion: '1.2.3',
isFreeUser: false,
sharedVaultUuids: ['00000000-0000-0000-0000-000000000000'],
})
expect(result.getValue()).toEqual({
Expand Down Expand Up @@ -301,6 +306,7 @@ describe('SyncItems', () => {
contentType: 'Note',
apiVersion: ApiVersion.v20200115,
snjsVersion: '1.2.3',
isFreeUser: false,
})

expect(result.getValue()).toEqual({
Expand Down Expand Up @@ -340,6 +346,7 @@ describe('SyncItems', () => {
contentType: 'Note',
apiVersion: ApiVersion.v20200115,
snjsVersion: '1.2.3',
isFreeUser: false,
})

expect(result.isFailed()).toBeTruthy()
Expand All @@ -360,6 +367,7 @@ describe('SyncItems', () => {
contentType: 'Note',
apiVersion: ApiVersion.v20200115,
snjsVersion: '1.2.3',
isFreeUser: false,
})

expect(result.isFailed()).toBeTruthy()
Expand All @@ -380,6 +388,7 @@ describe('SyncItems', () => {
contentType: 'Note',
apiVersion: ApiVersion.v20200115,
snjsVersion: '1.2.3',
isFreeUser: false,
})

expect(result.isFailed()).toBeTruthy()
Expand All @@ -400,6 +409,7 @@ describe('SyncItems', () => {
contentType: 'Note',
apiVersion: ApiVersion.v20200115,
snjsVersion: '1.2.3',
isFreeUser: false,
})

expect(result.isFailed()).toBeTruthy()
Expand All @@ -420,6 +430,7 @@ describe('SyncItems', () => {
contentType: 'Note',
apiVersion: ApiVersion.v20200115,
snjsVersion: '1.2.3',
isFreeUser: false,
})

expect(result.isFailed()).toBeTruthy()
Expand All @@ -440,6 +451,7 @@ describe('SyncItems', () => {
contentType: 'Note',
apiVersion: ApiVersion.v20200115,
snjsVersion: '1.2.3',
isFreeUser: false,
})

expect(result.isFailed()).toBeTruthy()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export class SyncItems implements UseCaseInterface<SyncItemsResponse> {
readOnlyAccess: dto.readOnlyAccess,
sessionUuid: dto.sessionUuid,
snjsVersion: dto.snjsVersion,
isFreeUser: dto.isFreeUser,
})
if (saveItemsResultOrError.isFailed()) {
return Result.fail(saveItemsResultOrError.getError())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ export type SyncItemsDTO = {
snjsVersion: string
readOnlyAccess: boolean
sessionUuid: string | null
isFreeUser: boolean
}
Loading

0 comments on commit 398c10c

Please sign in to comment.