mirror of
				https://github.com/zadam/trilium.git
				synced 2025-10-31 10:26:08 +01:00 
			
		
		
		
	content migration fixes
This commit is contained in:
		| @@ -30,12 +30,13 @@ | ||||
|  | ||||
| const Entity = require('./entity'); | ||||
| const Attribute = require('./attribute'); | ||||
| const NoteContent = require('./note_content'); | ||||
| const protectedSessionService = require('../services/protected_session'); | ||||
| const repository = require('../services/repository'); | ||||
| const sql = require('../services/sql'); | ||||
| const utils = require('../services/utils'); | ||||
| const dateUtils = require('../services/date_utils'); | ||||
| const noteFulltextService = require('../services/note_fulltext'); | ||||
| const syncTableService = require('../services/sync_table'); | ||||
|  | ||||
| const LABEL = 'label'; | ||||
| const LABEL_DEFINITION = 'label-definition'; | ||||
| @@ -84,37 +85,33 @@ class Note extends Entity { | ||||
|                 protectedSessionService.decryptNote(this); | ||||
|             } | ||||
|             else { | ||||
|                 // saving ciphertexts in case we do want to update protected note outside of protected session | ||||
|                 // (which is allowed) | ||||
|                 this.titleCipherText = this.title; | ||||
|                 this.title = "[protected]"; | ||||
|             } | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     /** @returns {Promise<NoteContent>} */ | ||||
|     async getNoteContent() { | ||||
|         if (!this.noteContent) { | ||||
|             this.noteContent = await repository.getEntity(`SELECT * FROM note_contents WHERE noteId = ?`, [this.noteId]); | ||||
|     /** @returns {Promise<*>} */ | ||||
|     async getContent() { | ||||
|         if (this.content === undefined) { | ||||
|             this.content = await sql.getValue(`SELECT content FROM note_contents WHERE noteId = ?`, [this.noteId]); | ||||
|  | ||||
|             if (!this.noteContent) { | ||||
|                 throw new Error("Note content not found for noteId=" + this.noteId); | ||||
|             if (this.isProtected) { | ||||
|                 if (this.isContentAvailable) { | ||||
|                     protectedSessionService.decryptNoteContent(this); | ||||
|                 } | ||||
|                 else { | ||||
|                     this.content = ""; | ||||
|                 } | ||||
|             } | ||||
|  | ||||
|             if (this.isStringNote()) { | ||||
|                 this.noteContent.content = this.noteContent.content === null | ||||
|                     ? "" : this.noteContent.content.toString("UTF-8"); | ||||
|                 this.content = this.content === null | ||||
|                     ? "" | ||||
|                     : this.content.toString("UTF-8"); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         return this.noteContent; | ||||
|     } | ||||
|  | ||||
|     /** @returns {Promise<*>} */ | ||||
|     async getContent() { | ||||
|         const noteContent = await this.getNoteContent(); | ||||
|  | ||||
|         return noteContent.content; | ||||
|         return this.content; | ||||
|     } | ||||
|  | ||||
|     /** @returns {Promise<*>} */ | ||||
| @@ -126,14 +123,31 @@ class Note extends Entity { | ||||
|  | ||||
|     /** @returns {Promise} */ | ||||
|     async setContent(content) { | ||||
|         if (!this.noteContent) { | ||||
|             // make sure it is loaded | ||||
|             await this.getNoteContent(); | ||||
|         this.content = content; | ||||
|  | ||||
|         const pojo = { | ||||
|             noteId: this.noteId, | ||||
|             content: content, | ||||
|             utcDateModified: dateUtils.utcNowDateTime(), | ||||
|             hash: utils.hash(this.noteId + "|" + content) | ||||
|         }; | ||||
|  | ||||
|         if (this.isProtected) { | ||||
|             if (this.isContentAvailable) { | ||||
|                 protectedSessionService.encryptNoteContent(pojo); | ||||
|             } | ||||
|             else { | ||||
|                 throw new Error(`Cannot update content of noteId=${this.noteId} since we're out of protected session.`); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         this.noteContent.content = content; | ||||
|         await sql.upsert("note_contents", "noteId", pojo); | ||||
|  | ||||
|         await this.noteContent.save(); | ||||
|         await syncTableService.addNoteContentSync(this.noteId); | ||||
|  | ||||
|         this.forcedChange = true; | ||||
|  | ||||
|         await this.save(); | ||||
|     } | ||||
|  | ||||
|     /** @returns {Promise} */ | ||||
| @@ -715,14 +729,13 @@ class Note extends Entity { | ||||
|             } | ||||
|             else { | ||||
|                 // updating protected note outside of protected session means we will keep original ciphertexts | ||||
|                 pojo.title = pojo.titleCipherText; | ||||
|                 delete pojo.title; | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         delete pojo.isContentAvailable; | ||||
|         delete pojo.__attributeCache; | ||||
|         delete pojo.titleCipherText; | ||||
|         delete pojo.noteContent; | ||||
|         delete pojo.content; | ||||
|     } | ||||
|  | ||||
|     async afterSaving() { | ||||
| @@ -740,7 +753,7 @@ module.exports = Note;</code></pre> | ||||
| </div> | ||||
|  | ||||
| <nav> | ||||
|     <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="ApiToken.html">ApiToken</a></li><li><a href="Attribute.html">Attribute</a></li><li><a href="BackendScriptApi.html">BackendScriptApi</a></li><li><a href="Branch.html">Branch</a></li><li><a href="Entity.html">Entity</a></li><li><a href="Link.html">Link</a></li><li><a href="Note.html">Note</a></li><li><a href="NoteContent.html">NoteContent</a></li><li><a href="NoteRevision.html">NoteRevision</a></li><li><a href="Option.html">Option</a></li><li><a href="RecentNote.html">RecentNote</a></li></ul><h3><a href="global.html">Global</a></h3> | ||||
|     <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="ApiToken.html">ApiToken</a></li><li><a href="Attribute.html">Attribute</a></li><li><a href="BackendScriptApi.html">BackendScriptApi</a></li><li><a href="Branch.html">Branch</a></li><li><a href="Entity.html">Entity</a></li><li><a href="Link.html">Link</a></li><li><a href="Note.html">Note</a></li><li><a href="NoteRevision.html">NoteRevision</a></li><li><a href="Option.html">Option</a></li><li><a href="RecentNote.html">RecentNote</a></li></ul><h3><a href="global.html">Global</a></h3> | ||||
| </nav> | ||||
|  | ||||
| <br class="clear"> | ||||
|   | ||||
		Reference in New Issue
	
	Block a user