From d34a92d581773ff1f39b51f8eb09c3e1222ed9b2 Mon Sep 17 00:00:00 2001 From: Laurent Cozic Date: Fri, 2 Jul 2021 17:53:36 +0100 Subject: [PATCH] Desktop: Fixes #5133: Items keep being uploaded to Joplin Server after a note has been shared --- packages/lib/models/Folder.sharing.test.ts | 24 ++++++++++++++++++- packages/lib/models/Folder.ts | 27 +++++++++++++++++++++- 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/packages/lib/models/Folder.sharing.test.ts b/packages/lib/models/Folder.sharing.test.ts index 9e7bed52c..ad71fee0f 100644 --- a/packages/lib/models/Folder.sharing.test.ts +++ b/packages/lib/models/Folder.sharing.test.ts @@ -1,4 +1,4 @@ -import { setupDatabaseAndSynchronizer, switchClient, createFolderTree, supportDir } from '../testing/test-utils'; +import { setupDatabaseAndSynchronizer, switchClient, createFolderTree, supportDir, msleep } from '../testing/test-utils'; import Folder from '../models/Folder'; import { allNotesFolders } from '../testing/test-utils-synchronizer'; import Note from '../models/Note'; @@ -385,6 +385,28 @@ describe('models_Folder.sharing', function() { expect((await Note.loadByTitle('note 1')).share_id).toBe(''); expect((await Folder.loadByTitle('folder 1')).share_id).toBe(''); expect((await Resource.load(resourceId1)).share_id).toBe(''); + + { + // If we run it again, it should not update the notes since the share_id + // has already been cleared. + const resource1 = await Resource.load(resourceId1); + const resource2 = await Resource.load(resourceId2); + const note1 = await Note.loadByTitle('note 1'); + const note2 = await Note.loadByTitle('note 2'); + const folder1 = await Folder.loadByTitle('folder 1'); + const folder2 = await Folder.loadByTitle('folder 2'); + + await msleep(1); + + await Folder.updateNoLongerSharedItems(['1']); + + expect((await Resource.load(resourceId1)).updated_time).toBe(resource1.updated_time); + expect((await Resource.load(resourceId2)).updated_time).toBe(resource2.updated_time); + expect((await Note.loadByTitle('note 1')).updated_time).toBe(note1.updated_time); + expect((await Note.loadByTitle('note 2')).updated_time).toBe(note2.updated_time); + expect((await Folder.loadByTitle('folder 1')).updated_time).toBe(folder1.updated_time); + expect((await Folder.loadByTitle('folder 2')).updated_time).toBe(folder2.updated_time); + } }); }); diff --git a/packages/lib/models/Folder.ts b/packages/lib/models/Folder.ts index 92b4618d2..ec861714c 100644 --- a/packages/lib/models/Folder.ts +++ b/packages/lib/models/Folder.ts @@ -7,8 +7,11 @@ import Database from '../database'; import BaseItem from './BaseItem'; import Resource from './Resource'; import { isRootSharedFolder } from '../services/share/reducer'; +import Logger from '../Logger'; const { substrWithEllipsis } = require('../string-utils.js'); +const logger = Logger.create('models/Folder'); + interface FolderEntityWithChildren extends FolderEntity { children?: FolderEntity[]; } @@ -288,9 +291,16 @@ export default class Folder extends BaseItem { let sharedFolderIds: string[] = []; + const report = { + shareUpdateCount: 0, + unshareUpdateCount: 0, + }; + for (const rootFolder of rootFolders) { const children = await this.allChildrenFolders(rootFolder.id); + report.shareUpdateCount += children.length; + for (const child of children) { if (child.share_id !== rootFolder.share_id) { await this.save({ @@ -316,6 +326,9 @@ export default class Folder extends BaseItem { } const foldersToUnshare = await this.db().selectAll(sql.join(' ')); + + report.unshareUpdateCount += foldersToUnshare.length; + for (const item of foldersToUnshare) { await this.save({ id: item.id, @@ -323,6 +336,8 @@ export default class Folder extends BaseItem { updated_time: Date.now(), }, { autoTimestamp: false }); } + + logger.debug('updateFolderShareIds:', report); } public static async updateNoteShareIds() { @@ -335,6 +350,8 @@ export default class Folder extends BaseItem { WHERE notes.share_id != folders.share_id `); + logger.debug('updateNoteShareIds: notes to update:', rows.length); + for (const row of rows) { await Note.save({ id: row.id, @@ -359,6 +376,8 @@ export default class Folder extends BaseItem { OR n.is_shared != r.is_shared `); + logger.debug('updateResourceShareIds: resources to update:', rows.length); + for (const row of rows) { await Resource.save({ id: row.id, @@ -384,12 +403,14 @@ export default class Folder extends BaseItem { 'resources': Resource, }; + const report: any = {}; + for (const tableName of ['folders', 'notes', 'resources']) { const ItemClass = tableNameToClasses[tableName]; const query = activeShareIds.length ? ` SELECT id FROM ${tableName} - WHERE share_id NOT IN ("${activeShareIds.join('","')}") + WHERE share_id != "" AND share_id NOT IN ("${activeShareIds.join('","')}") ` : ` SELECT id FROM ${tableName} WHERE share_id != '' @@ -397,6 +418,8 @@ export default class Folder extends BaseItem { const rows = await this.db().selectAll(query); + report[tableName] = rows.length; + for (const row of rows) { await ItemClass.save({ id: row.id, @@ -405,6 +428,8 @@ export default class Folder extends BaseItem { }, { autoTimestamp: false }); } } + + logger.debug('updateNoLongerSharedItems:', report); } static async allAsTree(folders: FolderEntity[] = null, options: any = null) {