diff --git a/packages/lib/models/Folder.sharing.test.ts b/packages/lib/models/Folder.sharing.test.ts index ad71fee0f..7fcb5c20f 100644 --- a/packages/lib/models/Folder.sharing.test.ts +++ b/packages/lib/models/Folder.sharing.test.ts @@ -356,6 +356,8 @@ describe('models_Folder.sharing', function() { const resourceService = new ResourceService(); + const folder1: FolderEntity = await Folder.loadByTitle('folder 1'); + const folder2: FolderEntity = await Folder.loadByTitle('folder 2'); let note1: NoteEntity = await Note.loadByTitle('note 1'); let note2: NoteEntity = await Note.loadByTitle('note 2'); note1 = await shim.attachFileToNote(note1, testImagePath); @@ -369,6 +371,13 @@ describe('models_Folder.sharing', function() { await Folder.updateNoLongerSharedItems(['1']); + // Since `updateNoLongerSharedItems` sets the parent_id too, + // double-check that it's not actually modified. + expect((await Note.loadByTitle('note 1')).parent_id).toBe(folder1.id); + expect((await Note.loadByTitle('note 2')).parent_id).toBe(folder2.id); + expect((await Folder.loadByTitle('folder 1')).parent_id).toBe(folder1.parent_id); + expect((await Folder.loadByTitle('folder 2')).parent_id).toBe(folder2.parent_id); + // At this point, all items associated with share 2 should have their // share_id cleared, because the share no longer exists. We also // double-check that share 1 hasn't been cleared. diff --git a/packages/lib/models/Folder.ts b/packages/lib/models/Folder.ts index ec861714c..a0e6d8819 100644 --- a/packages/lib/models/Folder.ts +++ b/packages/lib/models/Folder.ts @@ -320,12 +320,12 @@ export default class Folder extends BaseItem { // if they've been moved out of a shared folder. // await this.unshareItems(ModelType.Folder, sharedFolderIds); - const sql = ['SELECT id FROM folders WHERE share_id != ""']; + const sql = ['SELECT id, parent_id FROM folders WHERE share_id != ""']; if (sharedFolderIds.length) { sql.push(` AND id NOT IN ("${sharedFolderIds.join('","')}")`); } - const foldersToUnshare = await this.db().selectAll(sql.join(' ')); + const foldersToUnshare: FolderEntity[] = await this.db().selectAll(sql.join(' ')); report.unshareUpdateCount += foldersToUnshare.length; @@ -334,6 +334,7 @@ export default class Folder extends BaseItem { id: item.id, share_id: '', updated_time: Date.now(), + parent_id: item.parent_id, }, { autoTimestamp: false }); } @@ -407,12 +408,16 @@ export default class Folder extends BaseItem { for (const tableName of ['folders', 'notes', 'resources']) { const ItemClass = tableNameToClasses[tableName]; + const hasParentId = tableName !== 'resources'; + + const fields = ['id']; + if (hasParentId) fields.push('parent_id'); const query = activeShareIds.length ? ` - SELECT id FROM ${tableName} + SELECT ${this.db().escapeFields(fields)} FROM ${tableName} WHERE share_id != "" AND share_id NOT IN ("${activeShareIds.join('","')}") ` : ` - SELECT id FROM ${tableName} + SELECT ${this.db().escapeFields(fields)} FROM ${tableName} WHERE share_id != '' `; @@ -421,11 +426,15 @@ export default class Folder extends BaseItem { report[tableName] = rows.length; for (const row of rows) { - await ItemClass.save({ + const toSave: any = { id: row.id, share_id: '', updated_time: Date.now(), - }, { autoTimestamp: false }); + }; + + if (hasParentId) toSave.parent_id = row.parent_id; + + await ItemClass.save(toSave, { autoTimestamp: false }); } }