mirror of
				https://github.com/zadam/trilium.git
				synced 2025-10-31 18:36:30 +01:00 
			
		
		
		
	smaller refactorings
This commit is contained in:
		| @@ -18,6 +18,8 @@ class Entity { | |||||||
|  |  | ||||||
|     async save() { |     async save() { | ||||||
|         await repository.updateEntity(this); |         await repository.updateEntity(this); | ||||||
|  |  | ||||||
|  |         return this; | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -19,15 +19,13 @@ async function cloneNoteToParent(req) { | |||||||
|     const maxNotePos = await sql.getValue('SELECT MAX(notePosition) FROM branches WHERE parentNoteId = ? AND isDeleted = 0', [parentNoteId]); |     const maxNotePos = await sql.getValue('SELECT MAX(notePosition) FROM branches WHERE parentNoteId = ? AND isDeleted = 0', [parentNoteId]); | ||||||
|     const newNotePos = maxNotePos === null ? 0 : maxNotePos + 1; |     const newNotePos = maxNotePos === null ? 0 : maxNotePos + 1; | ||||||
|  |  | ||||||
|     const branch = new Branch({ |     const branch = await new Branch({ | ||||||
|         noteId: noteId, |         noteId: noteId, | ||||||
|         parentNoteId: parentNoteId, |         parentNoteId: parentNoteId, | ||||||
|         prefix: prefix, |         prefix: prefix, | ||||||
|         notePosition: newNotePos, |         notePosition: newNotePos, | ||||||
|         isExpanded: 0 |         isExpanded: 0 | ||||||
|     }); |     }).save(); | ||||||
|  |  | ||||||
|     await branch.save(); |  | ||||||
|  |  | ||||||
|     await sql.execute("UPDATE branches SET isExpanded = 1 WHERE noteId = ?", [parentNoteId]); |     await sql.execute("UPDATE branches SET isExpanded = 1 WHERE noteId = ?", [parentNoteId]); | ||||||
|  |  | ||||||
| @@ -53,14 +51,12 @@ async function cloneNoteAfter(req) { | |||||||
|  |  | ||||||
|     await syncTable.addNoteReorderingSync(afterNote.parentNoteId); |     await syncTable.addNoteReorderingSync(afterNote.parentNoteId); | ||||||
|  |  | ||||||
|     const branch = new Branch({ |     const branch = await new Branch({ | ||||||
|         noteId: noteId, |         noteId: noteId, | ||||||
|         parentNoteId: afterNote.parentNoteId, |         parentNoteId: afterNote.parentNoteId, | ||||||
|         notePosition: afterNote.notePosition + 1, |         notePosition: afterNote.notePosition + 1, | ||||||
|         isExpanded: 0 |         isExpanded: 0 | ||||||
|     }); |     }).save(); | ||||||
|  |  | ||||||
|     await branch.save(); |  | ||||||
|  |  | ||||||
|     return { success: true }; |     return { success: true }; | ||||||
| } | } | ||||||
|   | |||||||
| @@ -24,14 +24,12 @@ 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 recentNote = new RecentNote({ |     await new RecentNote({ | ||||||
|         branchId: branchId, |         branchId: branchId, | ||||||
|         notePath: notePath, |         notePath: notePath, | ||||||
|         dateAccessed: dateUtils.nowDate(), |         dateAccessed: dateUtils.nowDate(), | ||||||
|         isDeleted: 0 |         isDeleted: 0 | ||||||
|     }); |     }).save(); | ||||||
|  |  | ||||||
|     await recentNote.save(); |  | ||||||
|  |  | ||||||
|     await optionService.setOption('startNotePath', notePath); |     await optionService.setOption('startNotePath', notePath); | ||||||
|  |  | ||||||
|   | |||||||
| @@ -20,8 +20,9 @@ async function login(req) { | |||||||
|         return [401, "Incorrect username/password"]; |         return [401, "Incorrect username/password"]; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     const apiToken = new ApiToken({ token: utils.randomSecureToken() }); |     const apiToken = await new ApiToken({ | ||||||
|     await apiToken.save(); |         token: utils.randomSecureToken() | ||||||
|  |     }).save(); | ||||||
|  |  | ||||||
|     return { |     return { | ||||||
|         token: apiToken.token |         token: apiToken.token | ||||||
|   | |||||||
| @@ -2,7 +2,6 @@ | |||||||
|  |  | ||||||
| const sql = require('../../services/sql'); | const sql = require('../../services/sql'); | ||||||
| const optionService = require('../../services/options'); | const optionService = require('../../services/options'); | ||||||
| const config = require('../../services/config'); |  | ||||||
| const protectedSessionService = require('../../services/protected_session'); | const protectedSessionService = require('../../services/protected_session'); | ||||||
|  |  | ||||||
| async function getTree() { | async function getTree() { | ||||||
|   | |||||||
| @@ -10,7 +10,7 @@ async function anonymize() { | |||||||
|         fs.mkdirSync(dataDir.ANONYMIZED_DB_DIR, 0o700); |         fs.mkdirSync(dataDir.ANONYMIZED_DB_DIR, 0o700); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     const anonymizedFile = dataDir.ANONYMIZED_DB_DIR + "/" + "backup-" + dateUtils.getDateTimeForFile() + ".db"; |     const anonymizedFile = dataDir.ANONYMIZED_DB_DIR + "/" + "anonymized-" + dateUtils.getDateTimeForFile() + ".db"; | ||||||
|  |  | ||||||
|     fs.copySync(dataDir.DOCUMENT_PATH, anonymizedFile); |     fs.copySync(dataDir.DOCUMENT_PATH, anonymizedFile); | ||||||
|  |  | ||||||
|   | |||||||
| @@ -20,21 +20,17 @@ async function saveImage(file, noteId) { | |||||||
|     const fileNameWithouExtension = file.originalname.replace(/\.[^/.]+$/, ""); |     const fileNameWithouExtension = file.originalname.replace(/\.[^/.]+$/, ""); | ||||||
|     const fileName = sanitizeFilename(fileNameWithouExtension + "." + imageFormat.ext); |     const fileName = sanitizeFilename(fileNameWithouExtension + "." + imageFormat.ext); | ||||||
|  |  | ||||||
|     const image = new Image({ |     const image = await new Image({ | ||||||
|         format: imageFormat.ext, |         format: imageFormat.ext, | ||||||
|         name: fileName, |         name: fileName, | ||||||
|         checksum: utils.hash(optimizedImage), |         checksum: utils.hash(optimizedImage), | ||||||
|         data: optimizedImage |         data: optimizedImage | ||||||
|     }); |     }).save(); | ||||||
|  |  | ||||||
|     await image.save(); |     await new NoteImage({ | ||||||
|  |  | ||||||
|     const noteImage = new NoteImage({ |  | ||||||
|         noteId: noteId, |         noteId: noteId, | ||||||
|         imageId: image.imageId |         imageId: image.imageId | ||||||
|     }); |     }).save(); | ||||||
|  |  | ||||||
|     await noteImage.save(); |  | ||||||
|  |  | ||||||
|     return {fileName, imageId: image.imageId}; |     return {fileName, imageId: image.imageId}; | ||||||
| } | } | ||||||
|   | |||||||
| @@ -15,10 +15,6 @@ const BUILTIN_LABELS = [ | |||||||
|     'appCss' |     'appCss' | ||||||
| ]; | ]; | ||||||
|  |  | ||||||
| async function getNoteLabelMap(noteId) { |  | ||||||
|     return await sql.getMap(`SELECT name, value FROM labels WHERE noteId = ? AND isDeleted = 0`, [noteId]); |  | ||||||
| } |  | ||||||
|  |  | ||||||
| async function getNoteIdWithLabel(name, value) { | async function getNoteIdWithLabel(name, value) { | ||||||
|     return await sql.getValue(`SELECT notes.noteId FROM notes JOIN labels USING(noteId)  |     return await sql.getValue(`SELECT notes.noteId FROM notes JOIN labels USING(noteId)  | ||||||
|           WHERE notes.isDeleted = 0 |           WHERE notes.isDeleted = 0 | ||||||
| @@ -54,19 +50,14 @@ async function getNoteIdsWithLabel(name) { | |||||||
| } | } | ||||||
|  |  | ||||||
| async function createLabel(noteId, name, value = "") { | async function createLabel(noteId, name, value = "") { | ||||||
|     const label = new Label({ |     return await new Label({ | ||||||
|         noteId: noteId, |         noteId: noteId, | ||||||
|         name: name, |         name: name, | ||||||
|         value: value |         value: value | ||||||
|     }); |     }).save(); | ||||||
|  |  | ||||||
|     await label.save(); |  | ||||||
|  |  | ||||||
|     return label; |  | ||||||
| } | } | ||||||
|  |  | ||||||
| module.exports = { | module.exports = { | ||||||
|     getNoteLabelMap, |  | ||||||
|     getNoteIdWithLabel, |     getNoteIdWithLabel, | ||||||
|     getNotesWithLabel, |     getNotesWithLabel, | ||||||
|     getNoteWithLabel, |     getNoteWithLabel, | ||||||
|   | |||||||
| @@ -44,24 +44,20 @@ async function createNewNote(parentNoteId, noteData) { | |||||||
|         noteData.mime = noteData.mime || parent.mime; |         noteData.mime = noteData.mime || parent.mime; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     const note = new Note({ |     const note = await new Note({ | ||||||
|         title: noteData.title, |         title: noteData.title, | ||||||
|         content: noteData.content || '', |         content: noteData.content || '', | ||||||
|         isProtected: noteData.isProtected, |         isProtected: noteData.isProtected, | ||||||
|         type: noteData.type || 'text', |         type: noteData.type || 'text', | ||||||
|         mime: noteData.mime || 'text/html' |         mime: noteData.mime || 'text/html' | ||||||
|     }); |     }).save(); | ||||||
|  |  | ||||||
|     await note.save(); |     const branch = await new Branch({ | ||||||
|  |  | ||||||
|     const branch = new Branch({ |  | ||||||
|         noteId: note.noteId, |         noteId: note.noteId, | ||||||
|         parentNoteId: parentNoteId, |         parentNoteId: parentNoteId, | ||||||
|         notePosition: newNotePos, |         notePosition: newNotePos, | ||||||
|         isExpanded: 0 |         isExpanded: 0 | ||||||
|     }); |     }).save(); | ||||||
|  |  | ||||||
|     await branch.save(); |  | ||||||
|  |  | ||||||
|     return { |     return { | ||||||
|         note, |         note, | ||||||
| @@ -141,10 +137,10 @@ async function saveNoteImages(note) { | |||||||
|         const existingNoteImage = existingNoteImages.find(ni => ni.imageId === imageId); |         const existingNoteImage = existingNoteImages.find(ni => ni.imageId === imageId); | ||||||
|  |  | ||||||
|         if (!existingNoteImage) { |         if (!existingNoteImage) { | ||||||
|             await (new NoteImage({ |             await new NoteImage({ | ||||||
|                 noteId: note.noteId, |                 noteId: note.noteId, | ||||||
|                 imageId: imageId |                 imageId: imageId | ||||||
|             })).save(); |             }).save(); | ||||||
|         } |         } | ||||||
|         // else we don't need to do anything |         // else we don't need to do anything | ||||||
|  |  | ||||||
| @@ -179,7 +175,7 @@ async function saveNoteRevision(note) { | |||||||
|         && !existingnoteRevisionId |         && !existingnoteRevisionId | ||||||
|         && msSinceDateCreated >= noteRevisionSnapshotTimeInterval * 1000) { |         && msSinceDateCreated >= noteRevisionSnapshotTimeInterval * 1000) { | ||||||
|  |  | ||||||
|         await (new NoteRevision({ |         await new NoteRevision({ | ||||||
|             noteId: note.noteId, |             noteId: note.noteId, | ||||||
|             // title and text should be decrypted now |             // title and text should be decrypted now | ||||||
|             title: note.title, |             title: note.title, | ||||||
| @@ -187,7 +183,7 @@ async function saveNoteRevision(note) { | |||||||
|             isProtected: 0, // will be fixed in the protectNoteRevisions() call |             isProtected: 0, // will be fixed in the protectNoteRevisions() call | ||||||
|             dateModifiedFrom: note.dateModified, |             dateModifiedFrom: note.dateModified, | ||||||
|             dateModifiedTo: dateUtils.nowDate() |             dateModifiedTo: dateUtils.nowDate() | ||||||
|         })).save(); |         }).save(); | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -4,12 +4,8 @@ const dateUtils = require('./date_utils'); | |||||||
| const syncTableService = require('./sync_table'); | const syncTableService = require('./sync_table'); | ||||||
| const appInfo = require('./app_info'); | const appInfo = require('./app_info'); | ||||||
|  |  | ||||||
| async function getOptionOrNull(name) { |  | ||||||
|     return await sql.getRowOrNull("SELECT value FROM options WHERE name = ?", [name]); |  | ||||||
| } |  | ||||||
|  |  | ||||||
| async function getOption(name) { | async function getOption(name) { | ||||||
|     const row = await getOptionOrNull(name); |     const row = await await sql.getRowOrNull("SELECT value FROM options WHERE name = ?", [name]); | ||||||
|  |  | ||||||
|     if (!row) { |     if (!row) { | ||||||
|         throw new Error("Option " + name + " doesn't exist"); |         throw new Error("Option " + name + " doesn't exist"); | ||||||
| @@ -69,8 +65,6 @@ async function initOptions(startNotePath) { | |||||||
|  |  | ||||||
| module.exports = { | module.exports = { | ||||||
|     getOption, |     getOption, | ||||||
|     getOptionOrNull, |  | ||||||
|     setOption, |     setOption, | ||||||
|     initOptions, |     initOptions | ||||||
|     createOption |  | ||||||
| }; | }; | ||||||
| @@ -48,21 +48,11 @@ function ScriptApi(startNote, currentNote) { | |||||||
|     this.getEntity = repository.getEntity; |     this.getEntity = repository.getEntity; | ||||||
|     this.getEntities = repository.getEntities; |     this.getEntities = repository.getEntities; | ||||||
|  |  | ||||||
|     this.getNotesWithLabel = async function (labelName, labelValue) { |  | ||||||
|         return await labelService.getNotesWithLabel(labelName, labelValue); |  | ||||||
|     }; |  | ||||||
|  |  | ||||||
|     this.getNoteWithLabel = async function (labelName, labelValue) { |  | ||||||
|         const notes = await this.getNotesWithLabel(labelName, labelValue); |  | ||||||
|  |  | ||||||
|         return notes.length > 0 ? notes[0] : null; |  | ||||||
|     }; |  | ||||||
|  |  | ||||||
|     this.createNote = async function(parentNoteId, title, content = "", extraOptions = {}) { |  | ||||||
|         return await noteService.createNote(parentNoteId, title, content, extraOptions); |  | ||||||
|     }; |  | ||||||
|  |  | ||||||
|     this.createLabel = labelService.createLabel; |     this.createLabel = labelService.createLabel; | ||||||
|  |     this.getNotesWithLabel = labelService.getNotesWithLabel; | ||||||
|  |     this.getNoteWithLabel = labelService.getNoteWithLabel; | ||||||
|  |  | ||||||
|  |     this.createNote = noteService.createNote; | ||||||
|  |  | ||||||
|     this.log = message => log.info(`Script ${currentNote.noteId}: ${message}`); |     this.log = message => log.info(`Script ${currentNote.noteId}: ${message}`); | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user