mirror of
				https://github.com/zadam/trilium.git
				synced 2025-10-31 18:36:30 +01:00 
			
		
		
		
	script related changes
This commit is contained in:
		| @@ -3,6 +3,14 @@ | |||||||
| const Entity = require('./entity'); | const Entity = require('./entity'); | ||||||
|  |  | ||||||
| class Note extends Entity { | class Note extends Entity { | ||||||
|  |     constructor(sql, row) { | ||||||
|  |         super(sql, row); | ||||||
|  |  | ||||||
|  |         if (this.type === "code" && this.mime === "application/json") { | ||||||
|  |             this.jsonContent = JSON.parse(this.content); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |  | ||||||
|     async getAttributes() { |     async getAttributes() { | ||||||
|         return this.sql.getEntities("SELECT * FROM attributes WHERE noteId = ?", [this.noteId]); |         return this.sql.getEntities("SELECT * FROM attributes WHERE noteId = ?", [this.noteId]); | ||||||
|     } |     } | ||||||
|   | |||||||
| @@ -18,11 +18,11 @@ async function getNotesWithAttribute(dataKey, name, value) { | |||||||
|     let notes; |     let notes; | ||||||
|  |  | ||||||
|     if (value !== undefined) { |     if (value !== undefined) { | ||||||
|         notes = await sql.getRows(`SELECT notes.* FROM notes JOIN attributes USING(noteId)  |         notes = await sql.getEntities(`SELECT notes.* FROM notes JOIN attributes USING(noteId)  | ||||||
|           WHERE notes.isDeleted = 0 AND attributes.name = ? AND attributes.value = ?`, [name, value]); |           WHERE notes.isDeleted = 0 AND attributes.name = ? AND attributes.value = ?`, [name, value]); | ||||||
|     } |     } | ||||||
|     else { |     else { | ||||||
|         notes = await sql.getRows(`SELECT notes.* FROM notes JOIN attributes USING(noteId)  |         notes = await sql.getEntities(`SELECT notes.* FROM notes JOIN attributes USING(noteId)  | ||||||
|           WHERE notes.isDeleted = 0 AND attributes.name = ?`, [name]); |           WHERE notes.isDeleted = 0 AND attributes.name = ?`, [name]); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -6,7 +6,7 @@ const attributes = require('./attributes'); | |||||||
| const protected_session = require('./protected_session'); | const protected_session = require('./protected_session'); | ||||||
|  |  | ||||||
| async function getNoteById(noteId, dataKey) { | async function getNoteById(noteId, dataKey) { | ||||||
|     const note = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [noteId]); |     const note = await sql.getEntity("SELECT * FROM notes WHERE noteId = ?", [noteId]); | ||||||
|  |  | ||||||
|     protected_session.decryptNote(dataKey, note); |     protected_session.decryptNote(dataKey, note); | ||||||
|  |  | ||||||
|   | |||||||
| @@ -10,32 +10,16 @@ function ScriptContext(noteId, dataKey) { | |||||||
|     this.scriptNoteId = noteId; |     this.scriptNoteId = noteId; | ||||||
|     this.dataKey = protected_session.getDataKey(dataKey); |     this.dataKey = protected_session.getDataKey(dataKey); | ||||||
|  |  | ||||||
|     function deserializePayload(note) { |  | ||||||
|         if (note && note.type === "code" && note.mime === "application/json") { |  | ||||||
|             note.payload = JSON.parse(note.content); |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     function serializePayload(payload) { |     function serializePayload(payload) { | ||||||
|         return JSON.stringify(payload, null, '\t'); |         return JSON.stringify(payload, null, '\t'); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     this.getNoteById = async function(noteId) { |     this.getNoteById = async function(noteId) { | ||||||
|         const note = await notes.getNoteById(noteId, this.dataKey); |         return notes.getNoteById(noteId, this.dataKey); | ||||||
|  |  | ||||||
|         deserializePayload(note); |  | ||||||
|  |  | ||||||
|         return note; |  | ||||||
|     }; |     }; | ||||||
|  |  | ||||||
|     this.getNotesWithAttribute = async function (attrName, attrValue) { |     this.getNotesWithAttribute = async function (attrName, attrValue) { | ||||||
|         const notes = await attributes.getNotesWithAttribute(this.dataKey, attrName, attrValue); |         return await attributes.getNotesWithAttribute(this.dataKey, attrName, attrValue); | ||||||
|  |  | ||||||
|         for (const note of notes) { |  | ||||||
|             deserializePayload(note); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         return notes; |  | ||||||
|     }; |     }; | ||||||
|  |  | ||||||
|     this.getNoteWithAttribute = async function (attrName, attrValue) { |     this.getNoteWithAttribute = async function (attrName, attrValue) { | ||||||
| @@ -77,12 +61,10 @@ function ScriptContext(noteId, dataKey) { | |||||||
|  |  | ||||||
|     this.updateNote = async function (note) { |     this.updateNote = async function (note) { | ||||||
|         if (note.type === 'code' && note.mime === 'application/json') { |         if (note.type === 'code' && note.mime === 'application/json') { | ||||||
|             note.content = serializePayload(note.payload); |             note.content = serializePayload(note.jsonContent); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         log.info("new note text: ", note.content); |         delete note.jsonContent; | ||||||
|  |  | ||||||
|         delete note.payload; |  | ||||||
|  |  | ||||||
|         if (note.isProtected) { |         if (note.isProtected) { | ||||||
|             protected_session.encryptNote(this.dataKey, note); |             protected_session.encryptNote(this.dataKey, note); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user