From 8e351e7e33774f9c4ad15b311f8c3a4725bacfdb Mon Sep 17 00:00:00 2001 From: "Cheng-Han, Wu" Date: Fri, 17 Jun 2016 16:11:14 +0800 Subject: [PATCH] Add revision api --- app.js | 2 ++ lib/response.js | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/app.js b/app.js index 31bf868..b8bb316 100644 --- a/app.js +++ b/app.js @@ -457,6 +457,8 @@ app.get("/p/:shortid", response.showPublishSlide); app.get("/:noteId", response.showNote); //note actions app.get("/:noteId/:action", response.noteActions); +//note actions with action id +app.get("/:noteId/:action/:actionId", response.noteActions); // response not found if no any route matches app.get('*', function (req, res) { response.errorNotFound(res); diff --git a/lib/response.js b/lib/response.js index 873dfe4..1ed3a5b 100644 --- a/lib/response.js +++ b/lib/response.js @@ -10,6 +10,7 @@ var shortId = require('shortid'); var metaMarked = require('meta-marked'); var querystring = require('querystring'); var request = require('request'); +var moment = require('moment'); //core var config = require("./config.js"); @@ -322,6 +323,38 @@ function actionGist(req, res, note) { res.redirect("https://github.com/login/oauth/authorize?" + query); } +function actionRevision(req, res, note) { + var actionId = req.params.actionId; + if (actionId) { + var time = moment(parseInt(actionId)); + if (time.isValid()) { + models.Revision.getPatchedNoteRevisionByTime(note, time, function (err, content) { + if (err) { + logger.error(err); + return response.errorInternalError(res); + } + if (!content) { + return response.errorNotFound(res); + } + res.end(JSON.stringify(content)); + }); + } else { + return response.errorNotFound(res); + } + } else { + models.Revision.getNoteRevisions(note, function (err, data) { + if (err) { + logger.error(err); + return response.errorInternalError(res); + } + var out = { + revision: data + }; + res.end(JSON.stringify(out)); + }); + } +} + function noteActions(req, res, next) { var noteId = req.params.noteId; findNote(req, res, function (note) { @@ -343,6 +376,9 @@ function noteActions(req, res, next) { case "gist": actionGist(req, res, note); break; + case "revision": + actionRevision(req, res, note); + break; default: return res.redirect(config.serverurl + '/' + noteId); break;