removed sourceId where it's not necessary (stored in CLS instead)

This commit is contained in:
azivner 2018-03-30 19:41:54 -04:00
parent 795d50f02e
commit 5d203b2278
16 changed files with 67 additions and 85 deletions

View File

@ -38,8 +38,6 @@ async function cleanupSoftDeletedItems() {
}
async function cleanupUnusedImages() {
const sourceId = req.headers.source_id;
const unusedImageIds = await sql.getColumn(`
SELECT images.imageId
FROM images
@ -56,7 +54,7 @@ async function cleanupUnusedImages() {
await sql.execute("UPDATE images SET isDeleted = 1, data = null, dateModified = ? WHERE imageId = ?",
[now, imageId]);
await sync_table.addImageSync(imageId, sourceId);
await sync_table.addImageSync(imageId);
}
}

View File

@ -9,7 +9,6 @@ async function cloneNoteToParent(req) {
const parentNoteId = req.params.parentNoteId;
const childNoteId = req.params.childNoteId;
const prefix = req.body.prefix;
const sourceId = req.headers.source_id;
const validationResult = await tree.validateParentChild(parentNoteId, childNoteId);
@ -33,7 +32,7 @@ async function cloneNoteToParent(req) {
await sql.replace("branches", branch);
await sync_table.addBranchSync(branch.branchId, sourceId);
await sync_table.addBranchSync(branch.branchId);
await sql.execute("UPDATE branches SET isExpanded = 1 WHERE noteId = ?", [parentNoteId]);
@ -43,7 +42,6 @@ async function cloneNoteToParent(req) {
async function cloneNoteAfter(req) {
const noteId = req.params.noteId;
const afterBranchId = req.params.afterBranchId;
const sourceId = req.headers.source_id;
const afterNote = await tree.getBranch(afterBranchId);
@ -58,7 +56,7 @@ async function cloneNoteAfter(req) {
await sql.execute("UPDATE branches SET notePosition = notePosition + 1 WHERE parentNoteId = ? AND notePosition > ? AND isDeleted = 0",
[afterNote.parentNoteId, afterNote.notePosition]);
await sync_table.addNoteReorderingSync(afterNote.parentNoteId, sourceId);
await sync_table.addNoteReorderingSync(afterNote.parentNoteId);
const branch = {
branchId: utils.newBranchId(),
@ -72,7 +70,7 @@ async function cloneNoteAfter(req) {
await sql.replace("branches", branch);
await sync_table.addBranchSync(branch.branchId, sourceId);
await sync_table.addBranchSync(branch.branchId);
return { success: true };
}

View File

@ -6,7 +6,6 @@ const labels = require('../../services/labels');
const protected_session = require('../../services/protected_session');
async function uploadFile(req) {
const sourceId = req.headers.source_id;
const parentNoteId = req.params.parentNoteId;
const file = req.file;
const originalName = file.originalname;
@ -25,10 +24,10 @@ async function uploadFile(req) {
isProtected: false,
type: 'file',
mime: file.mimetype
}, req, sourceId);
}, req);
await labels.createLabel(noteId, "original_file_name", originalName, sourceId);
await labels.createLabel(noteId, "file_size", size, sourceId);
await labels.createLabel(noteId, "original_file_name", originalName);
await labels.createLabel(noteId, "file_size", size);
return {
noteId: noteId

View File

@ -22,7 +22,6 @@ async function returnImage(req, res) {
}
async function uploadImage(req) {
const sourceId = req.headers.source_id;
const noteId = req.query.noteId;
const file = req.file;
@ -36,7 +35,7 @@ async function uploadImage(req) {
return [400, "Unknown image type: " + file.mimetype];
}
const {fileName, imageId} = await image.saveImage(file, sourceId, noteId);
const {fileName, imageId} = await image.saveImage(file, noteId);
return {
uploaded: true,

View File

@ -91,7 +91,6 @@ async function parseImportFile(file) {
}
async function importTar(req) {
const sourceId = req.headers.source_id;
const parentNoteId = req.params.parentNoteId;
const file = req.file;
@ -103,10 +102,10 @@ async function importTar(req) {
const files = await parseImportFile(file);
await importNotes(files, parentNoteId, sourceId);
await importNotes(files, parentNoteId);
}
async function importNotes(files, parentNoteId, sourceId) {
async function importNotes(files, parentNoteId) {
for (const file of files) {
if (file.meta.version !== 1) {
throw new Error("Can't read meta data version " + file.meta.version);
@ -118,8 +117,7 @@ async function importNotes(files, parentNoteId, sourceId) {
const noteId = await notes.createNote(parentNoteId, file.meta.title, file.data, {
type: file.meta.type,
mime: file.meta.mime,
sourceId: sourceId
mime: file.meta.mime
});
for (const attr of file.meta.labels) {
@ -127,7 +125,7 @@ async function importNotes(files, parentNoteId, sourceId) {
}
if (file.children.length > 0) {
await importNotes(file.children, noteId, sourceId);
await importNotes(file.children, noteId);
}
}
}

View File

@ -31,11 +31,10 @@ async function getNote(req) {
}
async function createNote(req) {
const sourceId = req.headers.source_id;
const parentNoteId = req.params.parentNoteId;
const newNote = req.body;
const { noteId, branchId, note } = await notes.createNewNote(parentNoteId, newNote, req, sourceId);
const { noteId, branchId, note } = await notes.createNewNote(parentNoteId, newNote, req);
return {
'noteId': noteId,
@ -47,39 +46,35 @@ async function createNote(req) {
async function updateNote(req) {
const note = req.body;
const noteId = req.params.noteId;
const sourceId = req.headers.source_id;
const dataKey = protected_session.getDataKey(req);
await notes.updateNote(noteId, note, dataKey, sourceId);
await notes.updateNote(noteId, note, dataKey);
}
async function sortNotes(req) {
const noteId = req.params.noteId;
const sourceId = req.headers.source_id;
const dataKey = protected_session.getDataKey(req);
await tree.sortNotesAlphabetically(noteId, dataKey, sourceId);
await tree.sortNotesAlphabetically(noteId, dataKey);
}
async function protectBranch(req) {
const noteId = req.params.noteId;
const isProtected = !!parseInt(req.params.isProtected);
const dataKey = protected_session.getDataKey(req);
const sourceId = req.headers.source_id;
await notes.protectNoteRecursively(noteId, dataKey, isProtected, sourceId);
await notes.protectNoteRecursively(noteId, dataKey, isProtected);
}
async function setNoteTypeMime(req) {
const noteId = req.params[0];
const type = req.params[1];
const mime = req.params[2];
const sourceId = req.headers.source_id;
await sql.execute("UPDATE notes SET type = ?, mime = ?, dateModified = ? WHERE noteId = ?",
[type, mime, utils.nowDate(), noteId]);
await sync_table.addNoteSync(noteId, sourceId);
await sync_table.addNoteSync(noteId);
}
module.exports = {

View File

@ -24,7 +24,6 @@ async function getRecentNotes() {
async function addRecentNote(req) {
const branchId = req.params.branchId;
const notePath = req.params.notePath;
const sourceId = req.headers.source_id;
await sql.replace('recent_notes', {
branchId: branchId,
@ -33,9 +32,9 @@ async function addRecentNote(req) {
isDeleted: 0
});
await sync_table.addRecentNoteSync(branchId, sourceId);
await sync_table.addRecentNoteSync(branchId);
await options.setOption('start_note_path', notePath, sourceId);
await options.setOption('start_note_path', notePath);
return await getRecentNotes();
}

View File

@ -19,13 +19,12 @@ async function getAllowedSettings() {
async function updateSetting(req) {
const body = req.body;
const sourceId = req.headers.source_id;
if (!ALLOWED_OPTIONS.includes(body['name'])) {
return [400, "not allowed option to set"];
}
await options.setOption(body['name'], body['value'], sourceId);
await options.setOption(body['name'], body['value']);
}
module.exports = {

View File

@ -62,13 +62,12 @@ async function getTree(req) {
async function setPrefix(req) {
const branchId = req.params.branchId;
const sourceId = req.headers.source_id;
const prefix = utils.isEmptyOrWhitespace(req.body.prefix) ? null : req.body.prefix;
await sql.doInTransaction(async () => {
await sql.execute("UPDATE branches SET prefix = ?, dateModified = ? WHERE branchId = ?", [prefix, utils.nowDate(), branchId]);
await sync_table.addBranchSync(branchId, sourceId);
await sync_table.addBranchSync(branchId);
});
return {};

View File

@ -14,7 +14,6 @@ const notes = require('../../services/notes');
async function moveBranchToParent(req) {
const branchId = req.params.branchId;
const parentNoteId = req.params.parentNoteId;
const sourceId = req.headers.source_id;
const noteToMove = await tree.getBranch(branchId);
@ -32,7 +31,7 @@ async function moveBranchToParent(req) {
await sql.execute("UPDATE branches SET parentNoteId = ?, notePosition = ?, dateModified = ? WHERE branchId = ?",
[parentNoteId, newNotePos, now, branchId]);
await sync_table.addBranchSync(branchId, sourceId);
await sync_table.addBranchSync(branchId);
return { success: true };
}
@ -40,7 +39,6 @@ async function moveBranchToParent(req) {
async function moveBranchBeforeNote(req) {
const branchId = req.params.branchId;
const beforeBranchId = req.params.beforeBranchId;
const sourceId = req.headers.source_id;
const noteToMove = await tree.getBranch(branchId);
const beforeNote = await tree.getBranch(beforeBranchId);
@ -56,12 +54,12 @@ async function moveBranchBeforeNote(req) {
await sql.execute("UPDATE branches SET notePosition = notePosition + 1 WHERE parentNoteId = ? AND notePosition >= ? AND isDeleted = 0",
[beforeNote.parentNoteId, beforeNote.notePosition]);
await sync_table.addNoteReorderingSync(beforeNote.parentNoteId, sourceId);
await sync_table.addNoteReorderingSync(beforeNote.parentNoteId);
await sql.execute("UPDATE branches SET parentNoteId = ?, notePosition = ?, dateModified = ? WHERE branchId = ?",
[beforeNote.parentNoteId, beforeNote.notePosition, utils.nowDate(), branchId]);
await sync_table.addBranchSync(branchId, sourceId);
await sync_table.addBranchSync(branchId);
return { success: true };
}
@ -69,7 +67,6 @@ async function moveBranchBeforeNote(req) {
async function moveBranchAfterNote(req) {
const branchId = req.params.branchId;
const afterBranchId = req.params.afterBranchId;
const sourceId = req.headers.source_id;
const noteToMove = await tree.getBranch(branchId);
const afterNote = await tree.getBranch(afterBranchId);
@ -85,12 +82,12 @@ async function moveBranchAfterNote(req) {
await sql.execute("UPDATE branches SET notePosition = notePosition + 1 WHERE parentNoteId = ? AND notePosition > ? AND isDeleted = 0",
[afterNote.parentNoteId, afterNote.notePosition]);
await sync_table.addNoteReorderingSync(afterNote.parentNoteId, sourceId);
await sync_table.addNoteReorderingSync(afterNote.parentNoteId);
await sql.execute("UPDATE branches SET parentNoteId = ?, notePosition = ?, dateModified = ? WHERE branchId = ?",
[afterNote.parentNoteId, afterNote.notePosition + 1, utils.nowDate(), branchId]);
await sync_table.addBranchSync(branchId, sourceId);
await sync_table.addBranchSync(branchId);
return { success: true };
}
@ -104,7 +101,7 @@ async function setExpanded(req) {
}
async function deleteBranch(req) {
await notes.deleteNote(req.params.branchId, req.headers.source_id);
await notes.deleteNote(req.params.branchId);
}
module.exports = {

View File

@ -11,7 +11,7 @@ const jimp = require('jimp');
const imageType = require('image-type');
const sanitizeFilename = require('sanitize-filename');
async function saveImage(file, sourceId, noteId) {
async function saveImage(file, noteId) {
const resizedImage = await resize(file.buffer);
const optimizedImage = await optimize(resizedImage);
@ -35,7 +35,7 @@ async function saveImage(file, sourceId, noteId) {
dateCreated: now
});
await sync_table.addImageSync(imageId, sourceId);
await sync_table.addImageSync(imageId);
const noteImageId = utils.newNoteImageId();
@ -48,7 +48,7 @@ async function saveImage(file, sourceId, noteId) {
dateCreated: now
});
await sync_table.addNoteImageSync(noteImageId, sourceId);
await sync_table.addNoteImageSync(noteImageId);
});
return {fileName, imageId};
}

View File

@ -55,7 +55,7 @@ async function getNoteIdsWithLabel(name) {
WHERE notes.isDeleted = 0 AND labels.isDeleted = 0 AND labels.name = ? AND labels.isDeleted = 0`, [name]);
}
async function createLabel(noteId, name, value = "", sourceId = null) {
async function createLabel(noteId, name, value = "") {
if (value === null || value === undefined) {
value = "";
}
@ -75,7 +75,7 @@ async function createLabel(noteId, name, value = "", sourceId = null) {
isDeleted: false
});
await sync_table.addLabelSync(labelId, sourceId);
await sync_table.addLabelSync(labelId);
}
module.exports = {

View File

@ -5,7 +5,7 @@ const sync_table = require('./sync_table');
const labels = require('./labels');
const protected_session = require('./protected_session');
async function createNewNote(parentNoteId, noteOpts, dataKey, sourceId) {
async function createNewNote(parentNoteId, noteOpts, dataKey) {
const noteId = utils.newNoteId();
const branchId = utils.newBranchId();
@ -25,7 +25,7 @@ async function createNewNote(parentNoteId, noteOpts, dataKey, sourceId) {
await sql.execute('UPDATE branches SET notePosition = notePosition + 1 WHERE parentNoteId = ? AND notePosition > ? AND isDeleted = 0',
[parentNoteId, afterNote.notePosition]);
await sync_table.addNoteReorderingSync(parentNoteId, sourceId);
await sync_table.addNoteReorderingSync(parentNoteId);
}
else {
throw new Error('Unknown target: ' + noteOpts.target);
@ -62,7 +62,7 @@ async function createNewNote(parentNoteId, noteOpts, dataKey, sourceId) {
await sql.insert("notes", note);
await sync_table.addNoteSync(noteId, sourceId);
await sync_table.addNoteSync(noteId);
await sql.insert("branches", {
branchId: branchId,
@ -74,7 +74,7 @@ async function createNewNote(parentNoteId, noteOpts, dataKey, sourceId) {
isDeleted: 0
});
await sync_table.addBranchSync(branchId, sourceId);
await sync_table.addBranchSync(branchId);
return {
noteId,
@ -106,7 +106,7 @@ async function createNote(parentNoteId, title, content = "", extraOptions = {})
note.mime = "text/html";
}
const {noteId} = await createNewNote(parentNoteId, note, extraOptions.dataKey, extraOptions.sourceId);
const {noteId} = await createNewNote(parentNoteId, note, extraOptions.dataKey);
if (extraOptions.labels) {
for (const attrName in extraOptions.labels) {
@ -117,19 +117,19 @@ async function createNote(parentNoteId, title, content = "", extraOptions = {})
return noteId;
}
async function protectNoteRecursively(noteId, dataKey, protect, sourceId) {
async function protectNoteRecursively(noteId, dataKey, protect) {
const note = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [noteId]);
await protectNote(note, dataKey, protect, sourceId);
await protectNote(note, dataKey, protect);
const children = await sql.getColumn("SELECT noteId FROM branches WHERE parentNoteId = ? AND isDeleted = 0", [noteId]);
for (const childNoteId of children) {
await protectNoteRecursively(childNoteId, dataKey, protect, sourceId);
await protectNoteRecursively(childNoteId, dataKey, protect);
}
}
async function protectNote(note, dataKey, protect, sourceId) {
async function protectNote(note, dataKey, protect) {
let changed = false;
if (protect && !note.isProtected) {
@ -151,13 +151,13 @@ async function protectNote(note, dataKey, protect, sourceId) {
await sql.execute("UPDATE notes SET title = ?, content = ?, isProtected = ? WHERE noteId = ?",
[note.title, note.content, note.isProtected, note.noteId]);
await sync_table.addNoteSync(note.noteId, sourceId);
await sync_table.addNoteSync(note.noteId);
}
await protectNoteRevisions(note.noteId, dataKey, protect, sourceId);
await protectNoteRevisions(note.noteId, dataKey, protect);
}
async function protectNoteRevisions(noteId, dataKey, protect, sourceId) {
async function protectNoteRevisions(noteId, dataKey, protect) {
const revisionsToChange = await sql.getRows("SELECT * FROM note_revisions WHERE noteId = ? AND isProtected != ?", [noteId, protect]);
for (const revision of revisionsToChange) {
@ -175,11 +175,11 @@ async function protectNoteRevisions(noteId, dataKey, protect, sourceId) {
await sql.execute("UPDATE note_revisions SET title = ?, content = ?, isProtected = ? WHERE noteRevisionId = ?",
[revision.title, revision.content, revision.isProtected, revision.noteRevisionId]);
await sync_table.addNoteRevisionSync(revision.noteRevisionId, sourceId);
await sync_table.addNoteRevisionSync(revision.noteRevisionId);
}
}
async function saveNoteRevision(noteId, dataKey, sourceId, nowStr) {
async function saveNoteRevision(noteId, dataKey, nowStr) {
const oldNote = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [noteId]);
if (oldNote.type === 'file') {
@ -205,10 +205,10 @@ async function saveNoteRevision(noteId, dataKey, sourceId, nowStr) {
dateModifiedTo: nowStr
});
await sync_table.addNoteRevisionSync(newNoteRevisionId, sourceId);
await sync_table.addNoteRevisionSync(newNoteRevisionId);
}
async function saveNoteImages(noteId, noteText, sourceId) {
async function saveNoteImages(noteId, noteText) {
const existingNoteImages = await sql.getRows("SELECT * FROM note_images WHERE noteId = ?", [noteId]);
const foundImageIds = [];
const now = utils.nowDate();
@ -231,13 +231,13 @@ async function saveNoteImages(noteId, noteText, sourceId) {
dateCreated: now
});
await sync_table.addNoteImageSync(noteImageId, sourceId);
await sync_table.addNoteImageSync(noteImageId);
}
else if (existingNoteImage.isDeleted) {
await sql.execute("UPDATE note_images SET isDeleted = 0, dateModified = ? WHERE noteImageId = ?",
[now, existingNoteImage.noteImageId]);
await sync_table.addNoteImageSync(existingNoteImage.noteImageId, sourceId);
await sync_table.addNoteImageSync(existingNoteImage.noteImageId);
}
// else we don't need to do anything
@ -251,7 +251,7 @@ async function saveNoteImages(noteId, noteText, sourceId) {
await sql.execute("UPDATE note_images SET isDeleted = 1, dateModified = ? WHERE noteImageId = ?",
[now, unusedNoteImage.noteImageId]);
await sync_table.addNoteImageSync(unusedNoteImage.noteImageId, sourceId);
await sync_table.addNoteImageSync(unusedNoteImage.noteImageId);
}
}
@ -265,7 +265,7 @@ async function loadFile(noteId, newNote, dataKey) {
newNote.content = oldNote.content;
}
async function updateNote(noteId, newNote, dataKey, sourceId) {
async function updateNote(noteId, newNote, dataKey) {
if (newNote.type === 'file') {
await loadFile(noteId, newNote, dataKey);
}
@ -293,10 +293,10 @@ async function updateNote(noteId, newNote, dataKey, sourceId) {
&& !existingnoteRevisionId
&& msSinceDateCreated >= noteRevisionSnapshotTimeInterval * 1000) {
await saveNoteRevision(noteId, dataKey, sourceId, nowStr);
await saveNoteRevision(noteId, dataKey, nowStr);
}
await saveNoteImages(noteId, newNote.content, sourceId);
await saveNoteImages(noteId, newNote.content);
await protectNoteRevisions(noteId, dataKey, newNote.isProtected);
@ -307,11 +307,11 @@ async function updateNote(noteId, newNote, dataKey, sourceId) {
nowStr,
noteId]);
await sync_table.addNoteSync(noteId, sourceId);
await sync_table.addNoteSync(noteId);
});
}
async function deleteNote(branchId, sourceId) {
async function deleteNote(branchId) {
const branch = await sql.getRowOrNull("SELECT * FROM branches WHERE branchId = ?", [branchId]);
if (!branch || branch.isDeleted === 1) {
@ -321,7 +321,7 @@ async function deleteNote(branchId, sourceId) {
const now = utils.nowDate();
await sql.execute("UPDATE branches SET isDeleted = 1, dateModified = ? WHERE branchId = ?", [now, branchId]);
await sync_table.addBranchSync(branchId, sourceId);
await sync_table.addBranchSync(branchId);
const noteId = await sql.getValue("SELECT noteId FROM branches WHERE branchId = ?", [branchId]);
@ -329,12 +329,12 @@ async function deleteNote(branchId, sourceId) {
if (!notDeletedBranchsCount) {
await sql.execute("UPDATE notes SET isDeleted = 1, dateModified = ? WHERE noteId = ?", [now, noteId]);
await sync_table.addNoteSync(noteId, sourceId);
await sync_table.addNoteSync(noteId);
const children = await sql.getRows("SELECT branchId FROM branches WHERE parentNoteId = ? AND isDeleted = 0", [noteId]);
for (const child of children) {
await deleteNote(child.branchId, sourceId);
await deleteNote(child.branchId);
}
}
}

View File

@ -22,7 +22,7 @@ async function getOption(name) {
return row['value'] ? row['value'] : row['opt_value'];
}
async function setOption(name, value, sourceId = null) {
async function setOption(name, value) {
const opt = await sql.getRow("SELECT * FROM options WHERE name = ?", [name]);
if (!opt) {
@ -30,14 +30,14 @@ async function setOption(name, value, sourceId = null) {
}
if (opt.isSynced) {
await sync_table.addOptionsSync(name, sourceId);
await sync_table.addOptionsSync(name);
}
await sql.execute("UPDATE options SET value = ?, dateModified = ? WHERE name = ?",
[value, utils.nowDate(), name]);
}
async function createOption(name, value, isSynced, sourceId = null) {
async function createOption(name, value, isSynced) {
await sql.insert("options", {
name: name,
value: value,
@ -46,7 +46,7 @@ async function createOption(name, value, isSynced, sourceId = null) {
});
if (isSynced) {
await sync_table.addOptionsSync(name, sourceId);
await sync_table.addOptionsSync(name);
}
}

View File

@ -3,6 +3,7 @@ const source_id = require('./source_id');
const utils = require('./utils');
const sync_setup = require('./sync_setup');
const log = require('./log');
const cls = require('./cls');
async function addNoteSync(noteId, sourceId) {
await addEntitySync("notes", noteId, sourceId)
@ -49,7 +50,7 @@ async function addEntitySync(entityName, entityId, sourceId) {
entityName: entityName,
entityId: entityId,
syncDate: utils.nowDate(),
sourceId: sourceId || source_id.getCurrentSourceId()
sourceId: sourceId || cls.getSourceId() || source_id.getCurrentSourceId()
});
if (!sync_setup.isSyncSetup) {

View File

@ -76,7 +76,7 @@ async function loadSubTreeNoteIds(parentNoteId, subTreeNoteIds) {
}
}
async function sortNotesAlphabetically(parentNoteId, req, sourceId) {
async function sortNotesAlphabetically(parentNoteId, req) {
await sql.doInTransaction(async () => {
const notes = await sql.getRows(`SELECT branchId, noteId, title, isProtected
FROM notes JOIN branches USING(noteId)
@ -95,7 +95,7 @@ async function sortNotesAlphabetically(parentNoteId, req, sourceId) {
position++;
}
await sync_table.addNoteReorderingSync(parentNoteId, sourceId);
await sync_table.addNoteReorderingSync(parentNoteId);
});
}