diff --git a/src/public/app/dialogs/recent_changes.js b/src/public/app/dialogs/recent_changes.js
index d008fce35..a53520702 100644
--- a/src/public/app/dialogs/recent_changes.js
+++ b/src/public/app/dialogs/recent_changes.js
@@ -16,18 +16,18 @@ export async function showDialog(ancestorNoteId) {
ancestorNoteId = hoistedNoteService.getHoistedNoteId();
}
- const result = await server.get('recent-changes/' + ancestorNoteId);
+ const recentChangesRows = await server.get('recent-changes/' + ancestorNoteId);
// preload all notes into cache
- await treeCache.getNotes(result.map(r => r.noteId), true);
+ await treeCache.getNotes(recentChangesRows.map(r => r.noteId), true);
$content.empty();
- if (result.length === 0) {
+ if (recentChangesRows.length === 0) {
$content.append("No changes yet ...");
}
- const groupedByDate = groupByDate(result);
+ const groupedByDate = groupByDate(recentChangesRows);
for (const [dateDay, dayChanges] of groupedByDate) {
const $changesList = $('
');
@@ -95,10 +95,10 @@ export async function showDialog(ancestorNoteId) {
}
}
-function groupByDate(result) {
+function groupByDate(rows) {
const groupedByDate = new Map();
- for (const row of result) {
+ for (const row of rows) {
const dateDay = row.date.substr(0, 10);
if (!groupedByDate.has(dateDay)) {
diff --git a/src/routes/api/recent_changes.js b/src/routes/api/recent_changes.js
index a4049072a..333be1f65 100644
--- a/src/routes/api/recent_changes.js
+++ b/src/routes/api/recent_changes.js
@@ -8,69 +8,55 @@ const noteCacheService = require('../../services/note_cache');
async function getRecentChanges(req) {
const {ancestorNoteId} = req.params;
- const noteRows = await sql.getRows(
- `
- SELECT * FROM (
- SELECT note_revisions.noteId,
- note_revisions.noteRevisionId,
- note_revisions.dateLastEdited AS date
- FROM note_revisions
- ORDER BY note_revisions.dateLastEdited DESC
- )
- UNION ALL SELECT * FROM (
- SELECT
- notes.noteId,
- NULL AS noteRevisionId,
- dateModified AS date
- FROM notes
- ORDER BY dateModified DESC
- )
- ORDER BY date DESC`);
+ let recentChanges = [];
- const recentChanges = [];
+ const noteRevisions = await sql.getRows(`
+ 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 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.dateCreated 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.dateModified AS date
- FROM
- notes
- WHERE noteId = ?`, [noteRow.noteId]));
- }
-
- if (recentChanges.length >= 200) {
- break;
+ for (const noteRevision of noteRevisions) {
+ if (noteCacheService.isInAncestor(noteRevision.noteId, ancestorNoteId)) {
+ recentChanges.push(noteRevision);
}
}
+ const notes = await sql.getRows(`
+ 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);
+ }
+ }
+
+ recentChanges.sort((a, b) => a.utcDate > b.utcDate ? -1 : 1);
+
+ recentChanges = recentChanges.slice(0, Math.min(500, recentChanges.length));
+
+ console.log(recentChanges);
+
for (const change of recentChanges) {
if (change.current_isProtected) {
if (protectedSessionService.isProtectedSessionAvailable()) {
@@ -102,4 +88,4 @@ async function getRecentChanges(req) {
module.exports = {
getRecentChanges
-};
\ No newline at end of file
+};