mirror of
https://github.com/zadam/trilium.git
synced 2025-03-01 14:22:32 +01: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;
|
||||
|
||||
res.send({
|
||||
sourceId: source_id.currentSourceId
|
||||
sourceId: await source_id.getCurrentSourceId()
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -4,12 +4,10 @@ const express = require('express');
|
||||
const router = express.Router();
|
||||
const auth = require('../../services/auth');
|
||||
const sql = require('../../services/sql');
|
||||
const utils = require('../../services/utils');
|
||||
const notes = require('../../services/notes');
|
||||
const log = require('../../services/log');
|
||||
const protected_session = require('../../services/protected_session');
|
||||
const data_encryption = require('../../services/data_encryption');
|
||||
const RequestContext = require('../../services/request_context');
|
||||
|
||||
router.get('/:noteId', auth.checkApiAuth, async (req, res, next) => {
|
||||
const noteId = req.params.noteId;
|
||||
@ -50,9 +48,10 @@ router.post('/:parentNoteId/children', async (req, res, next) => {
|
||||
router.put('/:noteId', async (req, res, next) => {
|
||||
const note = req.body;
|
||||
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({});
|
||||
});
|
||||
|
@ -63,9 +63,9 @@ async function createNewNote(parentNoteId, note, sourceId) {
|
||||
};
|
||||
}
|
||||
|
||||
async function encryptNote(note, ctx) {
|
||||
note.detail.note_title = data_encryption.encrypt(ctx.getDataKey(), 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);
|
||||
async function encryptNote(note, dataKey) {
|
||||
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(dataKey, data_encryption.noteTextIv(note.detail.note_id), note.detail.note_text);
|
||||
}
|
||||
|
||||
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) {
|
||||
await encryptNote(newNote, ctx);
|
||||
await encryptNote(newNote, dataKey);
|
||||
}
|
||||
|
||||
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]);
|
||||
|
||||
if (oldNote.is_protected) {
|
||||
decryptNote(oldNote, ctx.getDataKey());
|
||||
decryptNote(oldNote, dataKey);
|
||||
}
|
||||
|
||||
const newNoteHistoryId = utils.newNoteHistoryId();
|
||||
@ -174,10 +174,10 @@ async function updateNote(noteId, newNote, ctx) {
|
||||
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 = ?", [
|
||||
newNote.detail.note_title,
|
||||
@ -186,7 +186,7 @@ async function updateNote(noteId, newNote, ctx) {
|
||||
nowStr,
|
||||
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);
|
||||
}
|
||||
|
||||
const currentSourceId = generateSourceId();
|
||||
const currentSourceIdPromise = generateSourceId();
|
||||
|
||||
async function getCurrentSourceId() {
|
||||
return await currentSourceIdPromise;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
generateSourceId,
|
||||
currentSourceId,
|
||||
getCurrentSourceId,
|
||||
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`);
|
||||
}
|
||||
else {
|
||||
await readAndPushEntity(sync, syncContext);
|
||||
await pushEntity(sync, syncContext);
|
||||
}
|
||||
|
||||
lastSyncedPush = sync.id;
|
||||
@ -195,7 +195,7 @@ async function pushSync(syncContext) {
|
||||
}
|
||||
}
|
||||
|
||||
async function readAndPushEntity(sync, syncContext) {
|
||||
async function pushEntity(sync, syncContext) {
|
||||
let entity;
|
||||
|
||||
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}`);
|
||||
|
||||
await sendEntity(syncContext, entity, sync.entity_name);
|
||||
}
|
||||
|
||||
async function sendEntity(syncContext, entity, entityName) {
|
||||
const payload = {
|
||||
sourceId: source_id.currentSourceId,
|
||||
sourceId: await source_id.getCurrentSourceId(),
|
||||
entity: entity
|
||||
};
|
||||
|
||||
await syncRequest(syncContext, 'PUT', '/api/sync/' + entityName, payload);
|
||||
await syncRequest(syncContext, 'PUT', '/api/sync/' + sync.entity_name, payload);
|
||||
}
|
||||
|
||||
async function checkContentHash(syncContext) {
|
||||
|
@ -32,7 +32,7 @@ async function addEntitySync(entityName, entityId, sourceId) {
|
||||
entity_name: entityName,
|
||||
entity_id: entityId,
|
||||
sync_date: utils.nowDate(),
|
||||
source_id: sourceId || source_id.currentSourceId
|
||||
source_id: sourceId || await source_id.getCurrentSourceId()
|
||||
});
|
||||
|
||||
if (!sync_setup.isSyncSetup) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user