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

fix: fixes issue where files imported from another account could not be deleted #2082

Merged
merged 1 commit into from Dec 2, 2022
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
17 changes: 17 additions & 0 deletions packages/services/src/Domain/Files/FileService.spec.ts
Expand Up @@ -145,6 +145,23 @@ describe('fileService', () => {
expect(fileService['encryptedCache'].get(file.uuid)).toBeFalsy()
})

it('if file fails to delete, should present alert asking if they want to remove item', async () => {
const file = {
uuid: '1',
decryptedSize: 100_000,
} as jest.Mocked<FileItem>

const alertMock = (alertService.confirm = jest.fn().mockReturnValue(true))
const deleteItemMock = (itemManager.setItemToBeDeleted = jest.fn())

apiService.deleteFile = jest.fn().mockReturnValue({ error: true })

await fileService.deleteFile(file)

expect(alertMock).toHaveBeenCalledTimes(1)
expect(deleteItemMock).toHaveBeenCalledTimes(1)
})

it('should download file from network if no backup', async () => {
const file = {
uuid: '1',
Expand Down
20 changes: 17 additions & 3 deletions packages/services/src/Domain/Files/FileService.ts
Expand Up @@ -11,7 +11,7 @@ import {
isEncryptedPayload,
} from '@standardnotes/models'
import { PureCryptoInterface } from '@standardnotes/sncrypto-common'
import { UuidGenerator } from '@standardnotes/utils'
import { spaceSeparatedStrings, UuidGenerator } from '@standardnotes/utils'
import { EncryptionProviderInterface, SNItemsKey } from '@standardnotes/encryption'
import {
DownloadAndDecryptFileOperation,
Expand All @@ -33,7 +33,7 @@ import {
readAndDecryptBackupFileUsingBackupService,
BackupServiceInterface,
} from '@standardnotes/files'
import { AlertService } from '../Alert/AlertService'
import { AlertService, ButtonType } from '../Alert/AlertService'
import { ChallengeServiceInterface } from '../Challenge'
import { InternalEventBusInterface } from '../Internal/InternalEventBusInterface'
import { ItemManagerInterface } from '../Item/ItemManagerInterface'
Expand Down Expand Up @@ -244,7 +244,21 @@ export class FileService extends AbstractService implements FilesClientInterface
const result = await this.api.deleteFile(tokenResult)

if (result.error) {
return ClientDisplayableError.FromError(result.error)
const deleteAnyway = await this.alertService.confirm(
spaceSeparatedStrings(
'This file could not be deleted from the server, possibly because you are attempting to delete a file item',
'that was imported from another account. Would you like to remove this file item from your account anyway?',
"If you're sure the file is yours and still exists on the server, do not choose this option,",
'and instead try to delete it again.',
),
'Unable to Delete',
'Delete Anyway',
ButtonType.Danger,
)

if (!deleteAnyway) {
return ClientDisplayableError.FromError(result.error)
}
}

await this.itemManager.setItemToBeDeleted(file)
Expand Down
4 changes: 4 additions & 0 deletions packages/utils/src/Domain/Utils/Utils.ts
Expand Up @@ -683,3 +683,7 @@ export function assert(value: unknown): asserts value {
export function useBoolean(value: boolean | undefined, defaultValue: boolean): boolean {
return value != undefined ? value : defaultValue
}

export function spaceSeparatedStrings(...strings: string[]): string {
return strings.join(' ')
}
Expand Up @@ -10,7 +10,7 @@ import ModalDialogLabel from '@/Components/Shared/ModalDialogLabel'
import Button from '@/Components/Button/Button'
import ImportPlugin from './Plugins/ImportPlugin/ImportPlugin'
import { NoteViewController } from '../Controller/NoteViewController'
import { spaceSeparatedStrings } from '../../../Utils/spaceSeparatedStrings'
import { spaceSeparatedStrings } from '@standardnotes/utils'

const NotePreviewCharLimit = 160

Expand Down
3 changes: 0 additions & 3 deletions packages/web/src/javascripts/Utils/spaceSeparatedStrings.tsx

This file was deleted.