Skip to content

Commit

Permalink
Merge pull request #131 from codex-team/new-merge-root-folder
Browse files Browse the repository at this point in the history
Merge local Root Folder with Root Folder from the Cloud
  • Loading branch information
talyguryn authored Feb 5, 2018
2 parents 5b7dc52 + a6fb7e6 commit 2b21eb0
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 37 deletions.
16 changes: 14 additions & 2 deletions src/codex-notes.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class CodexNotes {
*/
this.appProtocol = 'codex';

this.mainWindow = new BrowserWindow({
let windowParams = {
title: pkg.productName,
icon: __dirname + '/' + pkg.productIconPNG,
width: 1200,
Expand All @@ -95,7 +95,19 @@ class CodexNotes {
backgroundColor: '#fff',
titleBarStyle: 'hiddenInset',
show: false
});
}

/**
* Show small half screen window for debugging
*/
if (process.env.DEBUG === 'true') {
windowParams.x = 10;
windowParams.y = 50;
windowParams.width = 740;
windowParams.minWidth = 600;
}

this.mainWindow = new BrowserWindow(windowParams);

/**
* @todo make crossplaform menu
Expand Down
1 change: 0 additions & 1 deletion src/controllers/folders.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ let {ipcMain} = require('electron');

const Folder = require('../models/folder');
const FoldersList = require('../models/foldersList');
const NotesList = require('../models/foldersList');

/**
* Time helper
Expand Down
4 changes: 1 addition & 3 deletions src/controllers/notes.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,7 @@ class NotesController {
*/
async loadNotesList(folderId, event) {
try {
let list = new NotesList({
folderId
});
let list = new NotesList(folderId);
let notesInFolder = await list.get();

let returnValue = {
Expand Down
68 changes: 63 additions & 5 deletions src/models/folder.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
const db = require('../utils/database');
const utils = require('../utils/utils');

const NotesList = require('./notesList');

/**
* Time helper
*/
Expand Down Expand Up @@ -130,18 +132,38 @@ class Folder {
let folderFromLocalDB = await db.findOne(db.FOLDERS, {_id: this._id});

/**
* 2. If we do not have this Folder in local DB
* 2. If we do not have this Folder in local DB.
*
* If this Folder is Root then merge it with local Root Folder
*/
if (!folderFromLocalDB) {
return await this.createItemFromCloud();
/**
* Get current Root Folder's _id
*/
let currentRootFolderId = await db.getRootFolderId();

/**
* Save new Folder
*/
await this.createItemFromCloud();

/**
* If it is a Root Folder from the Cloud then
* merge it with local Root Folder
*/
if (this.isRoot && this._id !== currentRootFolderId) {
await this.updateRootFolder(currentRootFolderId);
}

return this.data;
}

/**
* 3. We need to update Folder if new dtModify
* is greater than item's dtModify from DB
*/
if (folderFromLocalDB.dtModify < this.dtModify) {
await this.saveUpdatedItem();
return await this.saveUpdatedItem();
}

/**
Expand Down Expand Up @@ -212,6 +234,43 @@ class Folder {
return this.data;
}

/**
* Have got a new Root Folder item then
* - set new folderId (new Root Folder) for all note in local Root
* - remove old Root Folder
*
* @param {String} currentRootFolderId
*
* @returns {Promise<FolderData>}
*/
async updateRootFolder(currentRootFolderId) {
let rootFolderNotesListModel = new NotesList(currentRootFolderId),
rootFolderNotesList = await rootFolderNotesListModel.get();

/**
* Update folderId for all current Root Folder notes
*
* These notes are not in the Cloud yet then
* we will have no problem with syncing
*/
await Promise.all(rootFolderNotesList.map( async note => {
try {
note.folderId = this._id;
note.dtModify = Time.now;
return await note.save();
} catch (e) {
console.log('Error while moving notes to the new Root Folder:', e);
}
}));

/**
* Remove from local DB not synced old Root Folder
*/
let removeOldRootFodlerResult = await db.remove(db.FOLDERS, {_id: currentRootFolderId}, {});

return this.data;
}

/**
* Need to update local item
*
Expand Down Expand Up @@ -255,7 +314,6 @@ class Folder {
return await this.save();
}


/**
* Get Folder by ID
*
Expand Down Expand Up @@ -301,4 +359,4 @@ class Folder {
}
}

module.exports = Folder;
module.exports = Folder;
5 changes: 3 additions & 2 deletions src/models/foldersList.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const Folder = require('./folder.js');
* @property {Object} query - search query to compose Folders List
* @type {module.FoldersList}
*/
module.exports = class FoldersList {
class FoldersList {

constructor(query) {
this.query = query || {
Expand All @@ -25,5 +25,6 @@ module.exports = class FoldersList {

return foldersList.map( folder => new Folder(folder));
}
}

};
module.exports = FoldersList;
38 changes: 19 additions & 19 deletions src/models/note.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,8 @@ const sanitizeHtml = require('sanitize-html');
* @type {Database}
*/
const db = require('../utils/database');

const utils = require('../utils/utils');

const Folder = require('./folder');

/**
* Time helper
*/
Expand Down Expand Up @@ -174,7 +171,7 @@ class Note {
* is greater than item's dtModify from DB
*/
if (noteFromLocalDB.dtModify < this.dtModify) {
await this.saveUpdatedItem();
return await this.saveUpdatedItem();
}

/**
Expand Down Expand Up @@ -279,27 +276,30 @@ class Note {
/**
* Update dtModify of parent Folder
*
* @param currentTimestamp
* @param timestamp
*
* @returns {Promise<FolderData>}
* @returns {Promise<Object>}
*/
async updateFolderModifyDate(currentTimestamp) {
/**
* Create Folder's model
*
* @type {Folder}
*/
let folder = await Folder.get(this.folderId);
async updateFolderModifyDate(timestamp) {
let query = {
_id: this.folderId,
dtModify: {$lt: timestamp}
},
data = {
$set: {dtModify: timestamp}
},
options = {
returnUpdatedDocs: true
};

/**
* Update Folder's timestamp
* Update parent Folder's dtModify it is less than the target timestamp
*
* @type {{numAffected: number, affectedDocuments: Object|null}}
*/
folder.dtModify = currentTimestamp;
let updatedFolder = await db.update(db.FOLDERS, query, data, options);

/**
* Try to save Folder
*/
return await folder.save();
return updatedFolder.affectedDocuments;
}

/**
Expand Down
10 changes: 5 additions & 5 deletions src/models/notesList.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ const Note = require('./note.js');
* @typedef {NotesList} NotesList
* @property {string} folderId - in which Folder we should to find Notes
*/
module.exports = class NotesList {
class NotesList {

/**
* @param {string} folderId - from which Folder we need to create Notes List
*/
constructor({folderId}) {
constructor(folderId) {
this.folderId = folderId;
}

Expand All @@ -20,8 +20,7 @@ module.exports = class NotesList {
* @return {Promise.<Note[]>}
*/
async get() {

if (!this.folderId){
if (!this.folderId) {
this.folderId = await db.getRootFolderId();
}

Expand All @@ -34,5 +33,6 @@ module.exports = class NotesList {

return notesList.map( note => new Note(note));
}
}

};
module.exports = NotesList;

0 comments on commit 2b21eb0

Please sign in to comment.