1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-24 10:27:10 +02:00

Desktop: Fixes #5133: Items keep being uploaded to Joplin Server after a note has been shared

This commit is contained in:
Laurent Cozic 2021-07-02 17:53:36 +01:00
parent 37ff926181
commit d34a92d581
2 changed files with 49 additions and 2 deletions

View File

@ -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);
}
});
});

View File

@ -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) {