2016-10-10 08:51:46 -04:00
|
|
|
//history
|
|
|
|
//external modules
|
|
|
|
var async = require('async');
|
|
|
|
|
|
|
|
//core
|
|
|
|
var config = require("./config.js");
|
|
|
|
var logger = require("./logger.js");
|
|
|
|
var response = require("./response.js");
|
|
|
|
var models = require("./models");
|
|
|
|
|
|
|
|
//public
|
|
|
|
var History = {
|
|
|
|
historyGet: historyGet,
|
|
|
|
historyPost: historyPost,
|
|
|
|
historyDelete: historyDelete,
|
|
|
|
updateHistory: updateHistory
|
|
|
|
};
|
|
|
|
|
2017-02-03 08:39:08 -05:00
|
|
|
function getHistory(userid, callback) {
|
|
|
|
models.User.findOne({
|
|
|
|
where: {
|
|
|
|
id: userid
|
2016-10-10 08:51:46 -04:00
|
|
|
}
|
2017-02-03 08:39:08 -05:00
|
|
|
}).then(function (user) {
|
|
|
|
if (!user)
|
|
|
|
return callback(null, null);
|
|
|
|
var history = {};
|
|
|
|
if (user.history)
|
|
|
|
history = parseHistoryToObject(JSON.parse(user.history));
|
|
|
|
if (config.debug)
|
|
|
|
logger.info('read history success: ' + user.id);
|
|
|
|
return callback(null, history);
|
|
|
|
}).catch(function (err) {
|
|
|
|
logger.error('read history failed: ' + err);
|
|
|
|
return callback(err, null);
|
2016-10-10 08:51:46 -04:00
|
|
|
});
|
2017-02-03 08:39:08 -05:00
|
|
|
}
|
2016-11-16 00:58:54 -05:00
|
|
|
|
2017-02-03 08:39:08 -05:00
|
|
|
function setHistory(userid, history, callback) {
|
2016-11-16 00:58:54 -05:00
|
|
|
models.User.update({
|
2017-02-03 08:39:08 -05:00
|
|
|
history: JSON.stringify(parseHistoryToArray(history))
|
2016-11-16 00:58:54 -05:00
|
|
|
}, {
|
|
|
|
where: {
|
|
|
|
id: userid
|
2016-10-10 08:51:46 -04:00
|
|
|
}
|
2016-11-16 00:58:54 -05:00
|
|
|
}).then(function (count) {
|
|
|
|
return callback(null, count);
|
|
|
|
}).catch(function (err) {
|
2017-02-03 08:39:08 -05:00
|
|
|
logger.error('set history failed: ' + err);
|
2016-11-16 00:58:54 -05:00
|
|
|
return callback(err, null);
|
2016-10-10 08:51:46 -04:00
|
|
|
});
|
2016-11-16 00:58:54 -05:00
|
|
|
}
|
2016-10-10 08:51:46 -04:00
|
|
|
|
2017-02-03 08:39:08 -05:00
|
|
|
function updateHistory(userid, noteId, document, time) {
|
2016-10-10 13:22:08 -04:00
|
|
|
if (userid && noteId && typeof document !== 'undefined') {
|
2016-10-10 08:51:46 -04:00
|
|
|
getHistory(userid, function (err, history) {
|
|
|
|
if (err || !history) return;
|
2017-02-03 08:39:08 -05:00
|
|
|
if (!history[noteId]) {
|
|
|
|
history[noteId] = {};
|
2016-10-10 08:51:46 -04:00
|
|
|
}
|
2017-02-03 08:39:08 -05:00
|
|
|
var noteHistory = history[noteId];
|
2016-10-10 08:51:46 -04:00
|
|
|
var noteInfo = models.Note.parseNoteInfo(document);
|
|
|
|
noteHistory.id = noteId;
|
|
|
|
noteHistory.text = noteInfo.title;
|
2017-02-03 08:39:08 -05:00
|
|
|
noteHistory.time = time || Date.now();
|
2016-10-10 08:51:46 -04:00
|
|
|
noteHistory.tags = noteInfo.tags;
|
2017-02-03 08:39:08 -05:00
|
|
|
setHistory(userid, history, function (err, count) {
|
|
|
|
return;
|
|
|
|
});
|
2016-10-10 08:51:46 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-16 00:58:54 -05:00
|
|
|
function parseHistoryToArray(history) {
|
|
|
|
var _history = [];
|
|
|
|
Object.keys(history).forEach(function (key) {
|
|
|
|
var item = history[key];
|
|
|
|
_history.push(item);
|
|
|
|
});
|
|
|
|
return _history;
|
|
|
|
}
|
|
|
|
|
|
|
|
function parseHistoryToObject(history) {
|
|
|
|
var _history = {};
|
|
|
|
for (var i = 0, l = history.length; i < l; i++) {
|
|
|
|
var item = history[i];
|
|
|
|
_history[item.id] = item;
|
|
|
|
}
|
|
|
|
return _history;
|
|
|
|
}
|
|
|
|
|
2016-10-10 08:51:46 -04:00
|
|
|
function historyGet(req, res) {
|
|
|
|
if (req.isAuthenticated()) {
|
|
|
|
getHistory(req.user.id, function (err, history) {
|
|
|
|
if (err) return response.errorInternalError(res);
|
|
|
|
if (!history) return response.errorNotFound(res);
|
|
|
|
res.send({
|
2016-11-16 00:58:54 -05:00
|
|
|
history: parseHistoryToArray(history)
|
2016-10-10 08:51:46 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return response.errorForbidden(res);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function historyPost(req, res) {
|
|
|
|
if (req.isAuthenticated()) {
|
|
|
|
var noteId = req.params.noteId;
|
|
|
|
if (!noteId) {
|
|
|
|
if (typeof req.body['history'] === 'undefined') return response.errorBadRequest(res);
|
|
|
|
if (config.debug)
|
|
|
|
logger.info('SERVER received history from [' + req.user.id + ']: ' + req.body.history);
|
|
|
|
try {
|
|
|
|
var history = JSON.parse(req.body.history);
|
|
|
|
} catch (err) {
|
|
|
|
return response.errorBadRequest(res);
|
|
|
|
}
|
|
|
|
if (Array.isArray(history)) {
|
2017-02-03 08:39:08 -05:00
|
|
|
setHistory(req.user.id, history, function (err, count) {
|
|
|
|
if (err) return response.errorInternalError(res);
|
|
|
|
res.end();
|
|
|
|
});
|
2016-10-10 08:51:46 -04:00
|
|
|
} else {
|
|
|
|
return response.errorBadRequest(res);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (typeof req.body['pinned'] === 'undefined') return response.errorBadRequest(res);
|
|
|
|
getHistory(req.user.id, function (err, history) {
|
|
|
|
if (err) return response.errorInternalError(res);
|
|
|
|
if (!history) return response.errorNotFound(res);
|
2017-02-03 08:39:08 -05:00
|
|
|
if (!history[noteId]) return response.errorNotFound(res);
|
2016-10-10 08:51:46 -04:00
|
|
|
if (req.body.pinned === 'true' || req.body.pinned === 'false') {
|
2017-02-03 08:39:08 -05:00
|
|
|
history[noteId].pinned = (req.body.pinned === 'true');
|
|
|
|
setHistory(req.user.id, history, function (err, count) {
|
|
|
|
if (err) return response.errorInternalError(res);
|
|
|
|
res.end();
|
|
|
|
});
|
2016-10-10 08:51:46 -04:00
|
|
|
} else {
|
|
|
|
return response.errorBadRequest(res);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return response.errorForbidden(res);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function historyDelete(req, res) {
|
|
|
|
if (req.isAuthenticated()) {
|
|
|
|
var noteId = req.params.noteId;
|
|
|
|
if (!noteId) {
|
2017-02-03 08:39:08 -05:00
|
|
|
setHistory(req.user.id, [], function (err, count) {
|
|
|
|
if (err) return response.errorInternalError(res);
|
|
|
|
res.end();
|
|
|
|
});
|
2016-10-10 08:51:46 -04:00
|
|
|
} else {
|
|
|
|
getHistory(req.user.id, function (err, history) {
|
|
|
|
if (err) return response.errorInternalError(res);
|
|
|
|
if (!history) return response.errorNotFound(res);
|
2017-02-03 08:39:08 -05:00
|
|
|
delete history[noteId];
|
|
|
|
setHistory(req.user.id, history, function (err, count) {
|
|
|
|
if (err) return response.errorInternalError(res);
|
|
|
|
res.end();
|
|
|
|
});
|
2016-10-10 08:51:46 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return response.errorForbidden(res);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = History;
|