-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…'t work with the trash feature (#10104)
- Loading branch information
1 parent
8eea395
commit c16ce1c
Showing
16 changed files
with
193 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
|
||
export default () => 'c04f1c7c04f1c7c04f1c7c04f1c7c04f'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { ModelType } from '../../BaseModel'; | ||
import Folder from '../Folder'; | ||
import ItemChange from '../ItemChange'; | ||
import Note from '../Note'; | ||
import { defaultState as defaultShareState, State as ShareState } from '../../services/share/reducer'; | ||
import { ItemSlice, itemIsReadOnlySync } from './readOnly'; | ||
import Resource from '../Resource'; | ||
import shim from '../../shim'; | ||
import { setupDatabaseAndSynchronizer, simulateReadOnlyShareEnv, switchClient, tempFilePath } from '../../testing/test-utils'; | ||
import BaseItem from '../BaseItem'; | ||
|
||
|
||
const checkReadOnly = (itemType: ModelType, item: ItemSlice, shareData: ShareState = defaultShareState) => { | ||
const syncUserId = ''; | ||
return itemIsReadOnlySync(itemType, ItemChange.SOURCE_UNSPECIFIED, item, syncUserId, shareData); | ||
}; | ||
|
||
const createTestResource = async () => { | ||
const tempFile = tempFilePath('txt'); | ||
await shim.fsDriver().writeFile(tempFile, 'Test', 'utf8'); | ||
const note1 = await Note.save({ title: 'note' }); | ||
await shim.attachFileToNote(note1, tempFile); | ||
}; | ||
|
||
describe('readOnly', () => { | ||
beforeEach(async () => { | ||
await setupDatabaseAndSynchronizer(1); | ||
await switchClient(1); | ||
}); | ||
|
||
test('trashed items should be marked as read-only', async () => { | ||
let folder = await Folder.save({ title: 'Test' }); | ||
let note = await Note.save({ parent_id: folder.id, title: 'Test note' }); | ||
|
||
expect(checkReadOnly(ModelType.Note, note as ItemSlice)).toBe(false); | ||
expect(checkReadOnly(ModelType.Folder, folder as ItemSlice)).toBe(false); | ||
|
||
await Folder.delete(folder.id, { toTrash: true }); | ||
|
||
// Should be deleted | ||
note = await Note.load(note.id); | ||
expect(note.deleted_time).not.toBe(0); | ||
folder = await Folder.load(folder.id); | ||
expect(folder.deleted_time).not.toBe(0); | ||
|
||
expect(checkReadOnly(ModelType.Note, note as ItemSlice)).toBe(true); | ||
expect(checkReadOnly(ModelType.Folder, folder as ItemSlice)).toBe(true); | ||
}); | ||
|
||
test('should support checking if resources are not read-only', async () => { | ||
await createTestResource(); | ||
const resource = (await Resource.all())[0]; | ||
expect(checkReadOnly(ModelType.Resource, resource)).toBe(false); | ||
}); | ||
|
||
test('should support checking that resources are read-only due to a share', async () => { | ||
await createTestResource(); | ||
|
||
const share_id = '123456'; | ||
let resource = (await Resource.all())[0]; | ||
resource = await Resource.save({ ...resource, share_id }); | ||
|
||
const cleanup = simulateReadOnlyShareEnv(share_id); | ||
expect(checkReadOnly(ModelType.Resource, resource, BaseItem.syncShareCache)).toBe(true); | ||
cleanup(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
|
||
const getTrashFolderId = () => { | ||
return 'de1e7ede1e7ede1e7ede1e7ede1e7ede'; | ||
}; | ||
|
||
export default getTrashFolderId; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { checkObjectHasProperties } from '@joplin/utils/object'; | ||
import { ModelType } from '../../BaseModel'; | ||
import isTrashableNoteOrFolder from './isTrashableNoteOrFolder'; | ||
|
||
type ItemSlice = { id?: string }; | ||
const isTrashableItem = (itemType: ModelType, item: ItemSlice) => { | ||
checkObjectHasProperties(item, ['id']); | ||
|
||
if (itemType !== ModelType.Folder && itemType !== ModelType.Note) { | ||
return false; | ||
} | ||
|
||
return isTrashableNoteOrFolder(item); | ||
}; | ||
|
||
export default isTrashableItem; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { checkObjectHasProperties } from '@joplin/utils/object'; | ||
import conflictFolderId from '../../models/utils/getConflictFolderId'; | ||
import getTrashFolderId from './getTrashFolderId'; | ||
|
||
type ItemSlice = { id?: string }; | ||
|
||
// This function is separate from isTrashableItem to handle the case where we know that an item | ||
// is either a note or a folder, but don't know which. | ||
const isTrashableNoteOrFolder = (item: ItemSlice) => { | ||
checkObjectHasProperties(item, ['id']); | ||
return item.id !== conflictFolderId() && item.id !== getTrashFolderId(); | ||
}; | ||
|
||
export default isTrashableNoteOrFolder; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.