persisted recent notes

This commit is contained in:
azivner 2017-11-04 23:46:50 -04:00
parent 30df3cba1c
commit 3fb30a0b5c
5 changed files with 71 additions and 6 deletions

View file

@ -0,0 +1,4 @@
CREATE TABLE `recent_notes` (
`note_id` TEXT NOT NULL PRIMARY KEY,
`date_accessed` INTEGER NOT NULL
);

View file

@ -8,20 +8,37 @@ const recentNotes = (function() {
const noteDetailEl = $('#note-detail');
let list = [];
$.ajax({
url: baseApiUrl + 'recent-notes',
type: 'GET',
error: () => error("Error getting recent notes.")
}).then(result => {
list = result.map(r => r.note_id);
});
function addRecentNote(noteTreeId, noteContentId) {
setTimeout(() => {
// we include the note into recent list only if the user stayed on the note at least 5 seconds
if (noteTreeId === noteEditor.getCurrentNoteId() || noteContentId === noteEditor.getCurrentNoteId()) {
// if it's already there, remove the note
list = list.filter(note => note !== noteTreeId);
list.unshift(noteTreeId);
$.ajax({
url: baseApiUrl + 'recent-notes/' + noteTreeId,
type: 'PUT',
error: () => error("Error setting recent notes.")
}).then(result => {
list = result.map(r => r.note_id);
});
}
}, 1500);
}
function removeRecentNote(noteIdToRemove) {
list = list.filter(note => note !== noteIdToRemove);
$.ajax({
url: baseApiUrl + 'recent-notes/' + noteIdToRemove,
type: 'DELETE',
error: () => error("Error removing note from recent notes.")
}).then(result => {
list = result.map(r => r.note_id);
});
}
function showDialog() {

View file

@ -0,0 +1,42 @@
"use strict";
const express = require('express');
const router = express.Router();
const sql = require('../../services/sql');
const auth = require('../../services/auth');
const utils = require('../../services/utils');
router.get('', auth.checkApiAuth, async (req, res, next) => {
res.send(await getRecentNotes());
});
router.put('/:noteId', auth.checkApiAuth, async (req, res, next) => {
await sql.replace('recent_notes', {
note_id: req.params.noteId,
date_accessed: utils.nowTimestamp()
});
res.send(await getRecentNotes());
});
router.delete('/:noteId', auth.checkApiAuth, async (req, res, next) => {
await sql.remove('recent_notes', req.params.noteId);
res.send(await getRecentNotes());
});
async function getRecentNotes() {
await deleteOld();
return await sql.getResults("SELECT * FROM recent_notes ORDER BY date_accessed DESC");
}
async function deleteOld() {
const cutoffDateAccessed = await sql.getSingleValue("SELECT date_accessed FROM recent_notes ORDER BY date_accessed DESC LIMIT 100, 1");
if (cutoffDateAccessed) {
await sql.execute("DELETE FROM recent_notes WHERE date_accessed < ?", [cutoffDateAccessed]);
}
}
module.exports = router;

View file

@ -16,6 +16,7 @@ const migrationApiRoute = require('./api/migration');
const syncApiRoute = require('./api/sync');
const loginApiRoute = require('./api/login');
const eventLogRoute = require('./api/event_log');
const recentNotesRoute = require('./api/recent_notes');
function register(app) {
app.use('/', indexRoute);
@ -35,6 +36,7 @@ function register(app) {
app.use('/api/sync', syncApiRoute);
app.use('/api/login', loginApiRoute);
app.use('/api/event-log', eventLogRoute);
app.use('/api/recent-notes', recentNotesRoute);
}
module.exports = {

View file

@ -4,7 +4,7 @@ const options = require('./options');
const fs = require('fs-extra');
const log = require('./log');
const APP_DB_VERSION = 25;
const APP_DB_VERSION = 26;
const MIGRATIONS_DIR = "./migrations";
async function migrate() {