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

Fixed synchronizer conflict handling when deleting folders

This commit is contained in:
Laurent Cozic 2017-07-13 18:47:31 +00:00
parent e397ad197d
commit 7859a1b990
12 changed files with 149 additions and 26 deletions

View File

@ -322,6 +322,53 @@ describe('Synchronizer', function() {
done(); done();
}); });
it('should resolve conflict if remote folder has been deleted, but note has been added to folder locally', async (done) => {
let folder1 = await Folder.save({ title: "folder1" });
await synchronizer().start();
await switchClient(2);
await synchronizer().start();
await Folder.delete(folder1.id);
await synchronizer().start();
await switchClient(1);
let note = await Note.save({ title: "note1", parent_id: folder1.id });
await synchronizer().start();
let items = await allItems();
expect(items.length).toBe(1);
expect(items[0].title).toBe('note1');
expect(items[0].is_conflict).toBe(1);
done();
});
it('should resolve conflict if note has been deleted remotely and locally', async (done) => {
let folder = await Folder.save({ title: "folder" });
let note = await Note.save({ title: "note", parent_id: folder.title });
await synchronizer().start();
await switchClient(2);
await synchronizer().start();
await Note.delete(note.id);
await synchronizer().start();
await switchClient(1);
await Note.delete(note.id);
await synchronizer().start();
let items = await allItems();
expect(items.length).toBe(1);
expect(items[0].title).toBe('folder');
localItemsSameAsRemote(items, expect);
done();
});
it('should cross delete all folders', async (done) => { it('should cross delete all folders', async (done) => {
// If client1 and 2 have two folders, client 1 deletes item 1 and client // If client1 and 2 have two folders, client 1 deletes item 1 and client
// 2 deletes item 2, they should both end up with no items after sync. // 2 deletes item 2, they should both end up with no items after sync.

View File

@ -47,7 +47,7 @@ class LogScreenComponent extends React.Component {
}; };
return ( return (
<View style={{flexDirection: 'row', paddingLeft: 1, paddingRight: 1, paddingTop:0, paddingBottom:0 }}> <View style={{flexDirection: 'row', paddingLeft: 1, paddingRight: 1, paddingTop:0, paddingBottom:0 }}>
<Text style={style}>{time.unixMsToIsoSec(item.timestamp) + ': ' + item.message}</Text> <Text style={style}>{time.formatMsToLocal(item.timestamp, 'MM-DDTHH:mm') + ': ' + item.message}</Text>
</View> </View>
); );
} }

View File

@ -1,5 +1,5 @@
import React, { Component } from 'react'; import React, { Component } from 'react';
import { View, Button, TextInput, WebView } from 'react-native'; import { View, Button, TextInput, WebView, Text } from 'react-native';
import { connect } from 'react-redux' import { connect } from 'react-redux'
import { Log } from 'lib/log.js' import { Log } from 'lib/log.js'
import { Note } from 'lib/models/note.js' import { Note } from 'lib/models/note.js'
@ -19,6 +19,8 @@ class NoteScreenComponent extends React.Component {
this.state = { this.state = {
note: Note.new(), note: Note.new(),
mode: 'view', mode: 'view',
noteMetadata: '',
showNoteMetadata: false,
} }
} }
@ -26,9 +28,11 @@ class NoteScreenComponent extends React.Component {
if (!this.props.noteId) { if (!this.props.noteId) {
let note = this.props.itemType == 'todo' ? Note.newTodo(this.props.folderId) : Note.new(this.props.folderId); let note = this.props.itemType == 'todo' ? Note.newTodo(this.props.folderId) : Note.new(this.props.folderId);
this.setState({ note: note }); this.setState({ note: note });
this.refreshNoteMetadata();
} else { } else {
Note.load(this.props.noteId).then((note) => { Note.load(this.props.noteId).then((note) => {
this.setState({ note: note }); this.setState({ note: note });
this.refreshNoteMetadata();
}); });
} }
} }
@ -41,6 +45,13 @@ class NoteScreenComponent extends React.Component {
}); });
} }
async refreshNoteMetadata(force = null) {
if (force !== true && !this.state.showNoteMetadata) return;
let noteMetadata = await Note.serializeAllProps(this.state.note);
this.setState({ noteMetadata: noteMetadata });
}
title_changeText(text) { title_changeText(text) {
this.noteComponent_change('title', text); this.noteComponent_change('title', text);
} }
@ -54,6 +65,7 @@ class NoteScreenComponent extends React.Component {
let note = await Note.save(this.state.note); let note = await Note.save(this.state.note);
this.setState({ note: note }); this.setState({ note: note });
if (isNew) Note.updateGeolocation(note.id); if (isNew) Note.updateGeolocation(note.id);
this.refreshNoteMetadata();
} }
deleteNote_onPress(noteId) { deleteNote_onPress(noteId) {
@ -61,12 +73,19 @@ class NoteScreenComponent extends React.Component {
} }
attachFile_onPress(noteId) { attachFile_onPress(noteId) {
}
showMetadata_onPress() {
this.setState({ showNoteMetadata: !this.state.showNoteMetadata });
this.refreshNoteMetadata(true);
} }
menuOptions() { menuOptions() {
return [ return [
{ title: _('Attach file'), onPress: () => { this.attachFile_onPress(this.state.note.id); } }, { title: _('Attach file'), onPress: () => { this.attachFile_onPress(this.state.note.id); } },
{ title: _('Delete note'), onPress: () => { this.deleteNote_onPress(this.state.note.id); } }, { title: _('Delete note'), onPress: () => { this.deleteNote_onPress(this.state.note.id); } },
{ title: _('Toggle metadata'), onPress: () => { this.showMetadata_onPress(); } },
]; ];
} }
@ -99,6 +118,8 @@ class NoteScreenComponent extends React.Component {
bodyComponent = <TextInput style={{flex: 1, textAlignVertical: 'top', fontFamily: 'monospace'}} multiline={true} value={note.body} onChangeText={(text) => this.body_changeText(text)} /> bodyComponent = <TextInput style={{flex: 1, textAlignVertical: 'top', fontFamily: 'monospace'}} multiline={true} value={note.body} onChangeText={(text) => this.body_changeText(text)} />
} }
console.info(this.state.noteMetadata);
return ( return (
<View style={{flex: 1}}> <View style={{flex: 1}}>
<ScreenHeader navState={this.props.navigation.state} menuOptions={this.menuOptions()} /> <ScreenHeader navState={this.props.navigation.state} menuOptions={this.menuOptions()} />
@ -108,6 +129,7 @@ class NoteScreenComponent extends React.Component {
{ bodyComponent } { bodyComponent }
{ todoComponents } { todoComponents }
<Button title="Save note" onPress={() => this.saveNoteButton_press()} /> <Button title="Save note" onPress={() => this.saveNoteButton_press()} />
{ this.state.showNoteMetadata && <Text>{this.state.noteMetadata}</Text> }
</View> </View>
); );
} }

