Skip to content

Commit

Permalink
chore: display shared vault file usage
Browse files Browse the repository at this point in the history
  • Loading branch information
karolsojko committed Aug 10, 2023
1 parent 5bb749b commit f038463
Show file tree
Hide file tree
Showing 26 changed files with 127 additions and 23 deletions.
Binary file not shown.
2 changes: 1 addition & 1 deletion packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
},
"dependencies": {
"@standardnotes/common": "^1.50.0",
"@standardnotes/domain-core": "^1.24.0",
"@standardnotes/domain-core": "^1.25.0",
"@standardnotes/models": "workspace:*",
"@standardnotes/responses": "workspace:*",
"@standardnotes/utils": "workspace:*",
Expand Down
2 changes: 1 addition & 1 deletion packages/desktop/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
},
"dependencies": {
"@electron/remote": "^2.0.9",
"@standardnotes/domain-core": "^1.24.0",
"@standardnotes/domain-core": "^1.25.0",
"@standardnotes/electron-clear-data": "1.1.1",
"@standardnotes/web": "workspace:*",
"axios": "^1.1.3",
Expand Down
2 changes: 1 addition & 1 deletion packages/encryption/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
},
"dependencies": {
"@standardnotes/common": "^1.50.0",
"@standardnotes/domain-core": "^1.24.0",
"@standardnotes/domain-core": "^1.25.0",
"@standardnotes/models": "workspace:*",
"@standardnotes/responses": "workspace:*",
"@standardnotes/sncrypto-common": "workspace:*",
Expand Down
2 changes: 1 addition & 1 deletion packages/features/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
},
"dependencies": {
"@standardnotes/common": "^1.50.0",
"@standardnotes/domain-core": "^1.24.0",
"@standardnotes/domain-core": "^1.25.0",
"reflect-metadata": "^0.1.13"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/models/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
},
"dependencies": {
"@standardnotes/common": "^1.50.0",
"@standardnotes/domain-core": "^1.24.0",
"@standardnotes/domain-core": "^1.25.0",
"@standardnotes/features": "workspace:*",
"@standardnotes/responses": "workspace:*",
"@standardnotes/sncrypto-common": "workspace:^",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,7 @@ export function isDeletedTransferPayload(payload: TransferPayload): payload is D
export function isCorruptTransferPayload(payload: TransferPayload): boolean {
const invalidDeletedState = payload.deleted === true && payload.content != undefined

return payload.uuid == undefined || invalidDeletedState || payload.content_type === ContentType.TYPES.Unknown
const contenTypeOrError = ContentType.create(payload.content_type)

return payload.uuid == undefined || invalidDeletedState || contenTypeOrError.isFailed()
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export type AsymmetricMessageSharedVaultInvite = {
name: string
description?: string
iconString: IconType | EmojiString
fileBytesUsed: number
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export type VaultListingSharingInfo = {
sharedVaultUuid: string
ownerUserUuid: string
fileBytesUsed: number
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
export interface SharedVaultServerHash {
uuid: string
user_uuid: string
file_upload_bytes_used: number
file_upload_bytes_limit: number
created_at_timestamp: number
updated_at_timestamp: number
}
2 changes: 1 addition & 1 deletion packages/services/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"dependencies": {
"@standardnotes/api": "workspace:^",
"@standardnotes/common": "^1.50.0",
"@standardnotes/domain-core": "^1.24.0",
"@standardnotes/domain-core": "^1.25.0",
"@standardnotes/encryption": "workspace:^",
"@standardnotes/features": "workspace:^",
"@standardnotes/files": "workspace:^",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export class ProcessAcceptedVaultInvite {
sharing: {
sharedVaultUuid: sharedVaultUuid,
ownerUserUuid: ownerUuid,
fileBytesUsed: metadata.fileBytesUsed,
},
}

Expand Down
20 changes: 18 additions & 2 deletions packages/services/src/Domain/SharedVaults/SharedVaultService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { ContentType, NotificationType, Uuid } from '@standardnotes/domain-core'
import { HandleKeyPairChange } from '../Contacts/UseCase/HandleKeyPairChange'
import { FindContact } from '../Contacts/UseCase/FindContact'
import { GetOwnedSharedVaults } from './UseCase/GetOwnedSharedVaults'
import { SyncLocalVaultsWithRemoteSharedVaults } from './UseCase/SyncLocalVaultsWithRemoteSharedVaults'

export class SharedVaultService
extends AbstractService<SharedVaultServiceEvent, SharedVaultServiceEventPayload>
Expand All @@ -40,6 +41,7 @@ export class SharedVaultService
constructor(
private items: ItemManagerInterface,
private session: SessionsClientInterface,
private _syncLocalVaultsWithRemoteSharedVaults: SyncLocalVaultsWithRemoteSharedVaults,
private _getVault: GetVault,
private _getOwnedSharedVaults: GetOwnedSharedVaults,
private _createSharedVault: CreateSharedVault,
Expand Down Expand Up @@ -67,6 +69,7 @@ export class SharedVaultService
super.deinit()
;(this.items as unknown) = undefined
;(this.session as unknown) = undefined
;(this._syncLocalVaultsWithRemoteSharedVaults as unknown) = undefined
;(this._getVault as unknown) = undefined
;(this._createSharedVault as unknown) = undefined
;(this._handleKeyPairChange as unknown) = undefined
Expand All @@ -88,15 +91,15 @@ export class SharedVaultService
break
}
case NotificationServiceEvent.NotificationReceived:
await this.handleUserEvent(event.payload as NotificationServiceEventPayload)
await this.handleNotification(event.payload as NotificationServiceEventPayload)
break
case SyncEvent.ReceivedRemoteSharedVaults:
void this.notifyEventSync(SharedVaultServiceEvent.SharedVaultStatusChanged)
break
}
}

private async handleUserEvent(event: NotificationServiceEventPayload): Promise<void> {
private async handleNotification(event: NotificationServiceEventPayload): Promise<void> {
switch (event.eventPayload.props.type.value) {
case NotificationType.TYPES.RemovedFromSharedVault: {
const vault = this._getVault.execute<SharedVaultListingInterface>({
Expand All @@ -114,6 +117,19 @@ export class SharedVaultService
}
break
}
case NotificationType.TYPES.SharedVaultFileRemoved:
case NotificationType.TYPES.SharedVaultFileUploaded: {
const vaultOrError = this._getVault.execute<SharedVaultListingInterface>({
sharedVaultUuid: event.eventPayload.props.sharedVaultUuid.value,
})
if (!vaultOrError.isFailed()) {
await this._syncLocalVaultsWithRemoteSharedVaults.execute([vaultOrError.getValue()])

void this.notifyEventSync(SharedVaultServiceEvent.SharedVaultStatusChanged)
}

break
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { KeySystemIdentifier } from '@standardnotes/models'

export enum SharedVaultServiceEvent {
SharedVaultStatusChanged = 'SharedVaultStatusChanged',
SharedVaultFileStorageUsageChanged = 'SharedVaultFileStorageUsageChanged',
}

export type SharedVaultServiceEventPayload = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export class ConvertToSharedVault {
mutator.sharing = {
sharedVaultUuid: serverVaultHash.uuid,
ownerUserUuid: serverVaultHash.user_uuid,
fileBytesUsed: serverVaultHash.file_upload_bytes_used,
}
},
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export class CreateSharedVault {
mutator.sharing = {
sharedVaultUuid: serverVaultHash.uuid,
ownerUserUuid: serverVaultHash.user_uuid,
fileBytesUsed: serverVaultHash.file_upload_bytes_used,
}
},
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { Result, UseCaseInterface } from '@standardnotes/domain-core'
import { SharedVaultServerInterface } from '@standardnotes/api'
import { isErrorResponse } from '@standardnotes/responses'
import { SharedVaultListingInterface, VaultListingInterface, VaultListingMutator } from '@standardnotes/models'

import { MutatorClientInterface } from '../../Mutator/MutatorClientInterface'

export class SyncLocalVaultsWithRemoteSharedVaults implements UseCaseInterface<void> {
constructor(
private sharedVaultServer: SharedVaultServerInterface,
private mutator: MutatorClientInterface,
) {}

async execute(localVaults: VaultListingInterface[]): Promise<Result<void>> {
const remoteVaultsResponse = await this.sharedVaultServer.getSharedVaults()
if (isErrorResponse(remoteVaultsResponse)) {
return Result.fail(remoteVaultsResponse.data.error?.message as string)
}
const remoteVaults = remoteVaultsResponse.data.sharedVaults
console.log('Remote shared vaults', remoteVaults)

for (const localVault of localVaults) {
if (!localVault.isSharedVaultListing()) {
continue
}
console.log('Processing local vault', localVault.sharing.sharedVaultUuid)
const remoteVault = remoteVaults.find((vault) => vault.uuid === localVault.sharing.sharedVaultUuid)
if (remoteVault) {
console.log('Syncing local vault with remote shared vault', localVault, remoteVault)
await this.mutator.changeItem<VaultListingMutator, SharedVaultListingInterface>(localVault, (mutator) => {
mutator.sharing = {
sharedVaultUuid: remoteVault.uuid,
ownerUserUuid: remoteVault.user_uuid,
fileBytesUsed: remoteVault.file_upload_bytes_used,
}
})
}
}

return Result.ok()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ export class InviteToVault implements UseCaseInterface<SharedVaultInviteServerHa
name: params.sharedVault.name,
description: params.sharedVault.description,
iconString: params.sharedVault.iconString,
fileBytesUsed: params.sharedVault.sharing.fileBytesUsed,
},
},
},
Expand Down
1 change: 1 addition & 0 deletions packages/services/src/Domain/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ export * from './SharedVaults/UseCase/NotifyVaultUsersOfKeyRotation'
export * from './SharedVaults/UseCase/SendVaultDataChangedMessage'
export * from './SharedVaults/UseCase/SendVaultKeyChangedMessage'
export * from './SharedVaults/UseCase/ShareContactWithVault'
export * from './SharedVaults/UseCase/SyncLocalVaultsWithRemoteSharedVaults'
export * from './Singleton/SingletonManagerInterface'
export * from './Status/StatusService'
export * from './Status/StatusServiceInterface'
Expand Down
9 changes: 9 additions & 0 deletions packages/snjs/lib/Application/Dependencies/Dependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ import {
GetVaultItems,
ValidateVaultPassword,
IsApplicationUsingThirdPartyHost,
SyncLocalVaultsWithRemoteSharedVaults,
} from '@standardnotes/services'
import { ItemManager } from '../../Services/Items/ItemManager'
import { PayloadManager } from '../../Services/Payloads/PayloadManager'
Expand Down Expand Up @@ -350,6 +351,13 @@ export class Dependencies {
return new GetVaults(this.get<ItemManager>(TYPES.ItemManager))
})

this.factory.set(TYPES.SyncLocalVaultsWithRemoteSharedVaults, () => {
return new SyncLocalVaultsWithRemoteSharedVaults(
this.get<SharedVaultServer>(TYPES.SharedVaultServer),
this.get<MutatorService>(TYPES.MutatorService),
)
})

this.factory.set(TYPES.ChangeAndSaveItem, () => {
return new ChangeAndSaveItem(
this.get<ItemManager>(TYPES.ItemManager),
Expand Down Expand Up @@ -844,6 +852,7 @@ export class Dependencies {
return new SharedVaultService(
this.get<ItemManager>(TYPES.ItemManager),
this.get<SessionManager>(TYPES.SessionManager),
this.get<SyncLocalVaultsWithRemoteSharedVaults>(TYPES.SyncLocalVaultsWithRemoteSharedVaults),
this.get<GetVault>(TYPES.GetVault),
this.get<GetOwnedSharedVaults>(TYPES.GetOwnedSharedVaults),
this.get<CreateSharedVault>(TYPES.CreateSharedVault),
Expand Down
1 change: 1 addition & 0 deletions packages/snjs/lib/Application/Dependencies/Types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ export const TYPES = {
ValidateItemSigner: Symbol.for('ValidateItemSigner'),
GetVault: Symbol.for('GetVault'),
GetVaults: Symbol.for('GetVaults'),
SyncLocalVaultsWithRemoteSharedVaults: Symbol.for('SyncLocalVaultsWithRemoteSharedVaults'),
GetSharedVaults: Symbol.for('GetSharedVaults'),
GetOwnedSharedVaults: Symbol.for('GetOwnedSharedVaults'),
ChangeVaultKeyOptions: Symbol.for('ChangeVaultKeyOptions'),
Expand Down
2 changes: 1 addition & 1 deletion packages/snjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"@babel/preset-env": "*",
"@standardnotes/api": "workspace:*",
"@standardnotes/common": "^1.50.0",
"@standardnotes/domain-core": "^1.24.0",
"@standardnotes/domain-core": "^1.25.0",
"@standardnotes/domain-events": "^2.108.1",
"@standardnotes/encryption": "workspace:*",
"@standardnotes/features": "workspace:*",
Expand Down
2 changes: 1 addition & 1 deletion packages/ui-services/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
},
"dependencies": {
"@standardnotes/common": "^1.50.0",
"@standardnotes/domain-core": "^1.24.0",
"@standardnotes/domain-core": "^1.25.0",
"@standardnotes/features": "workspace:^",
"@standardnotes/filepicker": "workspace:^",
"@standardnotes/models": "workspace:^",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ const Vaults = () => {
})
}, [application.sharedVaults, updateAllData])

useEffect(() => {
return application.sharedVaults.addEventObserver((event) => {
if (event === SharedVaultServiceEvent.SharedVaultFileStorageUsageChanged) {
void updateAllData()
}
})
}, [application.sharedVaults, updateAllData])

useEffect(() => {
return application.vaultUsers.addEventObserver((event) => {
if (event === VaultUserServiceEvent.UsersChanged) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { formatSizeToReadableString } from '@standardnotes/filepicker'
import { ButtonType, VaultListingInterface, isClientDisplayableError } from '@standardnotes/snjs'
import { useCallback, useState } from 'react'

import { useApplication } from '@/Components/ApplicationProvider'
import Button from '@/Components/Button/Button'
import Icon from '@/Components/Icon/Icon'
import ModalOverlay from '@/Components/Modal/ModalOverlay'
import { ButtonType, VaultListingInterface, isClientDisplayableError } from '@standardnotes/snjs'
import { useCallback, useState } from 'react'
import ContactInviteModal from '../Invites/ContactInviteModal'
import EditVaultModal from './VaultModal/EditVaultModal'

type Props = {
vault: VaultListingInterface
}
Expand Down Expand Up @@ -123,6 +124,10 @@ const VaultItem = ({ vault }: Props) => {
)}
<span className="mr-auto overflow-hidden text-ellipsis text-sm">Vault ID: {vault.systemIdentifier}</span>

<span className="mr-auto overflow-hidden text-ellipsis text-sm">
File storage used: {formatSizeToReadableString(vault.sharing?.fileBytesUsed ?? 0)}
</span>

<div className="mt-2 flex w-full">
<Button label="Edit" className="mr-3 text-xs" onClick={openEditModal} />
{isAdmin && <Button colorStyle="danger" label="Delete" className="mr-3 text-xs" onClick={deleteVault} />}
Expand Down

0 comments on commit f038463

Please sign in to comment.