Note deletion feature added

This commit is contained in:
Christian Müller
2016-03-21 11:16:35 +01:00
parent d78cb0412b
commit 8ccf7d5a71
6 changed files with 177 additions and 161 deletions
+7 -7
View File
@@ -23,15 +23,15 @@
},
"homepage": "https://github.com/chmllr/NoteHub",
"dependencies": {
"body-parser": "^1.14.1",
"express": "^4.13.3",
"lru-cache": "^2.6.5",
"body-parser": "^1.15.0",
"express": "^4.13.4",
"lru-cache": "^4.0.0",
"marked": "^0.3.5",
"md5": "^2.0.0",
"sequelize": "^3.8.0",
"sqlite3": "^3.1.0"
"md5": "^2.1.0",
"sequelize": "^3.19.3",
"sqlite3": "*"
},
"devDependencies": {
"nodemon": "^1.7.3"
"nodemon": "^1.9.1"
}
}
+2 -1
View File
@@ -22,7 +22,8 @@
<textarea id="note" name="note">%CONTENT%</textarea>
<fieldset id="input-elems">
<input class="ui-elem" id="plain-password" name="plain-password" placeholder="Password for editing" type="text">&nbsp;
<input class="button ui-elem" id="publish-button" type="submit" value="Publish">
<input class="button ui-elem" id="publish-button" name="button" type="submit" value="Publish">
<input class="button ui-elem" id="delete-button" name="button" type="submit" value="Delete">
<span id="tableau">
0 words
</span>
+1
View File
@@ -18,6 +18,7 @@
<article class="bottom-space">
<h2>Changelog</h2>
<ul>
<li><strong>2016-03</strong>: Note deletion feature added.</li>
<li><strong>2015-10</strong>: NoteHub rewritten in Node.js.</li>
<li><strong>2015-10</strong>: NoteHub API and note styling discontinued due to low adoption by the user base.</li>
<li><strong>2014-09</strong>: text size setting added</li>
+7 -8
View File
@@ -4,8 +4,7 @@ var $ = function(id) {
var iosDetected = navigator.userAgent.match("(iPad|iPod|iPhone)");
var timer = null;
var timerDelay = iosDetected ? 800 : 400;
var $note, $action, $preview, $plain_password,
updatePreview, $tableau;
var $note, $action, $preview, $plain_password, $tableau;
var backendTimer;
function md2html(input) {
@@ -25,7 +24,7 @@ function onLoad() {
$preview = $("draft");
$tableau = $("tableau");
$plain_password = $("plain-password");
updatePreview = function() {
var updatePreview = function() {
clearTimeout(timer);
var content = $note.value;
var delay = Math.min(timerDelay, timerDelay * (content.length / 400));
@@ -36,6 +35,7 @@ function onLoad() {
};
if ($action == "UPDATE") updatePreview();
else {
$("delete-button").style.display = "none";
$note.value = "";
var draft = localStorage.getItem("draft");
if (draft) {
@@ -44,14 +44,13 @@ function onLoad() {
}
}
$note.onkeyup = updatePreview;
$("publish-button").onclick = function(e) {
$("delete-button").onclick = $("publish-button").onclick = function(e) {
localStorage.removeItem("draft");
self.onbeforeunload = null;;
self.onbeforeunload = null;
if ($plain_password.value != "") $("password").value = md5($plain_password.value);
$plain_password.value = null;
$("signature").value = md5($("session").value +
$note.value.replace(/[\n\r]/g, ""));
}
$("signature").value = md5($("session").value + $note.value.replace(/[\n\r]/g, ""));
};
if (iosDetected) $note.className += " ui-border";
else $note.focus();
self.onbeforeunload = saveDraft;
+18 -10
View File
@@ -28,7 +28,7 @@ var getTimeStamp = () => {
app.use(express.static(__dirname + '/resources/public'));
var log = function () {
var log = function() {
var date = new Date();
var timestamp = date.getDate() + "/" + date.getMonth() + " " + date.getHours() + ":" +
date.getMinutes() + ":" + date.getSeconds() + "." + date.getMilliseconds();
@@ -38,12 +38,12 @@ var log = function () {
console.log.apply(console, message);
}
app.get('/new', function (req, res) {
app.get('/new', function(req, res) {
log(req.ip, "opens /new");
res.send(view.newNotePage(getTimeStamp() + md5(Math.random())));
});
app.post('/note', function (req, res) {
app.post('/note', function(req, res) {
var body = req.body,
session = body.session,
note = body.note,
@@ -61,12 +61,20 @@ app.post('/note', function (req, res) {
storage.addNote(note, password).then(goToNote);
else {
CACHE.del(id);
if (body.button == "Delete") {
log("deleting note", id);
storage.deleteNote(id, password).then(
() => sendResponse(res, 200, "Note deleted"),
error => sendResponse(res, 403, error.message));
} else {
log("updating note", id);
storage.updateNote(id, password, note).then(goToNote,
error => sendResponse(res, 403, error.message));
}
}
});
app.get("/:year/:month/:day/:title", function (req, res) {
app.get("/:year/:month/:day/:title", function(req, res) {
var P = req.params, url = P.year + "/" + P.month + "/" + P.day + "/" + P.title;
log(req.ip, "resolves deprecated id", url);
if (CACHE.has(url)) {
@@ -86,7 +94,7 @@ app.get("/:year/:month/:day/:title", function (req, res) {
});
});
app.get(/\/([a-z0-9]+\/edit)/, function (req, res) {
app.get(/\/([a-z0-9]+\/edit)/, function(req, res) {
var link = req.params["0"].replace("/edit", "");
log(req.ip, "calls /edit on", link);
storage.getNote(link).then(note => res.send(note
@@ -94,7 +102,7 @@ app.get(/\/([a-z0-9]+\/edit)/, function (req, res) {
: notFound(res)));
});
app.get(/\/([a-z0-9]+\/export)/, function (req, res) {
app.get(/\/([a-z0-9]+\/export)/, function(req, res) {
var link = req.params["0"].replace("/export", "");
log(req.ip, "calls /export on", link);
res.set({ 'Content-Type': 'text/plain', 'Charset': 'utf-8' });
@@ -103,7 +111,7 @@ app.get(/\/([a-z0-9]+\/export)/, function (req, res) {
: notFound(res));
});
app.get(/\/([a-z0-9]+\/stats)/, function (req, res) {
app.get(/\/([a-z0-9]+\/stats)/, function(req, res) {
var link = req.params["0"].replace("/stats", "");
log(req.ip, "calls /stats on", link);
var promise = link in MODELS
@@ -114,7 +122,7 @@ app.get(/\/([a-z0-9]+\/stats)/, function (req, res) {
: notFound(res));
});
app.get(/\/([a-z0-9]+)/, function (req, res) {
app.get(/\/([a-z0-9]+)/, function(req, res) {
var link = req.params["0"];
log(req.ip, "open note", link, "from", req.get("Referer"));
if (CACHE.has(link)) {
@@ -144,8 +152,8 @@ var sendResponse = (res, code, message) => {
var notFound = res => sendResponse(res, 404, "Not found");
var server = app.listen(process.env.PORT || 3000, function () {
log('NoteHub server listening on port %s', server.address().port);
var server = app.listen(process.env.PORT || 3000, function() {
log('NoteHub server listening on port', server.address().port);
});
setInterval(() => {
+13 -6
View File
@@ -25,7 +25,7 @@ module.exports.getNote = id => Note.findById(id);
module.exports.getNoteId = deprecatedId => Note.findOne({
where: { deprecatedId: deprecatedId }
});
});
var generateId = () => [1, 1, 1, 1, 1]
.map(() => {
@@ -45,11 +45,18 @@ module.exports.addNote = (note, password) => getFreeId().then(id => Note.create(
password: password
}));
module.exports.updateNote = (id, password, text) => Note.findById(id).then(note => {
if (!note || note.password !== password) return new Promise((resolve, reject) => {
reject({ message: "Password is wrong" });
});
var passwordCheck = (note, password, callback) => (!note || note.password !== password)
? new Promise((resolve, reject) => reject({ message: "Password is wrong" }))
: callback();
module.exports.updateNote = (id, password, text) =>
Note.findById(id).then(note =>
passwordCheck(note, password, () => {
note.text = text;
note.edited = new Date();
return note.save();
});
}));
module.exports.deleteNote = (id, password) =>
Note.findById(id).then(note =>
passwordCheck(note, password, () => note.destroy()));