View File

@ -28,7 +28,7 @@ class NotesScreenUtils {
} else { } else {
this.dispatch({ this.dispatch({
type: 'Navigation/NAVIGATE', type: 'Navigation/NAVIGATE',
routeName: 'Loading', routeName: 'Welcome',
}); });
} }
} }

View File

@ -5,7 +5,7 @@ import { Log } from 'lib/log.js'
import { ScreenHeader } from 'lib/components/screen-header.js'; import { ScreenHeader } from 'lib/components/screen-header.js';
import { ActionButton } from 'lib/components/action-button.js'; import { ActionButton } from 'lib/components/action-button.js';
class LoadingScreenComponent extends React.Component { class WelcomeScreenComponent extends React.Component {
static navigationOptions(options) { static navigationOptions(options) {
return { header: null }; return { header: null };
@ -31,12 +31,12 @@ class LoadingScreenComponent extends React.Component {
} }
const LoadingScreen = connect( const WelcomeScreen = connect(
(state) => { (state) => {
return { return {
loading: state.loading, loading: state.loading,
}; };
} }
)(LoadingScreenComponent) )(WelcomeScreenComponent)
export { LoadingScreen }; export { WelcomeScreen };

View File

@ -213,8 +213,15 @@ class Database {
} }
if (typeof where != 'string') { if (typeof where != 'string') {
params.push(where.id); let s = [];
where = 'id=?'; for (let n in where) {
if (!where.hasOwnProperty(n)) continue;
params.push(where[n]);
s.push('`' + n + '`=?');
}
where = s.join(' AND ');
// params.push(where.id);
// where = 'id=?';
} }
return { return {

View File

@ -177,7 +177,7 @@ class BaseItem extends BaseModel {
} }
static serialize_format(propName, propValue) { static serialize_format(propName, propValue) {
if (['created_time', 'updated_time'].indexOf(propName) >= 0) { if (['created_time', 'updated_time', 'sync_time'].indexOf(propName) >= 0) {
if (!propValue) return ''; if (!propValue) return '';
propValue = moment.unix(propValue / 1000).utc().format('YYYY-MM-DDTHH:mm:ss.SSS') + 'Z'; propValue = moment.unix(propValue / 1000).utc().format('YYYY-MM-DDTHH:mm:ss.SSS') + 'Z';
} else if (propValue === null || propValue === undefined) { } else if (propValue === null || propValue === undefined) {
@ -207,18 +207,20 @@ class BaseItem extends BaseModel {
let output = []; let output = [];
if ('title' in item) { if ('title' in item && shownKeys.indexOf('title') >= 0) {
output.push(item.title); output.push(item.title);
output.push(''); output.push('');
} }
if ('body' in item) { if ('body' in item && shownKeys.indexOf('body') >= 0) {
output.push(item.body); output.push(item.body);
if (shownKeys.length) output.push(''); if (shownKeys.length) output.push('');
} }
for (let i = 0; i < shownKeys.length; i++) { for (let i = 0; i < shownKeys.length; i++) {
let key = shownKeys[i]; let key = shownKeys[i];
if (key == 'title' || key == 'body') continue;
let value = null; let value = null;
if (typeof key === 'function') { if (typeof key === 'function') {
let r = await key(); let r = await key();

View File

@ -3,6 +3,7 @@ import { Log } from 'lib/log.js';
import { promiseChain } from 'lib/promise-utils.js'; import { promiseChain } from 'lib/promise-utils.js';
import { Note } from 'lib/models/note.js'; import { Note } from 'lib/models/note.js';
import { Setting } from 'lib/models/setting.js'; import { Setting } from 'lib/models/setting.js';
import { Database } from 'lib/database.js';
import { _ } from 'lib/locale.js'; import { _ } from 'lib/locale.js';
import moment from 'moment'; import moment from 'moment';
import { BaseItem } from 'lib/models/base-item.js'; import { BaseItem } from 'lib/models/base-item.js';
@ -48,14 +49,24 @@ class Folder extends BaseItem {
return r ? r.total : 0; return r ? r.total : 0;
} }
static async delete(folderId, options = null) { static markNotesAsConflict(parentId) {
let folder = await Folder.load(folderId); let query = Database.updateQuery('notes', { is_conflict: 1 }, { parent_id: parentId });
if (!folder) throw new Error('Trying to delete non-existing notebook: ' + folderId); return this.db().exec(query);
}
static async delete(folderId, options = null) {
if (!options) options = {};
if (!('deleteChildren' in options)) options.deleteChildren = true;
let folder = await Folder.load(folderId);
if (!folder) return; // noop
if (options.deleteChildren) {
let noteIds = await Folder.noteIds(folderId); let noteIds = await Folder.noteIds(folderId);
for (let i = 0; i < noteIds.length; i++) { for (let i = 0; i < noteIds.length; i++) {
await Note.delete(noteIds[i]); await Note.delete(noteIds[i]);
} }
}
await super.delete(folderId, options); await super.delete(folderId, options);

View File

@ -17,12 +17,12 @@ class Note extends BaseItem {
static async serialize(note, type = null, shownKeys = null) { static async serialize(note, type = null, shownKeys = null) {
let fieldNames = this.fieldNames(); let fieldNames = this.fieldNames();
fieldNames.push('type_'); fieldNames.push('type_');
lodash.pull(fieldNames, 'is_conflict', 'sync_time', 'body'); // Exclude 'body' since it's going to be added separately at the top of the note lodash.pull(fieldNames, 'is_conflict', 'sync_time');
return super.serialize(note, 'note', fieldNames); return super.serialize(note, 'note', fieldNames);
} }
static async serializeForEdit(note) { static async serializeForEdit(note) {
return super.serialize(note, 'note', []); return super.serialize(note, 'note', ['title', 'body']);
} }
static async unserializeForEdit(content) { static async unserializeForEdit(content) {
@ -30,6 +30,13 @@ class Note extends BaseItem {
return super.unserialize(content); return super.unserialize(content);
} }
static async serializeAllProps(note) {
let fieldNames = this.fieldNames();
fieldNames.push('type_');
lodash.pull(fieldNames, 'title', 'body');
return super.serialize(note, 'note', fieldNames);
}
static modelType() { static modelType() {
return BaseModel.TYPE_NOTE; return BaseModel.TYPE_NOTE;
} }

View File

@ -224,10 +224,13 @@ class Synchronizer {
// await this.api().move(tempPath, path); // await this.api().move(tempPath, path);
await this.api().put(path, content); await this.api().put(path, content);
await this.api().setTimestamp(path, local.updated_time);
if (this.randomFailure(options, 0)) return; if (this.randomFailure(options, 0)) return;
await this.api().setTimestamp(path, local.updated_time);
if (this.randomFailure(options, 1)) return;
await ItemClass.save({ id: local.id, sync_time: time.unixMs(), type_: local.type_ }, { autoTimestamp: false }); await ItemClass.save({ id: local.id, sync_time: time.unixMs(), type_: local.type_ }, { autoTimestamp: false });
} else if (action == 'itemConflict') { } else if (action == 'itemConflict') {
@ -251,7 +254,7 @@ class Synchronizer {
conflictedNote.is_conflict = 1; conflictedNote.is_conflict = 1;
await Note.save(conflictedNote, { autoTimestamp: false }); await Note.save(conflictedNote, { autoTimestamp: false });
if (this.randomFailure(options, 1)) return; if (this.randomFailure(options, 2)) return;
if (remote) { if (remote) {
let remoteContent = await this.api().get(path); let remoteContent = await this.api().get(path);
@ -289,7 +292,7 @@ class Synchronizer {
let path = BaseItem.systemPath(item.item_id) let path = BaseItem.systemPath(item.item_id)
this.logSyncOperation('deleteRemote', null, { id: item.item_id }, 'local has been deleted'); this.logSyncOperation('deleteRemote', null, { id: item.item_id }, 'local has been deleted');
await this.api().delete(path); await this.api().delete(path);
if (this.randomFailure(options, 2)) return; if (this.randomFailure(options, 3)) return;
await BaseItem.remoteDeletedItem(item.item_id); await BaseItem.remoteDeletedItem(item.item_id);
report['deleteRemote']++; report['deleteRemote']++;
@ -395,6 +398,8 @@ class Synchronizer {
if (this.randomFailure(options, 4)) return; if (this.randomFailure(options, 4)) return;
let localFoldersToDelete = [];
if (!this.cancelling()) { if (!this.cancelling()) {
let items = await BaseItem.syncedItems(); let items = await BaseItem.syncedItems();
for (let i = 0; i < items.length; i++) { for (let i = 0; i < items.length; i++) {
@ -402,6 +407,11 @@ class Synchronizer {
let item = items[i]; let item = items[i];
if (remoteIds.indexOf(item.id) < 0) { if (remoteIds.indexOf(item.id) < 0) {
if (item.type_ == Folder.modelType()) {
localFoldersToDelete.push(item);
continue;
}
report.localsToDelete++; report.localsToDelete++;
options.onProgress(report); options.onProgress(report);
this.logSyncOperation('deleteLocal', { id: item.id }, null, 'remote has been deleted'); this.logSyncOperation('deleteLocal', { id: item.id }, null, 'remote has been deleted');
@ -413,6 +423,19 @@ class Synchronizer {
} }
} }
} }
if (!this.cancelling()) {
for (let i = 0; i < localFoldersToDelete.length; i++) {
const folder = localFoldersToDelete[i];
const noteIds = await Folder.noteIds(folder.id);
if (noteIds.length) { // CONFLICT
await Folder.markNotesAsConflict(folder.id);
await Folder.delete(folder.id, { deleteChildren: false });
} else {
await Folder.delete(folder.id);
}
}
}
} catch (error) { } catch (error) {
report.errors.push(error); report.errors.push(error);
this.logger().error(error); this.logger().error(error);

View File

@ -26,6 +26,10 @@ let time = {
return moment.unix(ms / 1000).format('DD/MM/YYYY HH:mm'); return moment.unix(ms / 1000).format('DD/MM/YYYY HH:mm');
}, },
formatMsToLocal(ms, format) {
return moment.unix(ms / 1000).format(format);
},
msleep(ms) { msleep(ms) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
setTimeout(() => { setTimeout(() => {

View File

@ -23,7 +23,7 @@ import { FolderScreen } from 'lib/components/screens/folder.js'
import { FoldersScreen } from 'lib/components/screens/folders.js' import { FoldersScreen } from 'lib/components/screens/folders.js'
import { LogScreen } from 'lib/components/screens/log.js' import { LogScreen } from 'lib/components/screens/log.js'
import { StatusScreen } from 'lib/components/screens/status.js' import { StatusScreen } from 'lib/components/screens/status.js'
import { LoadingScreen } from 'lib/components/screens/loading.js' import { WelcomeScreen } from 'lib/components/screens/welcome.js'
import { OneDriveLoginScreen } from 'lib/components/screens/onedrive-login.js' import { OneDriveLoginScreen } from 'lib/components/screens/onedrive-login.js'
import { Setting } from 'lib/models/setting.js' import { Setting } from 'lib/models/setting.js'
import { Synchronizer } from 'lib/synchronizer.js' import { Synchronizer } from 'lib/synchronizer.js'
@ -212,7 +212,7 @@ const AppNavigator = StackNavigator({
Notes: { screen: NotesScreen }, Notes: { screen: NotesScreen },
Note: { screen: NoteScreen }, Note: { screen: NoteScreen },
Folder: { screen: FolderScreen }, Folder: { screen: FolderScreen },
Loading: { screen: LoadingScreen }, Welcome: { screen: WelcomeScreen },
OneDriveLogin: { screen: OneDriveLoginScreen }, OneDriveLogin: { screen: OneDriveLoginScreen },
Log: { screen: LogScreen }, Log: { screen: LogScreen },
Status: { screen: StatusScreen }, Status: { screen: StatusScreen },
@ -343,7 +343,7 @@ class AppComponent extends React.Component {
defaultState.nav = AppNavigator.router.getStateForAction({ defaultState.nav = AppNavigator.router.getStateForAction({
type: 'Navigation/NAVIGATE', type: 'Navigation/NAVIGATE',
routeName: 'Loading', routeName: 'Welcome',
params: {} params: {}
}); });