mirror of
				https://github.com/TriliumNext/Notes.git
				synced 2025-10-31 04:51:31 +08:00 
			
		
		
		
	got rid of request context and related refactorings
This commit is contained in:
		
							parent
							
								
									50ff5da947
								
							
						
					
					
						commit
						e206269457
					
				| @ -42,7 +42,7 @@ router.post('/sync', async (req, res, next) => { | |||||||
|     req.session.loggedIn = true; |     req.session.loggedIn = true; | ||||||
| 
 | 
 | ||||||
|     res.send({ |     res.send({ | ||||||
|         sourceId: source_id.currentSourceId |         sourceId: await source_id.getCurrentSourceId() | ||||||
|     }); |     }); | ||||||
| }); | }); | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -4,12 +4,10 @@ const express = require('express'); | |||||||
| const router = express.Router(); | const router = express.Router(); | ||||||
| const auth = require('../../services/auth'); | const auth = require('../../services/auth'); | ||||||
| const sql = require('../../services/sql'); | const sql = require('../../services/sql'); | ||||||
| const utils = require('../../services/utils'); |  | ||||||
| const notes = require('../../services/notes'); | const notes = require('../../services/notes'); | ||||||
| const log = require('../../services/log'); | const log = require('../../services/log'); | ||||||
| const protected_session = require('../../services/protected_session'); | const protected_session = require('../../services/protected_session'); | ||||||
| const data_encryption = require('../../services/data_encryption'); | const data_encryption = require('../../services/data_encryption'); | ||||||
| const RequestContext = require('../../services/request_context'); |  | ||||||
| 
 | 
 | ||||||
