mirror of
https://github.com/zadam/trilium.git
synced 2025-11-17 10:40:41 +01:00
converted note revision protection to repository/entities
This commit is contained in:
@@ -4,6 +4,7 @@ const utils = require('./utils');
|
||||
const sync_table = require('./sync_table');
|
||||
const labels = require('./labels');
|
||||
const protected_session = require('./protected_session');
|
||||
const repository = require('./repository');
|
||||
|
||||
async function createNewNote(parentNoteId, noteOpts) {
|
||||
const noteId = utils.newNoteId();
|
||||
@@ -117,15 +118,11 @@ async function createNote(parentNoteId, title, content = "", extraOptions = {})
|
||||
return noteId;
|
||||
}
|
||||
|
||||
async function protectNoteRecursively(noteId, protect) {
|
||||
const note = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [noteId]);
|
||||
|
||||
async function protectNoteRecursively(note, protect) {
|
||||
await protectNote(note, protect);
|
||||
|
||||
const children = await sql.getColumn("SELECT noteId FROM branches WHERE parentNoteId = ? AND isDeleted = 0", [noteId]);
|
||||
|
||||
for (const childNoteId of children) {
|
||||
await protectNoteRecursively(childNoteId, protect);
|
||||
for (const child of await note.getChildren()) {
|
||||
await protectNoteRecursively(child, protect);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -133,47 +130,43 @@ async function protectNote(note, protect) {
|
||||
let changed = false;
|
||||
|
||||
if (protect && !note.isProtected) {
|
||||
protected_session.encryptNote(note);
|
||||
|
||||
note.isProtected = true;
|
||||
|
||||
changed = true;
|
||||
}
|
||||
else if (!protect && note.isProtected) {
|
||||
protected_session.decryptNote(note);
|
||||
|
||||
note.isProtected = false;
|
||||
|
||||
changed = true;
|
||||
}
|
||||
|
||||
if (changed) {
|
||||
await sql.execute("UPDATE notes SET title = ?, content = ?, isProtected = ? WHERE noteId = ?",
|
||||
[note.title, note.content, note.isProtected, note.noteId]);
|
||||
await repository.updateEntity(note);
|
||||
|
||||
await sync_table.addNoteSync(note.noteId);
|
||||
}
|
||||
|
||||
await protectNoteRevisions(note.noteId, protect);
|
||||
await protectNoteRevisions(note, protect);
|
||||
}
|
||||
|
||||
async function protectNoteRevisions(noteId, protect) {
|
||||
const revisionsToChange = await sql.getRows("SELECT * FROM note_revisions WHERE noteId = ? AND isProtected != ?", [noteId, protect]);
|
||||
|
||||
for (const revision of revisionsToChange) {
|
||||
if (protect) {
|
||||
protected_session.encryptNoteRevision(revision);
|
||||
async function protectNoteRevisions(note, protect) {
|
||||
for (const revision of await note.getRevisions()) {
|
||||
let changed = false;
|
||||
|
||||
if (protect && !revision.isProtected) {
|
||||
revision.isProtected = true;
|
||||
}
|
||||
else {
|
||||
protected_session.decryptNoteRevision(revision);
|
||||
|
||||
changed = true;
|
||||
}
|
||||
else if(!protect && revision.isProtected) {
|
||||
revision.isProtected = false;
|
||||
|
||||
changed = true;
|
||||
}
|
||||
|
||||
await sql.execute("UPDATE note_revisions SET title = ?, content = ?, isProtected = ? WHERE noteRevisionId = ?",
|
||||
[revision.title, revision.content, revision.isProtected, revision.noteRevisionId]);
|
||||
if (changed) {
|
||||
await repository.updateEntity(revision);
|
||||
}
|
||||
|
||||
await sync_table.addNoteRevisionSync(revision.noteRevisionId);
|
||||
}
|
||||
@@ -298,7 +291,7 @@ async function updateNote(noteId, newNote) {
|
||||
|
||||
await saveNoteImages(noteId, newNote.content);
|
||||
|
||||
await protectNoteRevisions(noteId, newNote.isProtected);
|
||||
await protectNoteRevisions(await repository.getNote(noteId), newNote.isProtected);
|
||||
|
||||
await sql.execute("UPDATE notes SET title = ?, content = ?, isProtected = ?, dateModified = ? WHERE noteId = ?", [
|
||||
newNote.title,
|
||||
|
||||
@@ -1,16 +1,18 @@
|
||||
"use strict";
|
||||
|
||||
const sql = require('./sql');
|
||||
const Note = require('../entities/note');
|
||||
const NoteRevision = require('../entities/note_revision');
|
||||
const Branch = require('../entities/branch');
|
||||
const Label = require('../entities/label');
|
||||
const sync_table = require('../services/sync_table');
|
||||
|
||||
let entityConstructor;
|
||||
|
||||
async function setEntityConstructor(constructor) {
|
||||
entityConstructor = constructor;
|
||||
}
|
||||
|
||||
async function getEntities(query, params = []) {
|
||||
const rows = await sql.getRows(query, params);
|
||||
|
||||
return rows.map(row => this.createEntityFromRow(row));
|
||||
return rows.map(entityConstructor);
|
||||
}
|
||||
|
||||
async function getEntity(query, params = []) {
|
||||
@@ -20,33 +22,11 @@ async function getEntity(query, params = []) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return this.createEntityFromRow(row);
|
||||
return entityConstructor(row);
|
||||
}
|
||||
|
||||
async function getNote(noteId) {
|
||||
return await this.getEntity("SELECT * FROM notes WHERE noteId = ?", [noteId]);
|
||||
}
|
||||
|
||||
function createEntityFromRow(row) {
|
||||
let entity;
|
||||
|
||||
if (row.labelId) {
|
||||
entity = new Label(row);
|
||||
}
|
||||
else if (row.noteRevisionId) {
|
||||
entity = new NoteRevision(row);
|
||||
}
|
||||
else if (row.branchId) {
|
||||
entity = new Branch(row);
|
||||
}
|
||||
else if (row.noteId) {
|
||||
entity = new Note(row);
|
||||
}
|
||||
else {
|
||||
throw new Error('Unknown entity type for row: ' + JSON.stringify(row));
|
||||
}
|
||||
|
||||
return entity;
|
||||
return await getEntity("SELECT * FROM notes WHERE noteId = ?", [noteId]);
|
||||
}
|
||||
|
||||
async function updateEntity(entity) {
|
||||
@@ -69,5 +49,6 @@ module.exports = {
|
||||
getEntities,
|
||||
getEntity,
|
||||
getNote,
|
||||
updateEntity
|
||||
updateEntity,
|
||||
setEntityConstructor
|
||||
};
|
||||
@@ -41,7 +41,7 @@ function ScriptApi(startNote, currentNote) {
|
||||
this.getInstanceName = () => config.General ? config.General.instanceName : null;
|
||||
|
||||
this.getNoteById = async function(noteId) {
|
||||
return repository.getNote(noteId);
|
||||
return await repository.getNote(noteId);
|
||||
};
|
||||
|
||||
this.getNotesWithLabel = async function (attrName, attrValue) {
|
||||
|
||||
Reference in New Issue
Block a user