2017-10-22 09:10:33 +08:00
|
|
|
"use strict";
|
|
|
|
|
2017-10-16 07:47:05 +08:00
|
|
|
const sql = require('../../services/sql');
|
2018-11-07 00:47:40 +08:00
|
|
|
const protectedSessionService = require('../../services/protected_session');
|
2020-01-03 20:14:43 +08:00
|
|
|
const noteService = require('../../services/notes');
|
2020-05-17 05:12:29 +08:00
|
|
|
const noteCacheService = require('../../services/note_cache/note_cache.js');
|
2017-10-15 11:31:44 +08:00
|
|
|
|
2020-06-20 18:31:38 +08:00
|
|
|
function getRecentChanges(req) {
|
2020-03-30 01:37:34 +08:00
|
|
|
const {ancestorNoteId} = req.params;
|
|
|
|
|
2020-06-16 05:22:11 +08:00
|
|
|
let recentChanges = [];
|
2020-03-30 01:37:34 +08:00
|
|
|
|
2020-06-20 18:31:38 +08:00
|
|
|
const noteRevisions = sql.getRows(`
|
2020-06-16 05:22:11 +08:00
|
|
|
SELECT
|
|
|
|
notes.noteId,
|
|
|
|
notes.isDeleted AS current_isDeleted,
|
|
|
|
notes.deleteId AS current_deleteId,
|
|
|
|
notes.isErased AS current_isErased,
|
|
|
|
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
|
|
|
|
JOIN notes USING(noteId)`);
|
|
|
|
|
|
|
|
for (const noteRevision of noteRevisions) {
|
|
|
|
if (noteCacheService.isInAncestor(noteRevision.noteId, ancestorNoteId)) {
|
|
|
|
recentChanges.push(noteRevision);
|
2020-03-30 01:37:34 +08:00
|
|
|
}
|
2020-06-16 05:22:11 +08:00
|
|
|
}
|
2020-03-30 01:37:34 +08:00
|
|
|
|
2020-06-20 18:31:38 +08:00
|
|
|
const notes = sql.getRows(`
|
2020-06-16 05:22:11 +08:00
|
|
|
SELECT
|
|
|
|
notes.noteId,
|
|
|
|
notes.isDeleted AS current_isDeleted,
|
|
|
|
notes.deleteId AS current_deleteId,
|
|
|
|
notes.isErased AS current_isErased,
|
|
|
|
notes.title AS current_title,
|
|
|
|
notes.isProtected AS current_isProtected,
|
|
|
|
notes.title,
|
|
|
|
notes.utcDateCreated AS utcDate,
|
|
|
|
notes.dateCreated AS date
|
|
|
|
FROM
|
|
|
|
notes`);
|
|
|
|
|
|
|
|
for (const note of notes) {
|
|
|
|
if (noteCacheService.isInAncestor(note.noteId, ancestorNoteId)) {
|
|
|
|
recentChanges.push(note);
|
2020-03-30 01:37:34 +08:00
|
|
|
}
|
|
|
|
}
|
2017-10-15 11:31:44 +08:00
|
|
|
|
2020-06-16 05:22:11 +08:00
|
|
|
recentChanges.sort((a, b) => a.utcDate > b.utcDate ? -1 : 1);
|
|
|
|
|
|
|
|
recentChanges = recentChanges.slice(0, Math.min(500, recentChanges.length));
|
|
|
|
|
|
|
|
console.log(recentChanges);
|
|
|
|
|
2019-03-05 03:44:20 +08:00
|
|
|
for (const change of recentChanges) {
|
|
|
|
if (change.current_isProtected) {
|
|
|
|
if (protectedSessionService.isProtectedSessionAvailable()) {
|
2019-11-02 14:50:23 +08:00
|
|
|
change.title = protectedSessionService.decryptString(change.title);
|
|
|
|
change.current_title = protectedSessionService.decryptString(change.current_title);
|
2019-03-05 03:44:20 +08:00
|
|
|
}
|
|
|
|
else {
|
2018-11-07 00:47:40 +08:00
|
|
|
change.title = change.current_title = "[Protected]";
|
|
|
|
}
|
|
|
|
}
|
2020-01-03 17:48:36 +08:00
|
|
|
|
|
|
|
if (change.current_isDeleted) {
|
|
|
|
if (change.current_isErased) {
|
|
|
|
change.canBeUndeleted = false;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
const deleteId = change.current_deleteId;
|
|
|
|
|
2020-06-20 18:31:38 +08:00
|
|
|
const undeletedParentBranches = noteService.getUndeletedParentBranches(change.noteId, deleteId);
|
2020-01-03 17:48:36 +08:00
|
|
|
|
|
|
|
// note (and the subtree) can be undeleted if there's at least one undeleted parent (whose branch would be undeleted by this op)
|
2020-01-03 20:14:43 +08:00
|
|
|
change.canBeUndeleted = undeletedParentBranches.length > 0;
|
2020-01-03 17:48:36 +08:00
|
|
|
}
|
|
|
|
}
|
2018-11-07 00:47:40 +08:00
|
|
|
}
|
|
|
|
|
2018-03-31 01:56:46 +08:00
|
|
|
return recentChanges;
|
|
|
|
}
|
2017-10-15 11:31:44 +08:00
|
|
|
|
2018-03-31 01:56:46 +08:00
|
|
|
module.exports = {
|
|
|
|
getRecentChanges
|
2020-05-17 05:12:29 +08:00
|
|
|
};
|