| router.get('/:noteId', auth.checkApiAuth, async (req, res, next) => { | router.get('/:noteId', auth.checkApiAuth, async (req, res, next) => { | ||||||
|     const noteId = req.params.noteId; |     const noteId = req.params.noteId; | ||||||
| @ -50,9 +48,10 @@ router.post('/:parentNoteId/children', async (req, res, next) => { | |||||||
| router.put('/:noteId', async (req, res, next) => { | router.put('/:noteId', async (req, res, next) => { | ||||||
|     const note = req.body; |     const note = req.body; | ||||||
|     const noteId = req.params.noteId; |     const noteId = req.params.noteId; | ||||||
|     const reqCtx = new RequestContext(req); |     const sourceId = req.headers.source_id; | ||||||
|  |     const dataKey = protected_session.getDataKey(req); | ||||||
| 
 | 
 | ||||||
|     await notes.updateNote(noteId, note, reqCtx); |     await notes.updateNote(noteId, note, dataKey, sourceId); | ||||||
| 
 | 
 | ||||||
|     res.send({}); |     res.send({}); | ||||||
| }); | }); | ||||||
|  | |||||||
| @ -63,9 +63,9 @@ async function createNewNote(parentNoteId, note, sourceId) { | |||||||
|     }; |     }; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| async function encryptNote(note, ctx) { | async function encryptNote(note, dataKey) { | ||||||
|     note.detail.note_title = data_encryption.encrypt(ctx.getDataKey(), data_encryption.noteTitleIv(note.detail.note_id), note.detail.note_title); |     note.detail.note_title = data_encryption.encrypt(dataKey, data_encryption.noteTitleIv(note.detail.note_id), note.detail.note_title); | ||||||
|     note.detail.note_text = data_encryption.encrypt(ctx.getDataKey(), data_encryption.noteTextIv(note.detail.note_id), note.detail.note_text); |     note.detail.note_text = data_encryption.encrypt(dataKey, data_encryption.noteTextIv(note.detail.note_id), note.detail.note_text); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| async function protectNoteRecursively(noteId, dataKey, protect, sourceId) { | async function protectNoteRecursively(noteId, dataKey, protect, sourceId) { | ||||||
| @ -136,9 +136,9 @@ async function protectNoteHistory(noteId, dataKey, protect, sourceId) { | |||||||
|     } |     } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| async function updateNote(noteId, newNote, ctx) { | async function updateNote(noteId, newNote, dataKey, sourceId) { | ||||||
|     if (newNote.detail.is_protected) { |     if (newNote.detail.is_protected) { | ||||||
|         await encryptNote(newNote, ctx); |         await encryptNote(newNote, dataKey); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     const now = new Date(); |     const now = new Date(); | ||||||
| @ -158,7 +158,7 @@ async function updateNote(noteId, newNote, ctx) { | |||||||
|             const oldNote = await sql.getSingleResult("SELECT * FROM notes WHERE note_id = ?", [noteId]); |             const oldNote = await sql.getSingleResult("SELECT * FROM notes WHERE note_id = ?", [noteId]); | ||||||
| 
 | 
 | ||||||
|             if (oldNote.is_protected) { |             if (oldNote.is_protected) { | ||||||
|                 decryptNote(oldNote, ctx.getDataKey()); |                 decryptNote(oldNote, dataKey); | ||||||
|             } |             } | ||||||
| 
 | 
 | ||||||
|             const newNoteHistoryId = utils.newNoteHistoryId(); |             const newNoteHistoryId = utils.newNoteHistoryId(); | ||||||
| @ -174,10 +174,10 @@ async function updateNote(noteId, newNote, ctx) { | |||||||
|                 date_modified_to: nowStr |                 date_modified_to: nowStr | ||||||
|             }); |             }); | ||||||
| 
 | 
 | ||||||
|             await sync_table.addNoteHistorySync(newNoteHistoryId, ctx.sourceId); |             await sync_table.addNoteHistorySync(newNoteHistoryId, sourceId); | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         await protectNoteHistory(noteId, ctx.getDataKeyOrNull(), newNote.detail.is_protected); |         await protectNoteHistory(noteId, dataKey, newNote.detail.is_protected); | ||||||
| 
 | 
 | ||||||
|         await sql.execute("UPDATE notes SET note_title = ?, note_text = ?, is_protected = ?, date_modified = ? WHERE note_id = ?", [ |         await sql.execute("UPDATE notes SET note_title = ?, note_text = ?, is_protected = ?, date_modified = ? WHERE note_id = ?", [ | ||||||
|             newNote.detail.note_title, |             newNote.detail.note_title, | ||||||
| @ -186,7 +186,7 @@ async function updateNote(noteId, newNote, ctx) { | |||||||
|             nowStr, |             nowStr, | ||||||
|             noteId]); |             noteId]); | ||||||
| 
 | 
 | ||||||
|         await sync_table.addNoteSync(noteId, ctx.sourceId); |         await sync_table.addNoteSync(noteId, sourceId); | ||||||
|     }); |     }); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -1,34 +0,0 @@ | |||||||
| "use strict"; |  | ||||||
| 
 |  | ||||||
| const protected_session = require('./protected_session'); |  | ||||||
| 
 |  | ||||||
| module.exports = function(req) { |  | ||||||
|     const sourceId = req.headers.source_id; |  | ||||||
| 
 |  | ||||||
|     function isProtectedSessionAvailable() { |  | ||||||
|         return protected_session.isProtectedSessionAvailable(req); |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     function getDataKey() { |  | ||||||
|         if (!isProtectedSessionAvailable()) { |  | ||||||
|             throw new Error("Protected session is not available"); |  | ||||||
|         } |  | ||||||
| 
 |  | ||||||
|         return protected_session.getDataKey(req); |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     function getDataKeyOrNull() { |  | ||||||
|         if (!isProtectedSessionAvailable()) { |  | ||||||
|             return null; |  | ||||||
|         } |  | ||||||
| 
 |  | ||||||
|         return protected_session.getDataKey(req); |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     return { |  | ||||||
|         sourceId, |  | ||||||
|         isProtectedSessionAvailable, |  | ||||||
|         getDataKey, |  | ||||||
|         getDataKeyOrNull |  | ||||||
|     }; |  | ||||||
| }; |  | ||||||
| @ -31,10 +31,14 @@ function isLocalSourceId(srcId) { | |||||||
|     return allSourceIds.includes(srcId); |     return allSourceIds.includes(srcId); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| const currentSourceId = generateSourceId(); | const currentSourceIdPromise = generateSourceId(); | ||||||
|  | 
 | ||||||
|  | async function getCurrentSourceId() { | ||||||
|  |     return await currentSourceIdPromise; | ||||||
|  | } | ||||||
| 
 | 
 | ||||||
| module.exports = { | module.exports = { | ||||||
|     generateSourceId, |     generateSourceId, | ||||||
|     currentSourceId, |     getCurrentSourceId, | ||||||
|     isLocalSourceId |     isLocalSourceId | ||||||
| }; | }; | ||||||
| @ -186,7 +186,7 @@ async function pushSync(syncContext) { | |||||||
|             log.info(`Skipping push #${sync.id} ${sync.entity_name} ${sync.entity_id} because it originates from sync target`); |             log.info(`Skipping push #${sync.id} ${sync.entity_name} ${sync.entity_id} because it originates from sync target`); | ||||||
|         } |         } | ||||||
|         else { |         else { | ||||||
|             await readAndPushEntity(sync, syncContext); |             await pushEntity(sync, syncContext); | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         lastSyncedPush = sync.id; |         lastSyncedPush = sync.id; | ||||||
| @ -195,7 +195,7 @@ async function pushSync(syncContext) { | |||||||
|     } |     } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| async function readAndPushEntity(sync, syncContext) { | async function pushEntity(sync, syncContext) { | ||||||
|     let entity; |     let entity; | ||||||
| 
 | 
 | ||||||
|     if (sync.entity_name === 'notes') { |     if (sync.entity_name === 'notes') { | ||||||
| @ -230,16 +230,12 @@ async function readAndPushEntity(sync, syncContext) { | |||||||
| 
 | 
 | ||||||
|     log.info(`Pushing changes in sync #${sync.id} ${sync.entity_name} ${sync.entity_id}`); |     log.info(`Pushing changes in sync #${sync.id} ${sync.entity_name} ${sync.entity_id}`); | ||||||
| 
 | 
 | ||||||
|     await sendEntity(syncContext, entity, sync.entity_name); |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| async function sendEntity(syncContext, entity, entityName) { |  | ||||||
|     const payload = { |     const payload = { | ||||||
|         sourceId: source_id.currentSourceId, |         sourceId: await source_id.getCurrentSourceId(), | ||||||
|         entity: entity |         entity: entity | ||||||
|     }; |     }; | ||||||
| 
 | 
 | ||||||
|     await syncRequest(syncContext, 'PUT', '/api/sync/' + entityName, payload); |     await syncRequest(syncContext, 'PUT', '/api/sync/' + sync.entity_name, payload); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| async function checkContentHash(syncContext) { | async function checkContentHash(syncContext) { | ||||||
|  | |||||||
| @ -32,7 +32,7 @@ async function addEntitySync(entityName, entityId, sourceId) { | |||||||
|         entity_name: entityName, |         entity_name: entityName, | ||||||
|         entity_id: entityId, |         entity_id: entityId, | ||||||
|         sync_date: utils.nowDate(), |         sync_date: utils.nowDate(), | ||||||
|         source_id: sourceId || source_id.currentSourceId |         source_id: sourceId || await source_id.getCurrentSourceId() | ||||||
|     }); |     }); | ||||||
| 
 | 
 | ||||||
|     if (!sync_setup.isSyncSetup) { |     if (!sync_setup.isSyncSetup) { | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 azivner
						azivner