diff options
Diffstat (limited to 'app.js')
-rw-r--r-- | app.js | 68 |
1 files changed, 14 insertions, 54 deletions
@@ -22,6 +22,7 @@ var i18n = require('i18n'); var config = require("./lib/config.js"); var logger = require("./lib/logger.js"); var auth = require("./lib/auth.js"); +var history = require("./lib/history.js"); var response = require("./lib/response.js"); var models = require("./lib/models"); @@ -94,7 +95,7 @@ app.use(helmet.hsts({ })); i18n.configure({ - locales: ['en', 'zh', 'fr', 'de', 'ja', 'es', 'el', 'pt'], + locales: ['en', 'zh', 'fr', 'de', 'ja', 'es', 'el', 'pt', 'it', 'tr', 'ru', 'nl', 'hr', 'pl', 'uk'], cookie: 'locale', directory: __dirname + '/locales' }); @@ -365,56 +366,15 @@ app.get('/logout', function (req, res) { res.redirect(config.serverurl + '/'); }); //get history -app.get('/history', function (req, res) { - if (req.isAuthenticated()) { - models.User.findOne({ - where: { - id: req.user.id - } - }).then(function (user) { - if (!user) - return response.errorNotFound(res); - var history = []; - if (user.history) - history = JSON.parse(user.history); - res.send({ - history: history - }); - if (config.debug) - logger.info('read history success: ' + user.id); - }).catch(function (err) { - logger.error('read history failed: ' + err); - return response.errorInternalError(res); - }); - } else { - return response.errorForbidden(res); - } -}); +app.get('/history', history.historyGet); //post history -app.post('/history', urlencodedParser, function (req, res) { - if (req.isAuthenticated()) { - if (config.debug) - logger.info('SERVER received history from [' + req.user.id + ']: ' + req.body.history); - models.User.update({ - history: req.body.history - }, { - where: { - id: req.user.id - } - }).then(function (count) { - if (!count) - return response.errorNotFound(res); - if (config.debug) - logger.info("write user history success: " + req.user.id); - }).catch(function (err) { - logger.error('write history failed: ' + err); - return response.errorInternalError(res); - }); - res.end(); - } else { - return response.errorForbidden(res); - } -}); +app.post('/history', urlencodedParser, history.historyPost); +//post history by note id +app.post('/history/:noteId', urlencodedParser, history.historyPost); +//delete history +app.delete('/history', history.historyDelete); +//delete history by note id +app.delete('/history/:noteId', history.historyDelete); //get me info app.get('/me', function (req, res) { if (req.isAuthenticated()) { @@ -522,9 +482,9 @@ function startListen() { // sync db then start listen models.sequelize.sync().then(function () { // check if realtime is ready - if (realtime.isReady()) { + if (history.isReady() && realtime.isReady()) { models.Revision.checkAllNotesRevision(function (err, notes) { - if (err) return new Error(err); + if (err) throw new Error(err); if (!notes || notes.length <= 0) return startListen(); }); } @@ -549,9 +509,9 @@ process.on('SIGINT', function () { socket.disconnect(true); }); var checkCleanTimer = setInterval(function () { - if (realtime.isReady()) { + if (history.isReady() && realtime.isReady()) { models.Revision.checkAllNotesRevision(function (err, notes) { - if (err) return new Error(err); + if (err) throw new Error(err); if (notes.length <= 0) { clearInterval(checkCleanTimer); return process.exit(0); |