trilium/services/script_context.js

95 lines
2.7 KiB
JavaScript
Raw Normal View History

const log = require('./log');
const protected_session = require('./protected_session');
const notes = require('./notes');
const attributes = require('./attributes');
const date_notes = require('./date_notes');
const sql = require('./sql');
const sync_table = require('./sync_table');
function ScriptContext(noteId, dataKey) {
this.scriptNoteId = noteId;
this.dataKey = protected_session.getDataKey(dataKey);
function deserializePayload(note) {
if (note.type === "code" && note.mime === "application/json") {
note.payload = JSON.parse(note.note_text);
}
}
2018-01-28 07:02:44 +08:00
function serializePayload(payload) {
return JSON.stringify(payload, null, '\t');
}
this.getNoteById = async function(noteId) {
const note = await notes.getNoteById(noteId, this.dataKey);
deserializePayload(note);
return note;
};
this.getNoteWithAttribute = async function (attrName, attrValue) {
const note = await attributes.getNoteWithAttribute(this.dataKey, attrName, attrValue);
deserializePayload(note);
return note;
};
this.createNote = async function (parentNoteId, name, payload, extraOptions = {}) {
const note = {
note_title: name,
2018-01-28 07:02:44 +08:00
note_text: extraOptions.json ? serializePayload(payload) : payload,
target: 'into',
is_protected: extraOptions.isProtected !== undefined ? extraOptions.isProtected : false,
type: extraOptions.type,
mime: extraOptions.mime
};
if (extraOptions.json) {
note.type = "code";
note.mime = "application/json";
}
if (!note.type) {
note.type = "text";
note.mime = "";
}
const noteId = (await notes.createNewNote(parentNoteId, note)).noteId;
if (extraOptions.attributes) {
for (const attrName in extraOptions.attributes) {
await attributes.createAttribute(noteId, attrName, extraOptions.attributes[attrName]);
}
}
return noteId;
};
this.updateNote = async function (note) {
if (note.type === 'code' && note.mime === 'application/json') {
2018-01-28 07:02:44 +08:00
note.note_text = serializePayload(note.payload);
}
log.info("new note text: ", note.note_text);
delete note.payload;
if (note.is_protected) {
protected_session.encryptNote(this.dataKey, note);
}
await sql.replace("notes", note);
await sync_table.addNoteSync(note.note_id);
};
this.log = function(message) {
log.info(`Script ${this.scriptNoteId}: ${message}`);
};
this.getDateNoteId = date_notes.getDateNoteId;
}
module.exports = ScriptContext;