mirror of
https://github.com/zadam/trilium.git
synced 2025-03-01 14:22:32 +01:00
sync changes for note content
This commit is contained in:
parent
b0596c9eb2
commit
22228de63b
@ -4,6 +4,8 @@ CREATE TABLE IF NOT EXISTS "note_contents" (
|
|||||||
`isProtected` INT NOT NULL DEFAULT 0,
|
`isProtected` INT NOT NULL DEFAULT 0,
|
||||||
`content` TEXT NULL DEFAULT NULL,
|
`content` TEXT NULL DEFAULT NULL,
|
||||||
`hash` TEXT DEFAULT "" NOT NULL,
|
`hash` TEXT DEFAULT "" NOT NULL,
|
||||||
|
`dateCreated` TEXT NOT NULL,
|
||||||
|
`dateModified` TEXT NOT NULL,
|
||||||
PRIMARY KEY(`noteContentId`)
|
PRIMARY KEY(`noteContentId`)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -678,7 +678,6 @@ class Note extends Entity {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
delete pojo.jsonContent;
|
|
||||||
delete pojo.isContentAvailable;
|
delete pojo.isContentAvailable;
|
||||||
delete pojo.__attributeCache;
|
delete pojo.__attributeCache;
|
||||||
delete pojo.titleCipherText;
|
delete pojo.titleCipherText;
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
const Entity = require('./entity');
|
const Entity = require('./entity');
|
||||||
const protectedSessionService = require('../services/protected_session');
|
const protectedSessionService = require('../services/protected_session');
|
||||||
const repository = require('../services/repository');
|
const repository = require('../services/repository');
|
||||||
|
const dateUtils = require('../services/date_utils');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This represents a Note which is a central object in the Trilium Notes project.
|
* This represents a Note which is a central object in the Trilium Notes project.
|
||||||
@ -11,6 +12,8 @@ const repository = require('../services/repository');
|
|||||||
* @property {string} noteId - reference to owning note
|
* @property {string} noteId - reference to owning note
|
||||||
* @property {boolean} isProtected - true if note content is protected
|
* @property {boolean} isProtected - true if note content is protected
|
||||||
* @property {blob} content - note content - e.g. HTML text for text notes, file payload for files
|
* @property {blob} content - note content - e.g. HTML text for text notes, file payload for files
|
||||||
|
* @property {string} dateCreated
|
||||||
|
* @property {string} dateModified
|
||||||
*
|
*
|
||||||
* @extends Entity
|
* @extends Entity
|
||||||
*/
|
*/
|
||||||
@ -61,6 +64,18 @@ class NoteContent extends Entity {
|
|||||||
return await repository.getNote(this.noteId);
|
return await repository.getNote(this.noteId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
beforeSaving() {
|
||||||
|
if (!this.dateCreated) {
|
||||||
|
this.dateCreated = dateUtils.nowDate();
|
||||||
|
}
|
||||||
|
|
||||||
|
super.beforeSaving();
|
||||||
|
|
||||||
|
if (this.isChanged) {
|
||||||
|
this.dateModified = dateUtils.nowDate();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// cannot be static!
|
// cannot be static!
|
||||||
updatePojo(pojo) {
|
updatePojo(pojo) {
|
||||||
if (pojo.isProtected) {
|
if (pojo.isProtected) {
|
||||||
@ -73,7 +88,6 @@ class NoteContent extends Entity {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
delete pojo.jsonContent;
|
|
||||||
delete pojo.isContentAvailable;
|
delete pojo.isContentAvailable;
|
||||||
delete pojo.contentCipherText;
|
delete pojo.contentCipherText;
|
||||||
}
|
}
|
||||||
|
@ -31,7 +31,7 @@ async function loginSync(req) {
|
|||||||
const syncVersion = req.body.syncVersion;
|
const syncVersion = req.body.syncVersion;
|
||||||
|
|
||||||
if (syncVersion !== appInfo.syncVersion) {
|
if (syncVersion !== appInfo.syncVersion) {
|
||||||
return [400, { message: `Non-matching sync versions, local is version ${appInfo.syncVersion}, remote is ${syncVersion}` }];
|
return [400, { message: `Non-matching sync versions, local is version ${appInfo.syncVersion}, remote is ${syncVersion}. It is recommended to run same version of Trilium on both sides of sync.` }];
|
||||||
}
|
}
|
||||||
|
|
||||||
const documentSecret = await options.getOption('documentSecret');
|
const documentSecret = await options.getOption('documentSecret');
|
||||||
|
@ -239,6 +239,7 @@ async function syncRequest(syncContext, method, requestPath, body) {
|
|||||||
|
|
||||||
const primaryKeys = {
|
const primaryKeys = {
|
||||||
"notes": "noteId",
|
"notes": "noteId",
|
||||||
|
"note_contents": "noteContentId",
|
||||||
"branches": "branchId",
|
"branches": "branchId",
|
||||||
"note_revisions": "noteRevisionId",
|
"note_revisions": "noteRevisionId",
|
||||||
"recent_notes": "branchId",
|
"recent_notes": "branchId",
|
||||||
@ -261,9 +262,10 @@ async function getEntityRow(entityName, entityId) {
|
|||||||
|
|
||||||
const entity = await sql.getRow(`SELECT * FROM ${entityName} WHERE ${primaryKey} = ?`, [entityId]);
|
const entity = await sql.getRow(`SELECT * FROM ${entityName} WHERE ${primaryKey} = ?`, [entityId]);
|
||||||
|
|
||||||
if (entityName === 'notes'
|
if (['note_contents', 'note_revisions'].includes(entityName) && entity.content !== null) {
|
||||||
&& entity.content !== null
|
if (typeof entity.content === 'string') {
|
||||||
&& (entity.type === 'file' || entity.type === 'image')) {
|
entity.content = Buffer.from(entity.content, 'UTF-8');
|
||||||
|
}
|
||||||
|
|
||||||
entity.content = entity.content.toString("base64");
|
entity.content = entity.content.toString("base64");
|
||||||
}
|
}
|
||||||
|
@ -8,6 +8,10 @@ async function addNoteSync(noteId, sourceId) {
|
|||||||
await addEntitySync("notes", noteId, sourceId)
|
await addEntitySync("notes", noteId, sourceId)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function addNoteContentSync(noteContentId, sourceId) {
|
||||||
|
await addEntitySync("note_contents", noteContentId, sourceId)
|
||||||
|
}
|
||||||
|
|
||||||
async function addBranchSync(branchId, sourceId) {
|
async function addBranchSync(branchId, sourceId) {
|
||||||
await addEntitySync("branches", branchId, sourceId)
|
await addEntitySync("branches", branchId, sourceId)
|
||||||
}
|
}
|
||||||
@ -84,6 +88,7 @@ async function fillAllSyncRows() {
|
|||||||
await sql.execute("DELETE FROM sync");
|
await sql.execute("DELETE FROM sync");
|
||||||
|
|
||||||
await fillSyncRows("notes", "noteId");
|
await fillSyncRows("notes", "noteId");
|
||||||
|
await fillSyncRows("note_contents", "noteContentId");
|
||||||
await fillSyncRows("branches", "branchId");
|
await fillSyncRows("branches", "branchId");
|
||||||
await fillSyncRows("note_revisions", "noteRevisionId");
|
await fillSyncRows("note_revisions", "noteRevisionId");
|
||||||
await fillSyncRows("recent_notes", "branchId");
|
await fillSyncRows("recent_notes", "branchId");
|
||||||
@ -95,6 +100,7 @@ async function fillAllSyncRows() {
|
|||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
addNoteSync,
|
addNoteSync,
|
||||||
|
addNoteContentSync,
|
||||||
addBranchSync,
|
addBranchSync,
|
||||||
addNoteReorderingSync,
|
addNoteReorderingSync,
|
||||||
addNoteRevisionSync,
|
addNoteRevisionSync,
|
||||||
|
@ -10,6 +10,9 @@ async function updateEntity(sync, entity, sourceId) {
|
|||||||
if (entityName === 'notes') {
|
if (entityName === 'notes') {
|
||||||
await updateNote(entity, sourceId);
|
await updateNote(entity, sourceId);
|
||||||
}
|
}
|
||||||
|
else if (entityName === 'note_contents') {
|
||||||
|
await updateNoteContent(entity, sourceId);
|
||||||
|
}
|
||||||
else if (entityName === 'branches') {
|
else if (entityName === 'branches') {
|
||||||
await updateBranch(entity, sourceId);
|
await updateBranch(entity, sourceId);
|
||||||
}
|
}
|
||||||
@ -48,17 +51,7 @@ async function updateEntity(sync, entity, sourceId) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function deserializeNoteContentBuffer(note) {
|
|
||||||
const noteContent = await note.getNoteContent();
|
|
||||||
|
|
||||||
if (noteContent.content !== null && (note.type === 'file' || note.type === 'image')) {
|
|
||||||
noteContent.content = Buffer.from(noteContent.content, 'base64');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function updateNote(entity, sourceId) {
|
async function updateNote(entity, sourceId) {
|
||||||
await deserializeNoteContentBuffer(entity);
|
|
||||||
|
|
||||||
const origNote = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [entity.noteId]);
|
const origNote = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [entity.noteId]);
|
||||||
|
|
||||||
if (!origNote || origNote.dateModified <= entity.dateModified) {
|
if (!origNote || origNote.dateModified <= entity.dateModified) {
|
||||||
@ -72,6 +65,22 @@ async function updateNote(entity, sourceId) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function updateNoteContent(entity, sourceId) {
|
||||||
|
const origNoteContent = await sql.getRow("SELECT * FROM note_contents WHERE noteId = ?", [entity.noteId]);
|
||||||
|
|
||||||
|
if (!origNoteContent || origNoteContent.dateModified <= entity.dateModified) {
|
||||||
|
entity.content = entity.content === null ? null : Buffer.from(entity.content, 'base64');
|
||||||
|
|
||||||
|
await sql.transactional(async () => {
|
||||||
|
await sql.replace("note_contents", entity);
|
||||||
|
|
||||||
|
await syncTableService.addNoteContentSync(entity.noteContentId, sourceId);
|
||||||
|
});
|
||||||
|
|
||||||
|
log.info("Update/sync note content " + entity.noteContentId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async function updateBranch(entity, sourceId) {
|
async function updateBranch(entity, sourceId) {
|
||||||
const orig = await sql.getRowOrNull("SELECT * FROM branches WHERE branchId = ?", [entity.branchId]);
|
const orig = await sql.getRowOrNull("SELECT * FROM branches WHERE branchId = ?", [entity.branchId]);
|
||||||
|
|
||||||
@ -99,6 +108,8 @@ async function updateNoteRevision(entity, sourceId) {
|
|||||||
// we update note revision even if date modified to is the same because the only thing which might have changed
|
// we update note revision even if date modified to is the same because the only thing which might have changed
|
||||||
// is the protected status (and correnspondingly title and content) which doesn't affect the dateModifiedTo
|
// is the protected status (and correnspondingly title and content) which doesn't affect the dateModifiedTo
|
||||||
if (orig === null || orig.dateModifiedTo <= entity.dateModifiedTo) {
|
if (orig === null || orig.dateModifiedTo <= entity.dateModifiedTo) {
|
||||||
|
entity.content = entity.content === null ? null : Buffer.from(entity.content, 'base64');
|
||||||
|
|
||||||
await sql.replace('note_revisions', entity);
|
await sql.replace('note_revisions', entity);
|
||||||
|
|
||||||
await syncTableService.addNoteRevisionSync(entity.noteRevisionId, sourceId);
|
await syncTableService.addNoteRevisionSync(entity.noteRevisionId, sourceId);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user