mirror of
				https://github.com/zadam/trilium.git
				synced 2025-10-31 03:29:02 +01:00 
			
		
		
		
	don't push changes to server which have been pulled from it
This commit is contained in:
		
							parent
							
								
									1cf247f164
								
							
						
					
					
						commit
						a5735961a4
					
				| @ -5,6 +5,7 @@ const router = express.Router(); | |||||||
| const sql = require('../../services/sql'); | const sql = require('../../services/sql'); | ||||||
| const utils = require('../../services/utils'); | const utils = require('../../services/utils'); | ||||||
| const migration = require('../../services/migration'); | const migration = require('../../services/migration'); | ||||||
|  | const SOURCE_ID = require('../../services/source_id'); | ||||||
| 
 | 
 | ||||||
| router.post('', async (req, res, next) => { | router.post('', async (req, res, next) => { | ||||||
|     const timestamp = req.body.timestamp; |     const timestamp = req.body.timestamp; | ||||||
| @ -35,7 +36,9 @@ router.post('', async (req, res, next) => { | |||||||
| 
 | 
 | ||||||
|     req.session.loggedIn = true; |     req.session.loggedIn = true; | ||||||
| 
 | 
 | ||||||
|     res.send({}); |     res.send({ | ||||||
|  |         sourceId: SOURCE_ID | ||||||
|  |     }); | ||||||
| }); | }); | ||||||
| 
 | 
 | ||||||
| module.exports = router; | module.exports = router; | ||||||
| @ -15,7 +15,7 @@ const isSyncSetup = !!SYNC_SERVER; | |||||||
| 
 | 
 | ||||||
| let syncInProgress = false; | let syncInProgress = false; | ||||||
| 
 | 
 | ||||||
| async function pullSync(cookieJar, syncLog) { | async function pullSync(syncContext, syncLog) { | ||||||
|     const lastSyncedPull = parseInt(await sql.getOption('last_synced_pull')); |     const lastSyncedPull = parseInt(await sql.getOption('last_synced_pull')); | ||||||
| 
 | 
 | ||||||
|     let syncRows; |     let syncRows; | ||||||
| @ -25,7 +25,7 @@ async function pullSync(cookieJar, syncLog) { | |||||||
| 
 | 
 | ||||||
|         syncRows = await rp({ |         syncRows = await rp({ | ||||||
|             uri: SYNC_SERVER + '/api/sync/changed?lastSyncId=' + lastSyncedPull + "&sourceId=" + SOURCE_ID, |             uri: SYNC_SERVER + '/api/sync/changed?lastSyncId=' + lastSyncedPull + "&sourceId=" + SOURCE_ID, | ||||||
|             jar: cookieJar, |             jar: syncContext.cookieJar, | ||||||
|             json: true, |             json: true, | ||||||
|             timeout: 5 * 1000 |             timeout: 5 * 1000 | ||||||
|         }); |         }); | ||||||
| @ -43,7 +43,7 @@ async function pullSync(cookieJar, syncLog) { | |||||||
|             resp = await rp({ |             resp = await rp({ | ||||||
|                 uri: SYNC_SERVER + "/api/sync/" + sync.entity_name + "/" + sync.entity_id, |                 uri: SYNC_SERVER + "/api/sync/" + sync.entity_name + "/" + sync.entity_id, | ||||||
|                 json: true, |                 json: true, | ||||||
|                 jar: cookieJar |                 jar: syncContext.cookieJar | ||||||
|             }); |             }); | ||||||
|         } |         } | ||||||
|         catch (e) { |         catch (e) { | ||||||
| @ -69,7 +69,7 @@ async function pullSync(cookieJar, syncLog) { | |||||||
|     logSync("Finished pull"); |     logSync("Finished pull"); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| async function pushEntity(entity, entityName, cookieJar, syncLog) { | async function sendEntity(entity, entityName, cookieJar, syncLog) { | ||||||
|     try { |     try { | ||||||
|         const payload = { |         const payload = { | ||||||
|             sourceId: SOURCE_ID, |             sourceId: SOURCE_ID, | ||||||
| @ -94,7 +94,28 @@ async function pushEntity(entity, entityName, cookieJar, syncLog) { | |||||||
|     } |     } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| async function pushSync(cookieJar, syncLog) { | async function readAndPushEntity(sync, syncLog, syncContext) { | ||||||
|  |     let entity; | ||||||
|  | 
 | ||||||
|  |     if (sync.entity_name === 'notes') { | ||||||
|  |         entity = await sql.getSingleResult('SELECT * FROM notes WHERE note_id = ?', [sync.entity_id]); | ||||||
|  |     } | ||||||
|  |     else if (sync.entity_name === 'notes_tree') { | ||||||
|  |         entity = await sql.getSingleResult('SELECT * FROM notes_tree WHERE note_id = ?', [sync.entity_id]); | ||||||
|  |     } | ||||||
|  |     else if (sync.entity_name === 'notes_history') { | ||||||
|  |         entity = await sql.getSingleResult('SELECT * FROM notes_history WHERE note_history_id = ?', [sync.entity_id]); | ||||||
|  |     } | ||||||
|  |     else { | ||||||
|  |         logSyncError("Unrecognized entity type " + sync.entity_name, null, syncLog); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     logSync("Pushing changes in " + sync.entity_name + " " + sync.entity_id); | ||||||
|  | 
 | ||||||
|  |     await sendEntity(entity, sync.entity_name, syncContext.cookieJar, syncLog); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | async function pushSync(syncContext, syncLog) { | ||||||
|     let lastSyncedPush = parseInt(await sql.getOption('last_synced_push')); |     let lastSyncedPush = parseInt(await sql.getOption('last_synced_push')); | ||||||
| 
 | 
 | ||||||
|     while (true) { |     while (true) { | ||||||
| @ -108,25 +129,15 @@ async function pushSync(cookieJar, syncLog) { | |||||||
|             break; |             break; | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         let entity; |         console.log("sync: ", sync); | ||||||
| 
 | 
 | ||||||
|         if (sync.entity_name === 'notes') { |         if (sync.sourceId === syncContext.sourceId) { | ||||||
|             entity = await sql.getSingleResult('SELECT * FROM notes WHERE note_id = ?', [sync.entity_id]); |             logSync("Skipping sync " + sync.entity_name + " " + sync.entity_id + " because it originates from sync target", syncLog); | ||||||
|         } |  | ||||||
|         else if (sync.entity_name === 'notes_tree') { |  | ||||||
|             entity = await sql.getSingleResult('SELECT * FROM notes_tree WHERE note_id = ?', [sync.entity_id]); |  | ||||||
|         } |  | ||||||
|         else if (sync.entity_name === 'notes_history') { |  | ||||||
|             entity = await sql.getSingleResult('SELECT * FROM notes_history WHERE note_history_id = ?', [sync.entity_id]); |  | ||||||
|         } |         } | ||||||
|         else { |         else { | ||||||
|             logSyncError("Unrecognized entity type " + sync.entity_name, null, syncLog); |             await readAndPushEntity(sync, syncLog, syncContext); | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         logSync("Pushing changes in " + sync.entity_name + " " + sync.entity_id); |  | ||||||
| 
 |  | ||||||
|         await pushEntity(entity, sync.entity_name, cookieJar, syncLog); |  | ||||||
| 
 |  | ||||||
|         lastSyncedPush = sync.id; |         lastSyncedPush = sync.id; | ||||||
| 
 | 
 | ||||||
|         await sql.setOption('last_synced_push', lastSyncedPush); |         await sql.setOption('last_synced_push', lastSyncedPush); | ||||||
| @ -142,7 +153,7 @@ async function login(syncLog) { | |||||||
|     const cookieJar = rp.jar(); |     const cookieJar = rp.jar(); | ||||||
| 
 | 
 | ||||||
|     try { |     try { | ||||||
|         await rp({ |         const resp = await rp({ | ||||||
|             method: 'POST', |             method: 'POST', | ||||||
|             uri: SYNC_SERVER + '/api/login', |             uri: SYNC_SERVER + '/api/login', | ||||||
|             body: { |             body: { | ||||||
| @ -155,7 +166,10 @@ async function login(syncLog) { | |||||||
|             jar: cookieJar |             jar: cookieJar | ||||||
|         }); |         }); | ||||||
| 
 | 
 | ||||||
|         return cookieJar; |         return { | ||||||
|  |             cookieJar: cookieJar, | ||||||
|  |             sourceId: resp.sourceId | ||||||
|  |         }; | ||||||
|     } |     } | ||||||
|     catch (e) { |     catch (e) { | ||||||
|         logSyncError("Can't login to API for sync, inner exception: ", e, syncLog); |         logSyncError("Can't login to API for sync, inner exception: ", e, syncLog); | ||||||
| @ -180,11 +194,13 @@ async function sync() { | |||||||
|             return syncLog; |             return syncLog; | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         const cookieJar = await login(syncLog); |         const syncContext = await login(syncLog); | ||||||
| 
 | 
 | ||||||
|         await pullSync(cookieJar, syncLog); |         console.log("sync context: ", syncContext); | ||||||
| 
 | 
 | ||||||
|         await pushSync(cookieJar, syncLog); |         await pullSync(syncContext, syncLog); | ||||||
|  | 
 | ||||||
|  |         await pushSync(syncContext, syncLog); | ||||||
|     } |     } | ||||||
|     catch (e) { |     catch (e) { | ||||||
|         logSync("sync failed: " + e.stack, syncLog); |         logSync("sync failed: " + e.stack, syncLog); | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 azivner
						azivner