mirror of
				https://github.com/zadam/trilium.git
				synced 2025-10-31 18:36:30 +01:00 
			
		
		
		
	removed sourceId where it's not necessary (stored in CLS instead)
This commit is contained in:
		| @@ -38,8 +38,6 @@ async function cleanupSoftDeletedItems() { | |||||||
| } | } | ||||||
|  |  | ||||||
| async function cleanupUnusedImages() { | async function cleanupUnusedImages() { | ||||||
|     const sourceId = req.headers.source_id; |  | ||||||
|  |  | ||||||
|     const unusedImageIds = await sql.getColumn(` |     const unusedImageIds = await sql.getColumn(` | ||||||
|       SELECT images.imageId  |       SELECT images.imageId  | ||||||
|       FROM images  |       FROM images  | ||||||
| @@ -56,7 +54,7 @@ async function cleanupUnusedImages() { | |||||||
|         await sql.execute("UPDATE images SET isDeleted = 1, data = null, dateModified = ? WHERE imageId = ?", |         await sql.execute("UPDATE images SET isDeleted = 1, data = null, dateModified = ? WHERE imageId = ?", | ||||||
|             [now, imageId]); |             [now, imageId]); | ||||||
|  |  | ||||||
|         await sync_table.addImageSync(imageId, sourceId); |         await sync_table.addImageSync(imageId); | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -9,7 +9,6 @@ async function cloneNoteToParent(req) { | |||||||
|     const parentNoteId = req.params.parentNoteId; |     const parentNoteId = req.params.parentNoteId; | ||||||
|     const childNoteId = req.params.childNoteId; |     const childNoteId = req.params.childNoteId; | ||||||
|     const prefix = req.body.prefix; |     const prefix = req.body.prefix; | ||||||
|     const sourceId = req.headers.source_id; |  | ||||||
|  |  | ||||||
|     const validationResult = await tree.validateParentChild(parentNoteId, childNoteId); |     const validationResult = await tree.validateParentChild(parentNoteId, childNoteId); | ||||||
|  |  | ||||||
| @@ -33,7 +32,7 @@ async function cloneNoteToParent(req) { | |||||||
|  |  | ||||||
|     await sql.replace("branches", branch); |     await sql.replace("branches", branch); | ||||||
|  |  | ||||||
|     await sync_table.addBranchSync(branch.branchId, sourceId); |     await sync_table.addBranchSync(branch.branchId); | ||||||
|  |  | ||||||
|     await sql.execute("UPDATE branches SET isExpanded = 1 WHERE noteId = ?", [parentNoteId]); |     await sql.execute("UPDATE branches SET isExpanded = 1 WHERE noteId = ?", [parentNoteId]); | ||||||
|  |  | ||||||
| @@ -43,7 +42,6 @@ async function cloneNoteToParent(req) { | |||||||
| async function cloneNoteAfter(req) { | async function cloneNoteAfter(req) { | ||||||
|     const noteId = req.params.noteId; |     const noteId = req.params.noteId; | ||||||
|     const afterBranchId = req.params.afterBranchId; |     const afterBranchId = req.params.afterBranchId; | ||||||
|     const sourceId = req.headers.source_id; |  | ||||||
|  |  | ||||||
|     const afterNote = await tree.getBranch(afterBranchId); |     const afterNote = await tree.getBranch(afterBranchId); | ||||||
|  |  | ||||||
| @@ -58,7 +56,7 @@ async function cloneNoteAfter(req) { | |||||||
|     await sql.execute("UPDATE branches SET notePosition = notePosition + 1 WHERE parentNoteId = ? AND notePosition > ? AND isDeleted = 0", |     await sql.execute("UPDATE branches SET notePosition = notePosition + 1 WHERE parentNoteId = ? AND notePosition > ? AND isDeleted = 0", | ||||||
|         [afterNote.parentNoteId, afterNote.notePosition]); |         [afterNote.parentNoteId, afterNote.notePosition]); | ||||||
|  |  | ||||||
|     await sync_table.addNoteReorderingSync(afterNote.parentNoteId, sourceId); |     await sync_table.addNoteReorderingSync(afterNote.parentNoteId); | ||||||
|  |  | ||||||
|     const branch = { |     const branch = { | ||||||
|         branchId: utils.newBranchId(), |         branchId: utils.newBranchId(), | ||||||
| @@ -72,7 +70,7 @@ async function cloneNoteAfter(req) { | |||||||
|  |  | ||||||
|     await sql.replace("branches", branch); |     await sql.replace("branches", branch); | ||||||
|  |  | ||||||
|     await sync_table.addBranchSync(branch.branchId, sourceId); |     await sync_table.addBranchSync(branch.branchId); | ||||||
|  |  | ||||||
|     return { success: true }; |     return { success: true }; | ||||||
| } | } | ||||||
|   | |||||||
| @@ -6,7 +6,6 @@ const labels = require('../../services/labels'); | |||||||
| const protected_session = require('../../services/protected_session'); | const protected_session = require('../../services/protected_session'); | ||||||
|  |  | ||||||
| async function uploadFile(req) { | async function uploadFile(req) { | ||||||
|     const sourceId = req.headers.source_id; |  | ||||||
|     const parentNoteId = req.params.parentNoteId; |     const parentNoteId = req.params.parentNoteId; | ||||||
|     const file = req.file; |     const file = req.file; | ||||||
|     const originalName = file.originalname; |     const originalName = file.originalname; | ||||||
| @@ -25,10 +24,10 @@ async function uploadFile(req) { | |||||||
|         isProtected: false, |         isProtected: false, | ||||||
|         type: 'file', |         type: 'file', | ||||||
|         mime: file.mimetype |         mime: file.mimetype | ||||||
|     }, req, sourceId); |     }, req); | ||||||
|  |  | ||||||
|     await labels.createLabel(noteId, "original_file_name", originalName, sourceId); |     await labels.createLabel(noteId, "original_file_name", originalName); | ||||||
|     await labels.createLabel(noteId, "file_size", size, sourceId); |     await labels.createLabel(noteId, "file_size", size); | ||||||
|  |  | ||||||
|     return { |     return { | ||||||
|         noteId: noteId |         noteId: noteId | ||||||
|   | |||||||
| @@ -22,7 +22,6 @@ async function returnImage(req, res) { | |||||||
| } | } | ||||||
|  |  | ||||||
| async function uploadImage(req) { | async function uploadImage(req) { | ||||||
|     const sourceId = req.headers.source_id; |  | ||||||
|     const noteId = req.query.noteId; |     const noteId = req.query.noteId; | ||||||
|     const file = req.file; |     const file = req.file; | ||||||
|  |  | ||||||
| @@ -36,7 +35,7 @@ async function uploadImage(req) { | |||||||
|         return [400, "Unknown image type: " + file.mimetype]; |         return [400, "Unknown image type: " + file.mimetype]; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     const {fileName, imageId} = await image.saveImage(file, sourceId, noteId); |     const {fileName, imageId} = await image.saveImage(file, noteId); | ||||||
|  |  | ||||||
|     return { |     return { | ||||||
|         uploaded: true, |         uploaded: true, | ||||||
|   | |||||||
| @@ -91,7 +91,6 @@ async function parseImportFile(file) { | |||||||
| } | } | ||||||
|  |  | ||||||
| async function importTar(req) { | async function importTar(req) { | ||||||
|     const sourceId = req.headers.source_id; |  | ||||||
|     const parentNoteId = req.params.parentNoteId; |     const parentNoteId = req.params.parentNoteId; | ||||||
|     const file = req.file; |     const file = req.file; | ||||||
|  |  | ||||||
| @@ -103,10 +102,10 @@ async function importTar(req) { | |||||||
|  |  | ||||||
|     const files = await parseImportFile(file); |     const files = await parseImportFile(file); | ||||||
|  |  | ||||||
|     await importNotes(files, parentNoteId, sourceId); |     await importNotes(files, parentNoteId); | ||||||
| } | } | ||||||
|  |  | ||||||
| async function importNotes(files, parentNoteId, sourceId) { | async function importNotes(files, parentNoteId) { | ||||||
|     for (const file of files) { |     for (const file of files) { | ||||||
|         if (file.meta.version !== 1) { |         if (file.meta.version !== 1) { | ||||||
|             throw new Error("Can't read meta data version " + file.meta.version); |             throw new Error("Can't read meta data version " + file.meta.version); | ||||||
| @@ -118,8 +117,7 @@ async function importNotes(files, parentNoteId, sourceId) { | |||||||
|  |  | ||||||
|         const noteId = await notes.createNote(parentNoteId, file.meta.title, file.data, { |         const noteId = await notes.createNote(parentNoteId, file.meta.title, file.data, { | ||||||
|             type: file.meta.type, |             type: file.meta.type, | ||||||
|             mime: file.meta.mime, |             mime: file.meta.mime | ||||||
|             sourceId: sourceId |  | ||||||
|         }); |         }); | ||||||
|  |  | ||||||
|         for (const attr of file.meta.labels) { |         for (const attr of file.meta.labels) { | ||||||
| @@ -127,7 +125,7 @@ async function importNotes(files, parentNoteId, sourceId) { | |||||||
|         } |         } | ||||||
|  |  | ||||||
|         if (file.children.length > 0) { |         if (file.children.length > 0) { | ||||||
|             await importNotes(file.children, noteId, sourceId); |             await importNotes(file.children, noteId); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -31,11 +31,10 @@ async function getNote(req) { | |||||||
| } | } | ||||||
|  |  | ||||||
| async function createNote(req) { | async function createNote(req) { | ||||||
|     const sourceId = req.headers.source_id; |  | ||||||
|     const parentNoteId = req.params.parentNoteId; |     const parentNoteId = req.params.parentNoteId; | ||||||
|     const newNote = req.body; |     const newNote = req.body; | ||||||
|  |  | ||||||
|     const { noteId, branchId, note } = await notes.createNewNote(parentNoteId, newNote, req, sourceId); |     const { noteId, branchId, note } = await notes.createNewNote(parentNoteId, newNote, req); | ||||||
|  |  | ||||||
|     return { |     return { | ||||||
|         'noteId': noteId, |         'noteId': noteId, | ||||||
| @@ -47,39 +46,35 @@ async function createNote(req) { | |||||||
| async function updateNote(req) { | async function updateNote(req) { | ||||||
|     const note = req.body; |     const note = req.body; | ||||||
|     const noteId = req.params.noteId; |     const noteId = req.params.noteId; | ||||||
|     const sourceId = req.headers.source_id; |  | ||||||
|     const dataKey = protected_session.getDataKey(req); |     const dataKey = protected_session.getDataKey(req); | ||||||
|  |  | ||||||
|     await notes.updateNote(noteId, note, dataKey, sourceId); |     await notes.updateNote(noteId, note, dataKey); | ||||||
| } | } | ||||||
|  |  | ||||||
| async function sortNotes(req) { | async function sortNotes(req) { | ||||||
|     const noteId = req.params.noteId; |     const noteId = req.params.noteId; | ||||||
|     const sourceId = req.headers.source_id; |  | ||||||
|     const dataKey = protected_session.getDataKey(req); |     const dataKey = protected_session.getDataKey(req); | ||||||
|  |  | ||||||
|     await tree.sortNotesAlphabetically(noteId, dataKey, sourceId); |     await tree.sortNotesAlphabetically(noteId, dataKey); | ||||||
| } | } | ||||||
|  |  | ||||||
| async function protectBranch(req) { | async function protectBranch(req) { | ||||||
|     const noteId = req.params.noteId; |     const noteId = req.params.noteId; | ||||||
|     const isProtected = !!parseInt(req.params.isProtected); |     const isProtected = !!parseInt(req.params.isProtected); | ||||||
|     const dataKey = protected_session.getDataKey(req); |     const dataKey = protected_session.getDataKey(req); | ||||||
|     const sourceId = req.headers.source_id; |  | ||||||
|  |  | ||||||
|     await notes.protectNoteRecursively(noteId, dataKey, isProtected, sourceId); |     await notes.protectNoteRecursively(noteId, dataKey, isProtected); | ||||||
| } | } | ||||||
|  |  | ||||||
| async function setNoteTypeMime(req) { | async function setNoteTypeMime(req) { | ||||||
|     const noteId = req.params[0]; |     const noteId = req.params[0]; | ||||||
|     const type = req.params[1]; |     const type = req.params[1]; | ||||||
|     const mime = req.params[2]; |     const mime = req.params[2]; | ||||||
|     const sourceId = req.headers.source_id; |  | ||||||
|  |  | ||||||
|     await sql.execute("UPDATE notes SET type = ?, mime = ?, dateModified = ? WHERE noteId = ?", |     await sql.execute("UPDATE notes SET type = ?, mime = ?, dateModified = ? WHERE noteId = ?", | ||||||
|         [type, mime, utils.nowDate(), noteId]); |         [type, mime, utils.nowDate(), noteId]); | ||||||
|  |  | ||||||
|     await sync_table.addNoteSync(noteId, sourceId); |     await sync_table.addNoteSync(noteId); | ||||||
| } | } | ||||||
|  |  | ||||||
| module.exports = { | module.exports = { | ||||||
|   | |||||||
| @@ -24,7 +24,6 @@ async function getRecentNotes() { | |||||||
| async function addRecentNote(req) { | async function addRecentNote(req) { | ||||||
|     const branchId = req.params.branchId; |     const branchId = req.params.branchId; | ||||||
|     const notePath = req.params.notePath; |     const notePath = req.params.notePath; | ||||||
|     const sourceId = req.headers.source_id; |  | ||||||
|  |  | ||||||
|     await sql.replace('recent_notes', { |     await sql.replace('recent_notes', { | ||||||
|         branchId: branchId, |         branchId: branchId, | ||||||
| @@ -33,9 +32,9 @@ async function addRecentNote(req) { | |||||||
|         isDeleted: 0 |         isDeleted: 0 | ||||||
|     }); |     }); | ||||||
|  |  | ||||||
|     await sync_table.addRecentNoteSync(branchId, sourceId); |     await sync_table.addRecentNoteSync(branchId); | ||||||
|  |  | ||||||
|     await options.setOption('start_note_path', notePath, sourceId); |     await options.setOption('start_note_path', notePath); | ||||||
|  |  | ||||||
|     return await getRecentNotes(); |     return await getRecentNotes(); | ||||||
| } | } | ||||||
|   | |||||||
| @@ -19,13 +19,12 @@ async function getAllowedSettings() { | |||||||
|  |  | ||||||
| async function updateSetting(req) { | async function updateSetting(req) { | ||||||
|     const body = req.body; |     const body = req.body; | ||||||
|     const sourceId = req.headers.source_id; |  | ||||||
|  |  | ||||||
|     if (!ALLOWED_OPTIONS.includes(body['name'])) { |     if (!ALLOWED_OPTIONS.includes(body['name'])) { | ||||||
|         return [400, "not allowed option to set"]; |         return [400, "not allowed option to set"]; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     await options.setOption(body['name'], body['value'], sourceId); |     await options.setOption(body['name'], body['value']); | ||||||
| } | } | ||||||
|  |  | ||||||
| module.exports = { | module.exports = { | ||||||
|   | |||||||
| @@ -62,13 +62,12 @@ async function getTree(req) { | |||||||
|  |  | ||||||
| async function setPrefix(req) { | async function setPrefix(req) { | ||||||
|     const branchId = req.params.branchId; |     const branchId = req.params.branchId; | ||||||
|     const sourceId = req.headers.source_id; |  | ||||||
|     const prefix = utils.isEmptyOrWhitespace(req.body.prefix) ? null : req.body.prefix; |     const prefix = utils.isEmptyOrWhitespace(req.body.prefix) ? null : req.body.prefix; | ||||||
|  |  | ||||||
|     await sql.doInTransaction(async () => { |     await sql.doInTransaction(async () => { | ||||||
|         await sql.execute("UPDATE branches SET prefix = ?, dateModified = ? WHERE branchId = ?", [prefix, utils.nowDate(), branchId]); |         await sql.execute("UPDATE branches SET prefix = ?, dateModified = ? WHERE branchId = ?", [prefix, utils.nowDate(), branchId]); | ||||||
|  |  | ||||||
|         await sync_table.addBranchSync(branchId, sourceId); |         await sync_table.addBranchSync(branchId); | ||||||
|     }); |     }); | ||||||
|  |  | ||||||
|     return {}; |     return {}; | ||||||
|   | |||||||
| @@ -14,7 +14,6 @@ const notes = require('../../services/notes'); | |||||||
| async function moveBranchToParent(req) { | async function moveBranchToParent(req) { | ||||||
|     const branchId = req.params.branchId; |     const branchId = req.params.branchId; | ||||||
|     const parentNoteId = req.params.parentNoteId; |     const parentNoteId = req.params.parentNoteId; | ||||||
|     const sourceId = req.headers.source_id; |  | ||||||
|  |  | ||||||
|     const noteToMove = await tree.getBranch(branchId); |     const noteToMove = await tree.getBranch(branchId); | ||||||
|  |  | ||||||
| @@ -32,7 +31,7 @@ async function moveBranchToParent(req) { | |||||||
|     await sql.execute("UPDATE branches SET parentNoteId = ?, notePosition = ?, dateModified = ? WHERE branchId = ?", |     await sql.execute("UPDATE branches SET parentNoteId = ?, notePosition = ?, dateModified = ? WHERE branchId = ?", | ||||||
|         [parentNoteId, newNotePos, now, branchId]); |         [parentNoteId, newNotePos, now, branchId]); | ||||||
|  |  | ||||||
|     await sync_table.addBranchSync(branchId, sourceId); |     await sync_table.addBranchSync(branchId); | ||||||
|  |  | ||||||
|     return { success: true }; |     return { success: true }; | ||||||
| } | } | ||||||
| @@ -40,7 +39,6 @@ async function moveBranchToParent(req) { | |||||||
| async function moveBranchBeforeNote(req) { | async function moveBranchBeforeNote(req) { | ||||||
|     const branchId = req.params.branchId; |     const branchId = req.params.branchId; | ||||||
|     const beforeBranchId = req.params.beforeBranchId; |     const beforeBranchId = req.params.beforeBranchId; | ||||||
|     const sourceId = req.headers.source_id; |  | ||||||
|  |  | ||||||
|     const noteToMove = await tree.getBranch(branchId); |     const noteToMove = await tree.getBranch(branchId); | ||||||
|     const beforeNote = await tree.getBranch(beforeBranchId); |     const beforeNote = await tree.getBranch(beforeBranchId); | ||||||
| @@ -56,12 +54,12 @@ async function moveBranchBeforeNote(req) { | |||||||
|     await sql.execute("UPDATE branches SET notePosition = notePosition + 1 WHERE parentNoteId = ? AND notePosition >= ? AND isDeleted = 0", |     await sql.execute("UPDATE branches SET notePosition = notePosition + 1 WHERE parentNoteId = ? AND notePosition >= ? AND isDeleted = 0", | ||||||
|         [beforeNote.parentNoteId, beforeNote.notePosition]); |         [beforeNote.parentNoteId, beforeNote.notePosition]); | ||||||
|  |  | ||||||
|     await sync_table.addNoteReorderingSync(beforeNote.parentNoteId, sourceId); |     await sync_table.addNoteReorderingSync(beforeNote.parentNoteId); | ||||||
|  |  | ||||||
|     await sql.execute("UPDATE branches SET parentNoteId = ?, notePosition = ?, dateModified = ? WHERE branchId = ?", |     await sql.execute("UPDATE branches SET parentNoteId = ?, notePosition = ?, dateModified = ? WHERE branchId = ?", | ||||||
|         [beforeNote.parentNoteId, beforeNote.notePosition, utils.nowDate(), branchId]); |         [beforeNote.parentNoteId, beforeNote.notePosition, utils.nowDate(), branchId]); | ||||||
|  |  | ||||||
|     await sync_table.addBranchSync(branchId, sourceId); |     await sync_table.addBranchSync(branchId); | ||||||
|  |  | ||||||
|     return { success: true }; |     return { success: true }; | ||||||
| } | } | ||||||
| @@ -69,7 +67,6 @@ async function moveBranchBeforeNote(req) { | |||||||
| async function moveBranchAfterNote(req) { | async function moveBranchAfterNote(req) { | ||||||
|     const branchId = req.params.branchId; |     const branchId = req.params.branchId; | ||||||
|     const afterBranchId = req.params.afterBranchId; |     const afterBranchId = req.params.afterBranchId; | ||||||
|     const sourceId = req.headers.source_id; |  | ||||||
|  |  | ||||||
|     const noteToMove = await tree.getBranch(branchId); |     const noteToMove = await tree.getBranch(branchId); | ||||||
|     const afterNote = await tree.getBranch(afterBranchId); |     const afterNote = await tree.getBranch(afterBranchId); | ||||||
| @@ -85,12 +82,12 @@ async function moveBranchAfterNote(req) { | |||||||
|     await sql.execute("UPDATE branches SET notePosition = notePosition + 1 WHERE parentNoteId = ? AND notePosition > ? AND isDeleted = 0", |     await sql.execute("UPDATE branches SET notePosition = notePosition + 1 WHERE parentNoteId = ? AND notePosition > ? AND isDeleted = 0", | ||||||
|         [afterNote.parentNoteId, afterNote.notePosition]); |         [afterNote.parentNoteId, afterNote.notePosition]); | ||||||
|  |  | ||||||
|     await sync_table.addNoteReorderingSync(afterNote.parentNoteId, sourceId); |     await sync_table.addNoteReorderingSync(afterNote.parentNoteId); | ||||||
|  |  | ||||||
|     await sql.execute("UPDATE branches SET parentNoteId = ?, notePosition = ?, dateModified = ? WHERE branchId = ?", |     await sql.execute("UPDATE branches SET parentNoteId = ?, notePosition = ?, dateModified = ? WHERE branchId = ?", | ||||||
|         [afterNote.parentNoteId, afterNote.notePosition + 1, utils.nowDate(), branchId]); |         [afterNote.parentNoteId, afterNote.notePosition + 1, utils.nowDate(), branchId]); | ||||||
|  |  | ||||||
|     await sync_table.addBranchSync(branchId, sourceId); |     await sync_table.addBranchSync(branchId); | ||||||
|  |  | ||||||
|     return { success: true }; |     return { success: true }; | ||||||
| } | } | ||||||
| @@ -104,7 +101,7 @@ async function setExpanded(req) { | |||||||
| } | } | ||||||
|  |  | ||||||
| async function deleteBranch(req) { | async function deleteBranch(req) { | ||||||
|     await notes.deleteNote(req.params.branchId, req.headers.source_id); |     await notes.deleteNote(req.params.branchId); | ||||||
| } | } | ||||||
|  |  | ||||||
| module.exports = { | module.exports = { | ||||||
|   | |||||||
| @@ -11,7 +11,7 @@ const jimp = require('jimp'); | |||||||
| const imageType = require('image-type'); | const imageType = require('image-type'); | ||||||
| const sanitizeFilename = require('sanitize-filename'); | const sanitizeFilename = require('sanitize-filename'); | ||||||
|  |  | ||||||
| async function saveImage(file, sourceId, noteId) { | async function saveImage(file, noteId) { | ||||||
|     const resizedImage = await resize(file.buffer); |     const resizedImage = await resize(file.buffer); | ||||||
|     const optimizedImage = await optimize(resizedImage); |     const optimizedImage = await optimize(resizedImage); | ||||||
|  |  | ||||||
| @@ -35,7 +35,7 @@ async function saveImage(file, sourceId, noteId) { | |||||||
|             dateCreated: now |             dateCreated: now | ||||||
|         }); |         }); | ||||||
|  |  | ||||||
|         await sync_table.addImageSync(imageId, sourceId); |         await sync_table.addImageSync(imageId); | ||||||
|  |  | ||||||
|         const noteImageId = utils.newNoteImageId(); |         const noteImageId = utils.newNoteImageId(); | ||||||
|  |  | ||||||
| @@ -48,7 +48,7 @@ async function saveImage(file, sourceId, noteId) { | |||||||
|             dateCreated: now |             dateCreated: now | ||||||
|         }); |         }); | ||||||
|  |  | ||||||
|         await sync_table.addNoteImageSync(noteImageId, sourceId); |         await sync_table.addNoteImageSync(noteImageId); | ||||||
|     }); |     }); | ||||||
|     return {fileName, imageId}; |     return {fileName, imageId}; | ||||||
| } | } | ||||||
|   | |||||||
| @@ -55,7 +55,7 @@ async function getNoteIdsWithLabel(name) { | |||||||
|           WHERE notes.isDeleted = 0 AND labels.isDeleted = 0 AND labels.name = ? AND labels.isDeleted = 0`, [name]); |           WHERE notes.isDeleted = 0 AND labels.isDeleted = 0 AND labels.name = ? AND labels.isDeleted = 0`, [name]); | ||||||
| } | } | ||||||
|  |  | ||||||
| async function createLabel(noteId, name, value = "", sourceId = null) { | async function createLabel(noteId, name, value = "") { | ||||||
|     if (value === null || value === undefined) { |     if (value === null || value === undefined) { | ||||||
|         value = ""; |         value = ""; | ||||||
|     } |     } | ||||||
| @@ -75,7 +75,7 @@ async function createLabel(noteId, name, value = "", sourceId = null) { | |||||||
|         isDeleted: false |         isDeleted: false | ||||||
|     }); |     }); | ||||||
|  |  | ||||||
|     await sync_table.addLabelSync(labelId, sourceId); |     await sync_table.addLabelSync(labelId); | ||||||
| } | } | ||||||
|  |  | ||||||
| module.exports = { | module.exports = { | ||||||
|   | |||||||
| @@ -5,7 +5,7 @@ const sync_table = require('./sync_table'); | |||||||
| const labels = require('./labels'); | const labels = require('./labels'); | ||||||
| const protected_session = require('./protected_session'); | const protected_session = require('./protected_session'); | ||||||
|  |  | ||||||
| async function createNewNote(parentNoteId, noteOpts, dataKey, sourceId) { | async function createNewNote(parentNoteId, noteOpts, dataKey) { | ||||||
|     const noteId = utils.newNoteId(); |     const noteId = utils.newNoteId(); | ||||||
|     const branchId = utils.newBranchId(); |     const branchId = utils.newBranchId(); | ||||||
|  |  | ||||||
| @@ -25,7 +25,7 @@ async function createNewNote(parentNoteId, noteOpts, dataKey, sourceId) { | |||||||
|         await sql.execute('UPDATE branches SET notePosition = notePosition + 1 WHERE parentNoteId = ? AND notePosition > ? AND isDeleted = 0', |         await sql.execute('UPDATE branches SET notePosition = notePosition + 1 WHERE parentNoteId = ? AND notePosition > ? AND isDeleted = 0', | ||||||
|             [parentNoteId, afterNote.notePosition]); |             [parentNoteId, afterNote.notePosition]); | ||||||
|  |  | ||||||
|         await sync_table.addNoteReorderingSync(parentNoteId, sourceId); |         await sync_table.addNoteReorderingSync(parentNoteId); | ||||||
|     } |     } | ||||||
|     else { |     else { | ||||||
|         throw new Error('Unknown target: ' + noteOpts.target); |         throw new Error('Unknown target: ' + noteOpts.target); | ||||||
| @@ -62,7 +62,7 @@ async function createNewNote(parentNoteId, noteOpts, dataKey, sourceId) { | |||||||
|  |  | ||||||
|     await sql.insert("notes", note); |     await sql.insert("notes", note); | ||||||
|  |  | ||||||
|     await sync_table.addNoteSync(noteId, sourceId); |     await sync_table.addNoteSync(noteId); | ||||||
|  |  | ||||||
|     await sql.insert("branches", { |     await sql.insert("branches", { | ||||||
|         branchId: branchId, |         branchId: branchId, | ||||||
| @@ -74,7 +74,7 @@ async function createNewNote(parentNoteId, noteOpts, dataKey, sourceId) { | |||||||
|         isDeleted: 0 |         isDeleted: 0 | ||||||
|     }); |     }); | ||||||
|  |  | ||||||
|     await sync_table.addBranchSync(branchId, sourceId); |     await sync_table.addBranchSync(branchId); | ||||||
|  |  | ||||||
|     return { |     return { | ||||||
|         noteId, |         noteId, | ||||||
| @@ -106,7 +106,7 @@ async function createNote(parentNoteId, title, content = "", extraOptions = {}) | |||||||
|         note.mime = "text/html"; |         note.mime = "text/html"; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     const {noteId} = await createNewNote(parentNoteId, note, extraOptions.dataKey, extraOptions.sourceId); |     const {noteId} = await createNewNote(parentNoteId, note, extraOptions.dataKey); | ||||||
|  |  | ||||||
|     if (extraOptions.labels) { |     if (extraOptions.labels) { | ||||||
|         for (const attrName in extraOptions.labels) { |         for (const attrName in extraOptions.labels) { | ||||||
| @@ -117,19 +117,19 @@ async function createNote(parentNoteId, title, content = "", extraOptions = {}) | |||||||
|     return noteId; |     return noteId; | ||||||
| } | } | ||||||
|  |  | ||||||
| async function protectNoteRecursively(noteId, dataKey, protect, sourceId) { | async function protectNoteRecursively(noteId, dataKey, protect) { | ||||||
|     const note = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [noteId]); |     const note = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [noteId]); | ||||||
|  |  | ||||||
|     await protectNote(note, dataKey, protect, sourceId); |     await protectNote(note, dataKey, protect); | ||||||
|  |  | ||||||
|     const children = await sql.getColumn("SELECT noteId FROM branches WHERE parentNoteId = ? AND isDeleted = 0", [noteId]); |     const children = await sql.getColumn("SELECT noteId FROM branches WHERE parentNoteId = ? AND isDeleted = 0", [noteId]); | ||||||
|  |  | ||||||
|     for (const childNoteId of children) { |     for (const childNoteId of children) { | ||||||
|         await protectNoteRecursively(childNoteId, dataKey, protect, sourceId); |         await protectNoteRecursively(childNoteId, dataKey, protect); | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
| async function protectNote(note, dataKey, protect, sourceId) { | async function protectNote(note, dataKey, protect) { | ||||||
|     let changed = false; |     let changed = false; | ||||||
|  |  | ||||||
|     if (protect && !note.isProtected) { |     if (protect && !note.isProtected) { | ||||||
| @@ -151,13 +151,13 @@ async function protectNote(note, dataKey, protect, sourceId) { | |||||||
|         await sql.execute("UPDATE notes SET title = ?, content = ?, isProtected = ? WHERE noteId = ?", |         await sql.execute("UPDATE notes SET title = ?, content = ?, isProtected = ? WHERE noteId = ?", | ||||||
|             [note.title, note.content, note.isProtected, note.noteId]); |             [note.title, note.content, note.isProtected, note.noteId]); | ||||||
|  |  | ||||||
|         await sync_table.addNoteSync(note.noteId, sourceId); |         await sync_table.addNoteSync(note.noteId); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     await protectNoteRevisions(note.noteId, dataKey, protect, sourceId); |     await protectNoteRevisions(note.noteId, dataKey, protect); | ||||||
| } | } | ||||||
|  |  | ||||||
| async function protectNoteRevisions(noteId, dataKey, protect, sourceId) { | async function protectNoteRevisions(noteId, dataKey, protect) { | ||||||
|     const revisionsToChange = await sql.getRows("SELECT * FROM note_revisions WHERE noteId = ? AND isProtected != ?", [noteId, protect]); |     const revisionsToChange = await sql.getRows("SELECT * FROM note_revisions WHERE noteId = ? AND isProtected != ?", [noteId, protect]); | ||||||
|  |  | ||||||
|     for (const revision of revisionsToChange) { |     for (const revision of revisionsToChange) { | ||||||
| @@ -175,11 +175,11 @@ async function protectNoteRevisions(noteId, dataKey, protect, sourceId) { | |||||||
|         await sql.execute("UPDATE note_revisions SET title = ?, content = ?, isProtected = ? WHERE noteRevisionId = ?", |         await sql.execute("UPDATE note_revisions SET title = ?, content = ?, isProtected = ? WHERE noteRevisionId = ?", | ||||||
|             [revision.title, revision.content, revision.isProtected, revision.noteRevisionId]); |             [revision.title, revision.content, revision.isProtected, revision.noteRevisionId]); | ||||||
|  |  | ||||||
|         await sync_table.addNoteRevisionSync(revision.noteRevisionId, sourceId); |         await sync_table.addNoteRevisionSync(revision.noteRevisionId); | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
| async function saveNoteRevision(noteId, dataKey, sourceId, nowStr) { | async function saveNoteRevision(noteId, dataKey, nowStr) { | ||||||
|     const oldNote = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [noteId]); |     const oldNote = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [noteId]); | ||||||
|  |  | ||||||
|     if (oldNote.type === 'file') { |     if (oldNote.type === 'file') { | ||||||
| @@ -205,10 +205,10 @@ async function saveNoteRevision(noteId, dataKey, sourceId, nowStr) { | |||||||
|         dateModifiedTo: nowStr |         dateModifiedTo: nowStr | ||||||
|     }); |     }); | ||||||
|  |  | ||||||
|     await sync_table.addNoteRevisionSync(newNoteRevisionId, sourceId); |     await sync_table.addNoteRevisionSync(newNoteRevisionId); | ||||||
| } | } | ||||||
|  |  | ||||||
| async function saveNoteImages(noteId, noteText, sourceId) { | async function saveNoteImages(noteId, noteText) { | ||||||
|     const existingNoteImages = await sql.getRows("SELECT * FROM note_images WHERE noteId = ?", [noteId]); |     const existingNoteImages = await sql.getRows("SELECT * FROM note_images WHERE noteId = ?", [noteId]); | ||||||
|     const foundImageIds = []; |     const foundImageIds = []; | ||||||
|     const now = utils.nowDate(); |     const now = utils.nowDate(); | ||||||
| @@ -231,13 +231,13 @@ async function saveNoteImages(noteId, noteText, sourceId) { | |||||||
|                 dateCreated: now |                 dateCreated: now | ||||||
|             }); |             }); | ||||||
|  |  | ||||||
|             await sync_table.addNoteImageSync(noteImageId, sourceId); |             await sync_table.addNoteImageSync(noteImageId); | ||||||
|         } |         } | ||||||
|         else if (existingNoteImage.isDeleted) { |         else if (existingNoteImage.isDeleted) { | ||||||
|             await sql.execute("UPDATE note_images SET isDeleted = 0, dateModified = ? WHERE noteImageId = ?", |             await sql.execute("UPDATE note_images SET isDeleted = 0, dateModified = ? WHERE noteImageId = ?", | ||||||
|                 [now, existingNoteImage.noteImageId]); |                 [now, existingNoteImage.noteImageId]); | ||||||
|  |  | ||||||
|             await sync_table.addNoteImageSync(existingNoteImage.noteImageId, sourceId); |             await sync_table.addNoteImageSync(existingNoteImage.noteImageId); | ||||||
|         } |         } | ||||||
|         // else we don't need to do anything |         // else we don't need to do anything | ||||||
|  |  | ||||||
| @@ -251,7 +251,7 @@ async function saveNoteImages(noteId, noteText, sourceId) { | |||||||
|         await sql.execute("UPDATE note_images SET isDeleted = 1, dateModified = ? WHERE noteImageId = ?", |         await sql.execute("UPDATE note_images SET isDeleted = 1, dateModified = ? WHERE noteImageId = ?", | ||||||
|             [now, unusedNoteImage.noteImageId]); |             [now, unusedNoteImage.noteImageId]); | ||||||
|  |  | ||||||
|         await sync_table.addNoteImageSync(unusedNoteImage.noteImageId, sourceId); |         await sync_table.addNoteImageSync(unusedNoteImage.noteImageId); | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -265,7 +265,7 @@ async function loadFile(noteId, newNote, dataKey) { | |||||||
|     newNote.content = oldNote.content; |     newNote.content = oldNote.content; | ||||||
| } | } | ||||||
|  |  | ||||||
| async function updateNote(noteId, newNote, dataKey, sourceId) { | async function updateNote(noteId, newNote, dataKey) { | ||||||
|     if (newNote.type === 'file') { |     if (newNote.type === 'file') { | ||||||
|         await loadFile(noteId, newNote, dataKey); |         await loadFile(noteId, newNote, dataKey); | ||||||
|     } |     } | ||||||
| @@ -293,10 +293,10 @@ async function updateNote(noteId, newNote, dataKey, sourceId) { | |||||||
|             && !existingnoteRevisionId |             && !existingnoteRevisionId | ||||||
|             && msSinceDateCreated >= noteRevisionSnapshotTimeInterval * 1000) { |             && msSinceDateCreated >= noteRevisionSnapshotTimeInterval * 1000) { | ||||||
|  |  | ||||||
|             await saveNoteRevision(noteId, dataKey, sourceId, nowStr); |             await saveNoteRevision(noteId, dataKey, nowStr); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         await saveNoteImages(noteId, newNote.content, sourceId); |         await saveNoteImages(noteId, newNote.content); | ||||||
|  |  | ||||||
|         await protectNoteRevisions(noteId, dataKey, newNote.isProtected); |         await protectNoteRevisions(noteId, dataKey, newNote.isProtected); | ||||||
|  |  | ||||||
| @@ -307,11 +307,11 @@ async function updateNote(noteId, newNote, dataKey, sourceId) { | |||||||
|             nowStr, |             nowStr, | ||||||
|             noteId]); |             noteId]); | ||||||
|  |  | ||||||
|         await sync_table.addNoteSync(noteId, sourceId); |         await sync_table.addNoteSync(noteId); | ||||||
|     }); |     }); | ||||||
| } | } | ||||||
|  |  | ||||||
| async function deleteNote(branchId, sourceId) { | async function deleteNote(branchId) { | ||||||
|     const branch = await sql.getRowOrNull("SELECT * FROM branches WHERE branchId = ?", [branchId]); |     const branch = await sql.getRowOrNull("SELECT * FROM branches WHERE branchId = ?", [branchId]); | ||||||
|  |  | ||||||
|     if (!branch || branch.isDeleted === 1) { |     if (!branch || branch.isDeleted === 1) { | ||||||
| @@ -321,7 +321,7 @@ async function deleteNote(branchId, sourceId) { | |||||||
|     const now = utils.nowDate(); |     const now = utils.nowDate(); | ||||||
|  |  | ||||||
|     await sql.execute("UPDATE branches SET isDeleted = 1, dateModified = ? WHERE branchId = ?", [now, branchId]); |     await sql.execute("UPDATE branches SET isDeleted = 1, dateModified = ? WHERE branchId = ?", [now, branchId]); | ||||||
|     await sync_table.addBranchSync(branchId, sourceId); |     await sync_table.addBranchSync(branchId); | ||||||
|  |  | ||||||
|     const noteId = await sql.getValue("SELECT noteId FROM branches WHERE branchId = ?", [branchId]); |     const noteId = await sql.getValue("SELECT noteId FROM branches WHERE branchId = ?", [branchId]); | ||||||
|  |  | ||||||
| @@ -329,12 +329,12 @@ async function deleteNote(branchId, sourceId) { | |||||||
|  |  | ||||||
|     if (!notDeletedBranchsCount) { |     if (!notDeletedBranchsCount) { | ||||||
|         await sql.execute("UPDATE notes SET isDeleted = 1, dateModified = ? WHERE noteId = ?", [now, noteId]); |         await sql.execute("UPDATE notes SET isDeleted = 1, dateModified = ? WHERE noteId = ?", [now, noteId]); | ||||||
|         await sync_table.addNoteSync(noteId, sourceId); |         await sync_table.addNoteSync(noteId); | ||||||
|  |  | ||||||
|         const children = await sql.getRows("SELECT branchId FROM branches WHERE parentNoteId = ? AND isDeleted = 0", [noteId]); |         const children = await sql.getRows("SELECT branchId FROM branches WHERE parentNoteId = ? AND isDeleted = 0", [noteId]); | ||||||
|  |  | ||||||
|         for (const child of children) { |         for (const child of children) { | ||||||
|             await deleteNote(child.branchId, sourceId); |             await deleteNote(child.branchId); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -22,7 +22,7 @@ async function getOption(name) { | |||||||
|     return row['value'] ? row['value'] : row['opt_value']; |     return row['value'] ? row['value'] : row['opt_value']; | ||||||
| } | } | ||||||
|  |  | ||||||
| async function setOption(name, value, sourceId = null) { | async function setOption(name, value) { | ||||||
|     const opt = await sql.getRow("SELECT * FROM options WHERE name = ?", [name]); |     const opt = await sql.getRow("SELECT * FROM options WHERE name = ?", [name]); | ||||||
|  |  | ||||||
|     if (!opt) { |     if (!opt) { | ||||||
| @@ -30,14 +30,14 @@ async function setOption(name, value, sourceId = null) { | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     if (opt.isSynced) { |     if (opt.isSynced) { | ||||||
|         await sync_table.addOptionsSync(name, sourceId); |         await sync_table.addOptionsSync(name); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     await sql.execute("UPDATE options SET value = ?, dateModified = ? WHERE name = ?", |     await sql.execute("UPDATE options SET value = ?, dateModified = ? WHERE name = ?", | ||||||
|         [value, utils.nowDate(), name]); |         [value, utils.nowDate(), name]); | ||||||
| } | } | ||||||
|  |  | ||||||
| async function createOption(name, value, isSynced, sourceId = null) { | async function createOption(name, value, isSynced) { | ||||||
|     await sql.insert("options", { |     await sql.insert("options", { | ||||||
|         name: name, |         name: name, | ||||||
|         value: value, |         value: value, | ||||||
| @@ -46,7 +46,7 @@ async function createOption(name, value, isSynced, sourceId = null) { | |||||||
|     }); |     }); | ||||||
|  |  | ||||||
|     if (isSynced) { |     if (isSynced) { | ||||||
|         await sync_table.addOptionsSync(name, sourceId); |         await sync_table.addOptionsSync(name); | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -3,6 +3,7 @@ const source_id = require('./source_id'); | |||||||
| const utils = require('./utils'); | const utils = require('./utils'); | ||||||
| const sync_setup = require('./sync_setup'); | const sync_setup = require('./sync_setup'); | ||||||
| const log = require('./log'); | const log = require('./log'); | ||||||
|  | const cls = require('./cls'); | ||||||
|  |  | ||||||
| async function addNoteSync(noteId, sourceId) { | async function addNoteSync(noteId, sourceId) { | ||||||
|     await addEntitySync("notes", noteId, sourceId) |     await addEntitySync("notes", noteId, sourceId) | ||||||
| @@ -49,7 +50,7 @@ async function addEntitySync(entityName, entityId, sourceId) { | |||||||
|         entityName: entityName, |         entityName: entityName, | ||||||
|         entityId: entityId, |         entityId: entityId, | ||||||
|         syncDate: utils.nowDate(), |         syncDate: utils.nowDate(), | ||||||
|         sourceId: sourceId || source_id.getCurrentSourceId() |         sourceId: sourceId || cls.getSourceId() || source_id.getCurrentSourceId() | ||||||
|     }); |     }); | ||||||
|  |  | ||||||
|     if (!sync_setup.isSyncSetup) { |     if (!sync_setup.isSyncSetup) { | ||||||
|   | |||||||
| @@ -76,7 +76,7 @@ async function loadSubTreeNoteIds(parentNoteId, subTreeNoteIds) { | |||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
| async function sortNotesAlphabetically(parentNoteId, req, sourceId) { | async function sortNotesAlphabetically(parentNoteId, req) { | ||||||
|     await sql.doInTransaction(async () => { |     await sql.doInTransaction(async () => { | ||||||
|         const notes = await sql.getRows(`SELECT branchId, noteId, title, isProtected  |         const notes = await sql.getRows(`SELECT branchId, noteId, title, isProtected  | ||||||
|                                        FROM notes JOIN branches USING(noteId)  |                                        FROM notes JOIN branches USING(noteId)  | ||||||
| @@ -95,7 +95,7 @@ async function sortNotesAlphabetically(parentNoteId, req, sourceId) { | |||||||
|             position++; |             position++; | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         await sync_table.addNoteReorderingSync(parentNoteId, sourceId); |         await sync_table.addNoteReorderingSync(parentNoteId); | ||||||
|     }); |     }); | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user