Notes/src/routes/api/recent_changes.js

97 lines
3.3 KiB
JavaScript
Raw Normal View History

2017-10-21 21:10:33 -04:00
"use strict";
2017-10-15 19:47:05 -04:00
const sql = require('../../services/sql');
const protectedSessionService = require('../../services/protected_session');
2020-01-03 13:14:43 +01:00
const noteService = require('../../services/notes');
2020-08-06 23:55:17 +02:00
const noteCacheService = require('../../services/note_cache/note_cache_service');
2020-06-20 12:31:38 +02:00
function getRecentChanges(req) {
const {ancestorNoteId} = req.params;
let recentChanges = [];
2020-06-20 12:31:38 +02:00
const noteRevisions = sql.getRows(`
SELECT
notes.noteId,
notes.isDeleted AS current_isDeleted,
notes.deleteId AS current_deleteId,
notes.title AS current_title,
notes.isProtected AS current_isProtected,
note_revisions.title,
note_revisions.utcDateCreated AS utcDate,
note_revisions.dateCreated AS date
FROM
note_revisions
2020-12-16 15:01:20 +01:00
JOIN notes USING(noteId)`);
for (const noteRevision of noteRevisions) {
if (noteCacheService.isInAncestor(noteRevision.noteId, ancestorNoteId)) {
recentChanges.push(noteRevision);
}
}
// now we need to also collect date points not represented in note revisions:
// 1. creation for all notes (dateCreated)
// 2. deletion for deleted notes (dateModified)
2020-06-20 12:31:38 +02:00
const notes = sql.getRows(`
SELECT
notes.noteId,
notes.isDeleted AS current_isDeleted,
notes.deleteId AS current_deleteId,
notes.title AS current_title,
notes.isProtected AS current_isProtected,
notes.title,
notes.utcDateCreated AS utcDate,
notes.dateCreated AS date
2020-12-16 15:01:20 +01:00
FROM notes
UNION ALL
SELECT
notes.noteId,
notes.isDeleted AS current_isDeleted,
notes.deleteId AS current_deleteId,
notes.title AS current_title,
notes.isProtected AS current_isProtected,
notes.title,
notes.utcDateModified AS utcDate,
notes.dateModified AS date
2020-12-16 15:01:20 +01:00
FROM notes
WHERE notes.isDeleted = 1`);
for (const note of notes) {
if (noteCacheService.isInAncestor(note.noteId, ancestorNoteId)) {
recentChanges.push(note);
}
}
recentChanges.sort((a, b) => a.utcDate > b.utcDate ? -1 : 1);
recentChanges = recentChanges.slice(0, Math.min(500, recentChanges.length));
for (const change of recentChanges) {
if (change.current_isProtected) {
if (protectedSessionService.isProtectedSessionAvailable()) {
2019-11-02 07:50:23 +01:00
change.title = protectedSessionService.decryptString(change.title);
change.current_title = protectedSessionService.decryptString(change.current_title);
}
else {
change.title = change.current_title = "[Protected]";
}
}
2020-01-03 10:48:36 +01:00
if (change.current_isDeleted) {
2020-12-16 15:01:20 +01:00
const deleteId = change.current_deleteId;
2020-01-03 10:48:36 +01:00
2020-12-16 15:01:20 +01:00
const undeletedParentBranches = noteService.getUndeletedParentBranches(change.noteId, deleteId);
2020-01-03 10:48:36 +01:00
2020-12-16 15:01:20 +01:00
// note (and the subtree) can be undeleted if there's at least one undeleted parent (whose branch would be undeleted by this op)
change.canBeUndeleted = undeletedParentBranches.length > 0;
2020-01-03 10:48:36 +01:00
}
}
return recentChanges;
}
module.exports = {
getRecentChanges
2020-05-16 23:12:29 +02:00
};