From 96dab5d51e1e098c5f638afa3a8c3dcac96b6cee Mon Sep 17 00:00:00 2001 From: azivner Date: Sun, 1 Apr 2018 17:38:24 -0400 Subject: [PATCH] smaller refactorings continued --- src/entities/api_token.js | 25 +++++++++++ src/entities/branch.js | 8 ++++ src/entities/entity_constructor.js | 4 ++ src/entities/image.js | 8 ++++ src/entities/note.js | 8 ++++ src/entities/note_image.js | 8 ++++ src/entities/note_revision.js | 5 +++ src/routes/api/cloning.js | 10 +---- src/routes/api/labels.js | 1 - src/routes/api/notes.js | 15 +++---- src/routes/api/sender.js | 18 ++------ src/routes/api/sync.js | 2 +- src/services/image.js | 50 ++++++++-------------- src/services/notes.js | 69 +++++++++++------------------- 14 files changed, 122 insertions(+), 109 deletions(-) create mode 100644 src/entities/api_token.js diff --git a/src/entities/api_token.js b/src/entities/api_token.js new file mode 100644 index 000000000..14e80e17b --- /dev/null +++ b/src/entities/api_token.js @@ -0,0 +1,25 @@ +"use strict"; + +const Entity = require('./entity'); +const utils = require('../services/utils'); + +class ApiToken extends Entity { + static get tableName() { return "api_tokens"; } + static get primaryKeyName() { return "apiTokenId"; } + + beforeSaving() { + if (!this.apiTokenId) { + this.apiTokenId = utils.newApiTokenId(); + } + + if (!this.isDeleted) { + this.isDeleted = false; + } + + if (!this.dateCreated) { + this.dateCreated = utils.nowDate(); + } + } +} + +module.exports = ApiToken; \ No newline at end of file diff --git a/src/entities/branch.js b/src/entities/branch.js index 3331b045a..14a48ce95 100644 --- a/src/entities/branch.js +++ b/src/entities/branch.js @@ -13,6 +13,14 @@ class Branch extends Entity { } beforeSaving() { + if (!this.branchId) { + this.branchId = utils.newBranchId(); + } + + if (!this.isDeleted) { + this.isDeleted = false; + } + this.dateModified = utils.nowDate() } } diff --git a/src/entities/entity_constructor.js b/src/entities/entity_constructor.js index af4037f28..7fa3158da 100644 --- a/src/entities/entity_constructor.js +++ b/src/entities/entity_constructor.js @@ -5,6 +5,7 @@ const NoteImage = require('../entities/note_image'); const Branch = require('../entities/branch'); const Label = require('../entities/label'); const RecentNote = require('../entities/recent_note'); +const ApiToken = require('../entities/api_token'); const repository = require('../services/repository'); function createEntityFromRow(row) { @@ -25,6 +26,9 @@ function createEntityFromRow(row) { else if (row.branchId && row.notePath) { entity = new RecentNote(row); } + else if (row.apiTokenId) { + entity = new ApiToken(row); + } else if (row.branchId) { entity = new Branch(row); } diff --git a/src/entities/image.js b/src/entities/image.js index 5116ce2b8..5b1e899b1 100644 --- a/src/entities/image.js +++ b/src/entities/image.js @@ -8,6 +8,14 @@ class Image extends Entity { static get primaryKeyName() { return "imageId"; } beforeSaving() { + if (!this.imageId) { + this.imageId = utils.newImageId(); + } + + if (!this.isDeleted) { + this.isDeleted = false; + } + if (!this.dateCreated) { this.dateCreated = utils.nowDate(); } diff --git a/src/entities/note.js b/src/entities/note.js index bb08bb6a8..2e6a4f283 100644 --- a/src/entities/note.js +++ b/src/entities/note.js @@ -131,6 +131,10 @@ class Note extends Entity { } beforeSaving() { + if (!this.noteId) { + this.noteId = utils.newNoteId(); + } + if (this.isJson()) { this.content = JSON.stringify(this.jsonContent, null, '\t'); } @@ -139,6 +143,10 @@ class Note extends Entity { protected_session.encryptNote(this); } + if (!this.isDeleted) { + this.isDeleted = false; + } + if (!this.dateCreated) { this.dateCreated = utils.nowDate(); } diff --git a/src/entities/note_image.js b/src/entities/note_image.js index 2a4edefa5..944bf6e17 100644 --- a/src/entities/note_image.js +++ b/src/entities/note_image.js @@ -17,6 +17,14 @@ class NoteImage extends Entity { } beforeSaving() { + if (!this.noteImageId) { + this.noteImageId = utils.newNoteImageId(); + } + + if (!this.isDeleted) { + this.isDeleted = false; + } + if (!this.dateCreated) { this.dateCreated = utils.nowDate(); } diff --git a/src/entities/note_revision.js b/src/entities/note_revision.js index f8207c1df..41b2f995c 100644 --- a/src/entities/note_revision.js +++ b/src/entities/note_revision.js @@ -2,6 +2,7 @@ const Entity = require('./entity'); const protected_session = require('../services/protected_session'); +const utils = require('../services/utils'); const repository = require('../services/repository'); class NoteRevision extends Entity { @@ -21,6 +22,10 @@ class NoteRevision extends Entity { } beforeSaving() { + if (!this.noteRevisionId) { + this.noteRevisionId = utils.newNoteRevisionId(); + } + if (this.isProtected) { protected_session.encryptNoteRevision(this); } diff --git a/src/routes/api/cloning.js b/src/routes/api/cloning.js index b32f35d99..d9f59b831 100644 --- a/src/routes/api/cloning.js +++ b/src/routes/api/cloning.js @@ -21,14 +21,11 @@ async function cloneNoteToParent(req) { const newNotePos = maxNotePos === null ? 0 : maxNotePos + 1; const branch = new Branch({ - branchId: utils.newBranchId(), noteId: childNoteId, parentNoteId: parentNoteId, prefix: prefix, notePosition: newNotePos, - isExpanded: 0, - dateModified: utils.nowDate(), - isDeleted: 0 + isExpanded: 0 }); await branch.save(); @@ -58,13 +55,10 @@ async function cloneNoteAfter(req) { await sync_table.addNoteReorderingSync(afterNote.parentNoteId); const branch = new Branch({ - branchId: utils.newBranchId(), noteId: noteId, parentNoteId: afterNote.parentNoteId, notePosition: afterNote.notePosition + 1, - isExpanded: 0, - dateModified: utils.nowDate(), - isDeleted: 0 + isExpanded: 0 }); await branch.save(); diff --git a/src/routes/api/labels.js b/src/routes/api/labels.js index 68e4a042f..073bea483 100644 --- a/src/routes/api/labels.js +++ b/src/routes/api/labels.js @@ -29,7 +29,6 @@ async function updateNoteLabels(req) { } labelEntity = new Label(); - labelEntity.labelId = utils.newLabelId(); labelEntity.noteId = noteId; } diff --git a/src/routes/api/notes.js b/src/routes/api/notes.js index 1c5e03e0f..a77df9667 100644 --- a/src/routes/api/notes.js +++ b/src/routes/api/notes.js @@ -1,10 +1,7 @@ "use strict"; -const sql = require('../../services/sql'); const notes = require('../../services/notes'); -const utils = require('../../services/utils'); const tree = require('../../services/tree'); -const sync_table = require('../../services/sync_table'); const repository = require('../../services/repository'); async function getNote(req) { @@ -57,14 +54,12 @@ async function protectBranch(req) { } async function setNoteTypeMime(req) { - const noteId = req.params[0]; - const type = req.params[1]; - const mime = req.params[2]; + const [noteId, type, mime] = req.params; - await sql.execute("UPDATE notes SET type = ?, mime = ?, dateModified = ? WHERE noteId = ?", - [type, mime, utils.nowDate(), noteId]); - - await sync_table.addNoteSync(noteId); + const note = await repository.getNote(noteId); + note.type = type; + note.mime = mime; + await note.save(); } module.exports = { diff --git a/src/routes/api/sender.js b/src/routes/api/sender.js index fe345ac35..5a709a75a 100644 --- a/src/routes/api/sender.js +++ b/src/routes/api/sender.js @@ -7,7 +7,7 @@ const sql = require('../../services/sql'); const notes = require('../../services/notes'); const password_encryption = require('../../services/password_encryption'); const options = require('../../services/options'); -const sync_table = require('../../services/sync_table'); +const ApiToken = require('../../entities/api_token'); async function login(req) { const username = req.body.username; @@ -20,21 +20,11 @@ async function login(req) { return [401, "Incorrect username/password"]; } - const token = utils.randomSecureToken(); - - const apiTokenId = utils.newApiTokenId(); - - await sql.insert("api_tokens", { - apiTokenId: apiTokenId, - token: token, - dateCreated: utils.nowDate(), - isDeleted: false - }); - - await sync_table.addApiTokenSync(apiTokenId); + const apiToken = new ApiToken({ token: utils.randomSecureToken() }); + await apiToken.save(); return { - token: token + token: apiToken.token }; } diff --git a/src/routes/api/sync.js b/src/routes/api/sync.js index 6441fa603..e8651d41c 100644 --- a/src/routes/api/sync.js +++ b/src/routes/api/sync.js @@ -46,7 +46,7 @@ async function forceNoteSync(req) { } for (const noteRevisionId of await sql.getColumn("SELECT noteRevisionId FROM note_revisions WHERE noteId = ?", [noteId])) { - await sync_table.addNoteRevisionsSync(noteRevisionId); + await sync_table.addNoteRevisionSync(noteRevisionId); } log.info("Forcing note sync for " + noteId); diff --git a/src/services/image.js b/src/services/image.js index 347bd715a..1196f753c 100644 --- a/src/services/image.js +++ b/src/services/image.js @@ -1,8 +1,8 @@ "use strict"; const utils = require('./utils'); -const sql = require('./sql'); -const sync_table = require('./sync_table'); +const Image = require('../entities/image'); +const NoteImage = require('../entities/note_image'); const imagemin = require('imagemin'); const imageminMozJpeg = require('imagemin-mozjpeg'); const imageminPngQuant = require('imagemin-pngquant'); @@ -20,37 +20,23 @@ async function saveImage(file, noteId) { const fileNameWithouExtension = file.originalname.replace(/\.[^/.]+$/, ""); const fileName = sanitizeFilename(fileNameWithouExtension + "." + imageFormat.ext); - const imageId = utils.newImageId(); - const now = utils.nowDate(); - - await sql.doInTransaction(async () => { - await sql.insert("images", { - imageId: imageId, - format: imageFormat.ext, - name: fileName, - checksum: utils.hash(optimizedImage), - data: optimizedImage, - isDeleted: 0, - dateModified: now, - dateCreated: now - }); - - await sync_table.addImageSync(imageId); - - const noteImageId = utils.newNoteImageId(); - - await sql.insert("note_images", { - noteImageId: noteImageId, - noteId: noteId, - imageId: imageId, - isDeleted: 0, - dateModified: now, - dateCreated: now - }); - - await sync_table.addNoteImageSync(noteImageId); + const image = new Image({ + format: imageFormat.ext, + name: fileName, + checksum: utils.hash(optimizedImage), + data: optimizedImage }); - return {fileName, imageId}; + + await image.save(); + + const noteImage = new NoteImage({ + noteId: noteId, + imageId: image.imageId + }); + + await noteImage.save(); + + return {fileName, imageId: image.imageId}; } const MAX_SIZE = 1000; diff --git a/src/services/notes.js b/src/services/notes.js index bd38daaf0..856e10d41 100644 --- a/src/services/notes.js +++ b/src/services/notes.js @@ -9,16 +9,16 @@ const NoteImage = require('../entities/note_image'); const NoteRevision = require('../entities/note_revision'); const Branch = require('../entities/branch'); -async function getNewNotePosition(parentNoteId, noteOpts) { +async function getNewNotePosition(parentNoteId, noteData) { let newNotePos = 0; - if (noteOpts.target === 'into') { + if (noteData.target === 'into') { const maxNotePos = await sql.getValue('SELECT MAX(notePosition) FROM branches WHERE parentNoteId = ? AND isDeleted = 0', [parentNoteId]); newNotePos = maxNotePos === null ? 0 : maxNotePos + 1; } - else if (noteOpts.target === 'after') { - const afterNote = await sql.getRow('SELECT notePosition FROM branches WHERE branchId = ?', [noteOpts.target_branchId]); + else if (noteData.target === 'after') { + const afterNote = await sql.getRow('SELECT notePosition FROM branches WHERE branchId = ?', [noteData.target_branchId]); newNotePos = afterNote.notePosition + 1; @@ -29,44 +29,36 @@ async function getNewNotePosition(parentNoteId, noteOpts) { await sync_table.addNoteReorderingSync(parentNoteId); } else { - throw new Error('Unknown target: ' + noteOpts.target); + throw new Error('Unknown target: ' + noteData.target); } return newNotePos; } -async function createNewNote(parentNoteId, noteOpts) { - const newNotePos = await getNewNotePosition(parentNoteId, noteOpts); +async function createNewNote(parentNoteId, noteData) { + const newNotePos = await getNewNotePosition(parentNoteId, noteData); if (parentNoteId !== 'root') { const parent = await repository.getNote(parentNoteId); - if (!noteOpts.type) { - noteOpts.type = parent.type; - } - - if (!noteOpts.mime) { - noteOpts.mime = parent.mime; - } + noteData.type = noteData.type || parent.type; + noteData.mime = noteData.mime || parent.mime; } const note = new Note({ - noteId: utils.newNoteId(), - title: noteOpts.title, - content: noteOpts.content ? noteOpts.content : '', - isProtected: noteOpts.isProtected, - type: noteOpts.type ? noteOpts.type : 'text', - mime: noteOpts.mime ? noteOpts.mime : 'text/html' + title: noteData.title, + content: noteData.content || '', + isProtected: noteData.isProtected, + type: noteData.type || 'text', + mime: noteData.mime || 'text/html' }); await note.save(); const branch = new Branch({ - branchId: utils.newBranchId(), noteId: note.noteId, parentNoteId: parentNoteId, notePosition: newNotePos, - isExpanded: 0, - isDeleted: 0 + isExpanded: 0 }); await branch.save(); @@ -85,7 +77,7 @@ async function createNote(parentNoteId, title, content = "", extraOptions = {}) title: title, content: extraOptions.json ? JSON.stringify(content, null, '\t') : content, target: 'into', - isProtected: extraOptions.isProtected !== undefined ? extraOptions.isProtected : false, + isProtected: !!extraOptions.isProtected, type: extraOptions.type, mime: extraOptions.mime }; @@ -95,11 +87,6 @@ async function createNote(parentNoteId, title, content = "", extraOptions = {}) noteData.mime = "application/json"; } - if (!noteData.type) { - noteData.type = "text"; - noteData.mime = "text/html"; - } - const {note} = await createNewNote(parentNoteId, noteData); if (extraOptions.labels) { @@ -123,7 +110,7 @@ async function protectNote(note, protect) { if (protect !== note.isProtected) { note.isProtected = protect; - await repository.updateEntity(note); + await note.save(); } await protectNoteRevisions(note); @@ -134,7 +121,7 @@ async function protectNoteRevisions(note) { if (note.isProtected !== revision.isProtected) { revision.isProtected = note.isProtected; - await repository.updateEntity(revision); + await revision.save(); } } } @@ -154,12 +141,10 @@ async function saveNoteImages(note) { const existingNoteImage = existingNoteImages.find(ni => ni.imageId === imageId); if (!existingNoteImage) { - await repository.updateEntity(new NoteImage({ - noteImageId: utils.newNoteImageId(), + await (new NoteImage({ noteId: note.noteId, - imageId: imageId, - isDeleted: 0 - })); + imageId: imageId + })).save(); } // else we don't need to do anything @@ -170,9 +155,9 @@ async function saveNoteImages(note) { const unusedNoteImages = existingNoteImages.filter(ni => !foundImageIds.includes(ni.imageId)); for (const unusedNoteImage of unusedNoteImages) { - unusedNoteImage.isDeleted = 1; + unusedNoteImage.isDeleted = true; - await repository.updateEntity(unusedNoteImage); + await unusedNoteImage.save(); } } @@ -194,8 +179,7 @@ async function saveNoteRevision(note) { && !existingnoteRevisionId && msSinceDateCreated >= noteRevisionSnapshotTimeInterval * 1000) { - await repository.updateEntity(new NoteRevision({ - noteRevisionId: utils.newNoteRevisionId(), + await (new NoteRevision({ noteId: note.noteId, // title and text should be decrypted now title: note.title, @@ -203,7 +187,7 @@ async function saveNoteRevision(note) { isProtected: 0, // will be fixed in the protectNoteRevisions() call dateModifiedFrom: note.dateModified, dateModifiedTo: utils.nowDate() - })); + })).save(); } } @@ -220,8 +204,7 @@ async function updateNote(noteId, noteUpdates) { note.title = noteUpdates.title; note.content = noteUpdates.content; note.isProtected = noteUpdates.isProtected; - - await repository.updateEntity(note); + await note.save(); await saveNoteImages(note);