Notes/src/routes/api/recent_changes.ts

117 lines
4.2 KiB
TypeScript
Raw Normal View History

2017-10-21 21:10:33 -04:00
"use strict";
import sql from "../../services/sql.js";
import protectedSessionService from "../../services/protected_session.js";
import noteService from "../../services/notes.js";
import becca from "../../becca/becca.js";
import { Request } from 'express';
import { RevisionRow } from '../../becca/entities/rows';
interface RecentChangeRow {
noteId: string;
current_isDeleted: boolean;
current_deleteId: string;
current_title: string;
current_isProtected: boolean,
title: string;
utcDate: string;
date: string;
canBeUndeleted?: boolean;
}
function getRecentChanges(req: Request) {
const {ancestorNoteId} = req.params;
let recentChanges = [];
const revisionRows = sql.getRows<RecentChangeRow>(`
SELECT
notes.noteId,
notes.isDeleted AS current_isDeleted,
notes.deleteId AS current_deleteId,
notes.title AS current_title,
notes.isProtected AS current_isProtected,
revisions.title,
revisions.utcDateCreated AS utcDate,
revisions.dateCreated AS date
FROM
revisions
2020-12-16 15:01:20 +01:00
JOIN notes USING(noteId)`);
for (const revisionRow of revisionRows) {
const note = becca.getNote(revisionRow.noteId);
2023-04-16 09:22:24 +02:00
// for deleted notes, the becca note is null, and it's not possible to (easily) determine if it belongs to a subtree
if (ancestorNoteId === 'root' || note?.hasAncestor(ancestorNoteId)) {
recentChanges.push(revisionRow);
}
}
// 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)
const noteRows = sql.getRows<RecentChangeRow>(`
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, -- different from the second SELECT
notes.dateCreated AS date -- different from the second SELECT
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, -- different from the first SELECT
notes.dateModified AS date -- different from the first SELECT
2020-12-16 15:01:20 +01:00
FROM notes
WHERE notes.isDeleted = 1`);
2023-04-16 09:22:24 +02:00
for (const noteRow of noteRows) {
const note = becca.getNote(noteRow.noteId);
// for deleted notes, the becca note is null, and it's not possible to (easily) determine if it belongs to a subtree
if (ancestorNoteId === 'root' || note?.hasAncestor(ancestorNoteId)) {
2023-04-16 09:22:24 +02:00
recentChanges.push(noteRow);
}
}
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()) {
change.title = protectedSessionService.decryptString(change.title) || "[protected]";
change.current_title = protectedSessionService.decryptString(change.current_title) || "[protected]";
}
else {
2021-10-12 22:36:22 +02:00
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
2021-05-08 23:31:20 +02:00
const undeletedParentBranchIds = noteService.getUndeletedParentBranchIds(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)
2021-05-08 23:31:20 +02:00
change.canBeUndeleted = undeletedParentBranchIds.length > 0;
2020-01-03 10:48:36 +01:00
}
}
return recentChanges;
}
export default {
getRecentChanges
2020-05-16 23:12:29 +02:00
};