mirror of
				https://github.com/zadam/trilium.git
				synced 2025-10-31 02:16:05 +01:00 
			
		
		
		
	added document_id for sync identification
This commit is contained in:
		
							
								
								
									
										5
									
								
								app.js
									
									
									
									
									
								
							
							
						
						
									
										5
									
								
								app.js
									
									
									
									
									
								
							| @@ -95,6 +95,11 @@ app.use((req, res, next) => { | |||||||
| // error handler | // error handler | ||||||
| app.use((err, req, res, next) => { | app.use((err, req, res, next) => { | ||||||
|     log.error(err.message); |     log.error(err.message); | ||||||
|  |  | ||||||
|  |     res.status(err.status || 500); | ||||||
|  |     res.send({ | ||||||
|  |         message: err.message | ||||||
|  |     }); | ||||||
| }); | }); | ||||||
|  |  | ||||||
| // triggers sync timer | // triggers sync timer | ||||||
|   | |||||||
							
								
								
									
										1
									
								
								migrations/0017__document_id.sql
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								migrations/0017__document_id.sql
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1 @@ | |||||||
|  | INSERT INTO options (opt_name, opt_value) VALUES ('document_id', ''); | ||||||
| @@ -3,7 +3,7 @@ const sql = require('./sql'); | |||||||
| const fs = require('fs-extra'); | const fs = require('fs-extra'); | ||||||
| const log = require('./log'); | const log = require('./log'); | ||||||
|  |  | ||||||
| const APP_DB_VERSION = 16; | const APP_DB_VERSION = 17; | ||||||
| const MIGRATIONS_DIR = "./migrations"; | const MIGRATIONS_DIR = "./migrations"; | ||||||
|  |  | ||||||
| async function migrate() { | async function migrate() { | ||||||
|   | |||||||
| @@ -34,9 +34,14 @@ async function rollback() { | |||||||
| } | } | ||||||
|  |  | ||||||
| async function getOption(optName) { | async function getOption(optName) { | ||||||
|     const row = await getSingleResult("SELECT opt_value FROM options WHERE opt_name = ?", [optName]); |     try { | ||||||
|  |         const row = await getSingleResult("SELECT opt_value FROM options WHERE opt_name = ?", [optName]); | ||||||
|  |  | ||||||
|     return row['opt_value']; |         return row['opt_value']; | ||||||
|  |     } | ||||||
|  |     catch (e) { | ||||||
|  |         throw new Error("Option " + optName + " doesn't exist"); | ||||||
|  |     } | ||||||
| } | } | ||||||
|  |  | ||||||
| async function setOption(optName, optValue) { | async function setOption(optName, optValue) { | ||||||
|   | |||||||
| @@ -112,6 +112,7 @@ async function sync() { | |||||||
|  |  | ||||||
| async function getChangedSince(since) { | async function getChangedSince(since) { | ||||||
|     return { |     return { | ||||||
|  |         'documentId': await getDocumentId(), | ||||||
|         'syncTimestamp': utils.nowTimestamp(), |         'syncTimestamp': utils.nowTimestamp(), | ||||||
|         'tree': await sql.getResults("select * from notes_tree where date_modified >= ?", [since]), |         'tree': await sql.getResults("select * from notes_tree where date_modified >= ?", [since]), | ||||||
|         'notes': await sql.getFlattenedResults('note_id', "select note_id from notes where date_modified >= ?", [since]), |         'notes': await sql.getFlattenedResults('note_id', "select note_id from notes where date_modified >= ?", [since]), | ||||||
| @@ -167,6 +168,22 @@ async function putNote(note) { | |||||||
|     log.info("Update/sync note " + note.detail.note_id); |     log.info("Update/sync note " + note.detail.note_id); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | let documentIdCache = null; | ||||||
|  |  | ||||||
|  | async function getDocumentId() { | ||||||
|  |     if (!documentIdCache) { | ||||||
|  |         documentIdCache = await sql.getOption('document_id'); | ||||||
|  |  | ||||||
|  |         if (!documentIdCache) { | ||||||
|  |             documentIdCache = utils.randomString(16); | ||||||
|  |  | ||||||
|  |             await sql.setOption('document_id', documentIdCache); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     return documentIdCache; | ||||||
|  | } | ||||||
|  |  | ||||||
| if (SYNC_SERVER) { | if (SYNC_SERVER) { | ||||||
|     log.info("Setting up sync"); |     log.info("Setting up sync"); | ||||||
|  |  | ||||||
|   | |||||||
| @@ -6,7 +6,7 @@ function newNoteId() { | |||||||
|  |  | ||||||
| const ALPHA_NUMERIC = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; | const ALPHA_NUMERIC = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; | ||||||
|  |  | ||||||
| function randomString(length, chars) { | function randomString(length) { | ||||||
|     let result = ''; |     let result = ''; | ||||||
|  |  | ||||||
|     for (let i = length; i > 0; --i) { |     for (let i = length; i > 0; --i) { | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user