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-03-30 01:37:34 +08:00
|
|
|
const noteCacheService = require('../../services/note_cache');
|
2017-10-15 11:31:44 +08:00
|
|
|
|
2020-03-30 01:37:34 +08:00
|
|
|
async function getRecentChanges(req) {
|
|
|
|
const {ancestorNoteId} = req.params;
|
|
|
|
|
|
|
|
const noteRows = await sql.getRows(
|
2019-09-03 02:26:18 +08:00
|
|
|
`
|
|
|
|
SELECT * FROM (
|
2020-03-30 01:37:34 +08:00
|
|
|
SELECT note_revisions.noteId,
|
|
|
|
note_revisions.noteRevisionId,
|
|
|
|
note_revisions.utcDateCreated AS date
|
|
|
|
FROM note_revisions
|
|
|
|
ORDER BY note_revisions.utcDateCreated DESC
|
2019-09-03 02:26:18 +08:00
|
|
|
)
|
|
|
|
UNION ALL SELECT * FROM (
|
2020-03-30 01:37:34 +08:00
|
|
|
SELECT
|
|
|
|
notes.noteId,
|
|
|
|
NULL AS noteRevisionId,
|
|
|
|
utcDateModified AS date
|
|
|
|
FROM notes
|
|
|
|
ORDER BY utcDateModified DESC
|
2019-09-03 02:26:18 +08:00
|
|
|
)
|
2020-03-30 01:37:34 +08:00
|
|
|
ORDER BY date DESC`);
|
|
|
|
|
|
|
|
const recentChanges = [];
|
|
|
|
|
|
|
|
for (const noteRow of noteRows) {
|
|
|
|
if (!noteCacheService.isInAncestor(noteRow.noteId, ancestorNoteId)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (noteRow.noteRevisionId) {
|
|
|
|
recentChanges.push(await sql.getRow(`
|
|
|
|
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 date
|
|
|
|
FROM
|
|
|
|
note_revisions
|
|
|
|
JOIN notes USING(noteId)
|
|
|
|
WHERE noteRevisionId = ?`, [noteRow.noteRevisionId]));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
recentChanges.push(await sql.getRow(`
|
|
|
|
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.utcDateModified AS date
|
|
|
|
FROM
|
|
|
|
notes
|
|
|
|
WHERE noteId = ?`, [noteRow.noteId]));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (recentChanges.length >= 200) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-10-15 11:31:44 +08:00
|
|
|
|
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-01-03 20:14:43 +08:00
|
|
|
const undeletedParentBranches = await 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
|
|
|
|
};
|