trilium/src/routes/api/recent_changes.js

77 lines
2.6 KiB
JavaScript
Raw Normal View History

2017-10-22 09:10:33 +08:00
"use strict";
2017-10-16 07:47:05 +08:00
const sql = require('../../services/sql');
const protectedSessionService = require('../../services/protected_session');
2020-01-03 20:14:43 +08:00
const noteService = require('../../services/notes');
async function getRecentChanges() {
const recentChanges = await sql.getRows(
`
SELECT * FROM (
SELECT
notes.noteId,
notes.isDeleted AS current_isDeleted,
2020-01-03 17:48:36 +08:00
notes.deleteId AS current_deleteId,
notes.isErased AS current_isErased,
notes.title AS current_title,
notes.isProtected AS current_isProtected,
note_revisions.title,
2019-11-02 02:21:48 +08:00
note_revisions.utcDateCreated AS date
FROM
note_revisions
JOIN notes USING(noteId)
2019-11-20 02:02:16 +08:00
ORDER BY
note_revisions.utcDateCreated DESC
2020-01-03 17:48:36 +08:00
LIMIT 200
)
UNION ALL SELECT * FROM (
SELECT
notes.noteId,
notes.isDeleted AS current_isDeleted,
2020-01-03 17:48:36 +08:00
notes.deleteId AS current_deleteId,
notes.isErased AS current_isErased,
notes.title AS current_title,
notes.isProtected AS current_isProtected,
notes.title,
2020-01-03 17:48:36 +08:00
notes.utcDateModified AS date
FROM
notes
ORDER BY
2020-01-03 17:48:36 +08:00
utcDateModified DESC
LIMIT 200
)
ORDER BY date DESC
LIMIT 200`);
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);
}
else {
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
}
}
}
return recentChanges;
}
module.exports = {
getRecentChanges
};