Note deletion feature added
This commit is contained in:
+7
-7
@@ -23,15 +23,15 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://github.com/chmllr/NoteHub",
|
"homepage": "https://github.com/chmllr/NoteHub",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"body-parser": "^1.14.1",
|
"body-parser": "^1.15.0",
|
||||||
"express": "^4.13.3",
|
"express": "^4.13.4",
|
||||||
"lru-cache": "^2.6.5",
|
"lru-cache": "^4.0.0",
|
||||||
"marked": "^0.3.5",
|
"marked": "^0.3.5",
|
||||||
"md5": "^2.0.0",
|
"md5": "^2.1.0",
|
||||||
"sequelize": "^3.8.0",
|
"sequelize": "^3.19.3",
|
||||||
"sqlite3": "^3.1.0"
|
"sqlite3": "*"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"nodemon": "^1.7.3"
|
"nodemon": "^1.9.1"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-1
@@ -22,7 +22,8 @@
|
|||||||
<textarea id="note" name="note">%CONTENT%</textarea>
|
<textarea id="note" name="note">%CONTENT%</textarea>
|
||||||
<fieldset id="input-elems">
|
<fieldset id="input-elems">
|
||||||
<input class="ui-elem" id="plain-password" name="plain-password" placeholder="Password for editing" type="text">
|
<input class="ui-elem" id="plain-password" name="plain-password" placeholder="Password for editing" type="text">
|
||||||
<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">
|
<span id="tableau">
|
||||||
0 words
|
0 words
|
||||||
</span>
|
</span>
|
||||||
|
|||||||
@@ -18,6 +18,7 @@
|
|||||||
<article class="bottom-space">
|
<article class="bottom-space">
|
||||||
<h2>Changelog</h2>
|
<h2>Changelog</h2>
|
||||||
<ul>
|
<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 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>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>
|
<li><strong>2014-09</strong>: text size setting added</li>
|
||||||
|
|||||||
@@ -4,8 +4,7 @@ var $ = function(id) {
|
|||||||
var iosDetected = navigator.userAgent.match("(iPad|iPod|iPhone)");
|
var iosDetected = navigator.userAgent.match("(iPad|iPod|iPhone)");
|
||||||
var timer = null;
|
var timer = null;
|
||||||
var timerDelay = iosDetected ? 800 : 400;
|
var timerDelay = iosDetected ? 800 : 400;
|
||||||
var $note, $action, $preview, $plain_password,
|
var $note, $action, $preview, $plain_password, $tableau;
|
||||||
updatePreview, $tableau;
|
|
||||||
var backendTimer;
|
var backendTimer;
|
||||||
|
|
||||||
function md2html(input) {
|
function md2html(input) {
|
||||||
@@ -25,7 +24,7 @@ function onLoad() {
|
|||||||
$preview = $("draft");
|
$preview = $("draft");
|
||||||
$tableau = $("tableau");
|
$tableau = $("tableau");
|
||||||
$plain_password = $("plain-password");
|
$plain_password = $("plain-password");
|
||||||
updatePreview = function() {
|
var updatePreview = function() {
|
||||||
clearTimeout(timer);
|
clearTimeout(timer);
|
||||||
var content = $note.value;
|
var content = $note.value;
|
||||||
var delay = Math.min(timerDelay, timerDelay * (content.length / 400));
|
var delay = Math.min(timerDelay, timerDelay * (content.length / 400));
|
||||||
@@ -36,6 +35,7 @@ function onLoad() {
|
|||||||
};
|
};
|
||||||
if ($action == "UPDATE") updatePreview();
|
if ($action == "UPDATE") updatePreview();
|
||||||
else {
|
else {
|
||||||
|
$("delete-button").style.display = "none";
|
||||||
$note.value = "";
|
$note.value = "";
|
||||||
var draft = localStorage.getItem("draft");
|
var draft = localStorage.getItem("draft");
|
||||||
if (draft) {
|
if (draft) {
|
||||||
@@ -44,14 +44,13 @@ function onLoad() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
$note.onkeyup = updatePreview;
|
$note.onkeyup = updatePreview;
|
||||||
$("publish-button").onclick = function(e) {
|
$("delete-button").onclick = $("publish-button").onclick = function(e) {
|
||||||
localStorage.removeItem("draft");
|
localStorage.removeItem("draft");
|
||||||
self.onbeforeunload = null;;
|
self.onbeforeunload = null;
|
||||||
if ($plain_password.value != "") $("password").value = md5($plain_password.value);
|
if ($plain_password.value != "") $("password").value = md5($plain_password.value);
|
||||||
$plain_password.value = null;
|
$plain_password.value = null;
|
||||||
$("signature").value = md5($("session").value +
|
$("signature").value = md5($("session").value + $note.value.replace(/[\n\r]/g, ""));
|
||||||
$note.value.replace(/[\n\r]/g, ""));
|
};
|
||||||
}
|
|
||||||
if (iosDetected) $note.className += " ui-border";
|
if (iosDetected) $note.className += " ui-border";
|
||||||
else $note.focus();
|
else $note.focus();
|
||||||
self.onbeforeunload = saveDraft;
|
self.onbeforeunload = saveDraft;
|
||||||
|
|||||||
@@ -11,145 +11,153 @@ app.use(bodyParser.urlencoded({ extended: true }));
|
|||||||
|
|
||||||
var MODELS = {};
|
var MODELS = {};
|
||||||
var CACHE = new LRU({
|
var CACHE = new LRU({
|
||||||
max: 50,
|
max: 50,
|
||||||
dispose: key => {
|
dispose: key => {
|
||||||
log("disposing", key, "from cache");
|
log("disposing", key, "from cache");
|
||||||
var model = MODELS[key];
|
var model = MODELS[key];
|
||||||
model && model.save();
|
model && model.save();
|
||||||
delete MODELS[key];
|
delete MODELS[key];
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
var getTimeStamp = () => {
|
var getTimeStamp = () => {
|
||||||
var timestamp = new Date().getTime();
|
var timestamp = new Date().getTime();
|
||||||
timestamp = Math.floor(timestamp / 10000000);
|
timestamp = Math.floor(timestamp / 10000000);
|
||||||
return (timestamp).toString(16)
|
return (timestamp).toString(16)
|
||||||
}
|
}
|
||||||
|
|
||||||
app.use(express.static(__dirname + '/resources/public'));
|
app.use(express.static(__dirname + '/resources/public'));
|
||||||
|
|
||||||
var log = function () {
|
var log = function() {
|
||||||
var date = new Date();
|
var date = new Date();
|
||||||
var timestamp = date.getDate() + "/" + date.getMonth() + " " + date.getHours() + ":" +
|
var timestamp = date.getDate() + "/" + date.getMonth() + " " + date.getHours() + ":" +
|
||||||
date.getMinutes() + ":" + date.getSeconds() + "." + date.getMilliseconds();
|
date.getMinutes() + ":" + date.getSeconds() + "." + date.getMilliseconds();
|
||||||
var message = Array.prototype.slice.call(arguments);
|
var message = Array.prototype.slice.call(arguments);
|
||||||
message.unshift("--");
|
message.unshift("--");
|
||||||
message.unshift(timestamp);
|
message.unshift(timestamp);
|
||||||
console.log.apply(console, message);
|
console.log.apply(console, message);
|
||||||
}
|
}
|
||||||
|
|
||||||
app.get('/new', function (req, res) {
|
app.get('/new', function(req, res) {
|
||||||
log(req.ip, "opens /new");
|
log(req.ip, "opens /new");
|
||||||
res.send(view.newNotePage(getTimeStamp() + md5(Math.random())));
|
res.send(view.newNotePage(getTimeStamp() + md5(Math.random())));
|
||||||
});
|
});
|
||||||
|
|
||||||
app.post('/note', function (req, res) {
|
app.post('/note', function(req, res) {
|
||||||
var body = req.body,
|
var body = req.body,
|
||||||
session = body.session,
|
session = body.session,
|
||||||
note = body.note,
|
note = body.note,
|
||||||
password = body.password,
|
password = body.password,
|
||||||
action = body.action,
|
action = body.action,
|
||||||
id = body.id;
|
id = body.id;
|
||||||
log(req.ip, "calls /note to", action, id);
|
log(req.ip, "calls /note to", action, id);
|
||||||
var goToNote = note => res.redirect("/" + note.id);
|
var goToNote = note => res.redirect("/" + note.id);
|
||||||
if (session.indexOf(getTimeStamp()) != 0)
|
if (session.indexOf(getTimeStamp()) != 0)
|
||||||
return sendResponse(res, 400, "Session expired");
|
return sendResponse(res, 400, "Session expired");
|
||||||
var expectedSignature = md5(session + note.replace(/[\n\r]/g, ""));
|
var expectedSignature = md5(session + note.replace(/[\n\r]/g, ""));
|
||||||
if (expectedSignature != body.signature)
|
if (expectedSignature != body.signature)
|
||||||
return sendResponse(res, 400, "Signature mismatch");
|
return sendResponse(res, 400, "Signature mismatch");
|
||||||
if (action == "POST")
|
if (action == "POST")
|
||||||
storage.addNote(note, password).then(goToNote);
|
storage.addNote(note, password).then(goToNote);
|
||||||
else {
|
else {
|
||||||
CACHE.del(id);
|
CACHE.del(id);
|
||||||
storage.updateNote(id, password, note).then(goToNote,
|
if (body.button == "Delete") {
|
||||||
error => sendResponse(res, 403, error.message));
|
log("deleting note", id);
|
||||||
}
|
storage.deleteNote(id, password).then(
|
||||||
});
|
() => sendResponse(res, 200, "Note deleted"),
|
||||||
|
error => sendResponse(res, 403, error.message));
|
||||||
app.get("/:year/:month/:day/:title", function (req, res) {
|
} else {
|
||||||
var P = req.params, url = P.year + "/" + P.month + "/" + P.day + "/" + P.title;
|
log("updating note", id);
|
||||||
log(req.ip, "resolves deprecated id", url);
|
storage.updateNote(id, password, note).then(goToNote,
|
||||||
if (CACHE.has(url)) {
|
error => sendResponse(res, 403, error.message));
|
||||||
log(url, "is cached!");
|
}
|
||||||
var id = CACHE.get(url);
|
|
||||||
if (id) res.redirect("/" + id);
|
|
||||||
else notFound(res);
|
|
||||||
} else storage.getNoteId(url).then(note => {
|
|
||||||
log(url, "is not cached, resolving...");
|
|
||||||
if (note) {
|
|
||||||
CACHE.set(url, note.id);
|
|
||||||
res.redirect("/" + note.id)
|
|
||||||
} else {
|
|
||||||
CACHE.set(url, null);
|
|
||||||
notFound(res);
|
|
||||||
}
|
}
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get(/\/([a-z0-9]+\/edit)/, function (req, res) {
|
app.get("/:year/:month/:day/:title", function(req, res) {
|
||||||
var link = req.params["0"].replace("/edit", "");
|
var P = req.params, url = P.year + "/" + P.month + "/" + P.day + "/" + P.title;
|
||||||
log(req.ip, "calls /edit on", link);
|
log(req.ip, "resolves deprecated id", url);
|
||||||
storage.getNote(link).then(note => res.send(note
|
if (CACHE.has(url)) {
|
||||||
? view.editNotePage(getTimeStamp() + md5(Math.random()), note)
|
log(url, "is cached!");
|
||||||
: notFound(res)));
|
var id = CACHE.get(url);
|
||||||
|
if (id) res.redirect("/" + id);
|
||||||
|
else notFound(res);
|
||||||
|
} else storage.getNoteId(url).then(note => {
|
||||||
|
log(url, "is not cached, resolving...");
|
||||||
|
if (note) {
|
||||||
|
CACHE.set(url, note.id);
|
||||||
|
res.redirect("/" + note.id)
|
||||||
|
} else {
|
||||||
|
CACHE.set(url, null);
|
||||||
|
notFound(res);
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get(/\/([a-z0-9]+\/export)/, function (req, res) {
|
app.get(/\/([a-z0-9]+\/edit)/, function(req, res) {
|
||||||
var link = req.params["0"].replace("/export", "");
|
var link = req.params["0"].replace("/edit", "");
|
||||||
log(req.ip, "calls /export on", link);
|
log(req.ip, "calls /edit on", link);
|
||||||
res.set({ 'Content-Type': 'text/plain', 'Charset': 'utf-8' });
|
storage.getNote(link).then(note => res.send(note
|
||||||
storage.getNote(link).then(note => note
|
? view.editNotePage(getTimeStamp() + md5(Math.random()), note)
|
||||||
? res.send(note.text)
|
: notFound(res)));
|
||||||
: notFound(res));
|
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get(/\/([a-z0-9]+\/stats)/, function (req, res) {
|
app.get(/\/([a-z0-9]+\/export)/, function(req, res) {
|
||||||
var link = req.params["0"].replace("/stats", "");
|
var link = req.params["0"].replace("/export", "");
|
||||||
log(req.ip, "calls /stats on", link);
|
log(req.ip, "calls /export on", link);
|
||||||
var promise = link in MODELS
|
res.set({ 'Content-Type': 'text/plain', 'Charset': 'utf-8' });
|
||||||
? new Promise(resolve => resolve(MODELS[link]))
|
storage.getNote(link).then(note => note
|
||||||
: storage.getNote(link);
|
? res.send(note.text)
|
||||||
promise.then(note => note
|
: notFound(res));
|
||||||
? res.send(view.renderStats(note))
|
|
||||||
: notFound(res));
|
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get(/\/([a-z0-9]+)/, function (req, res) {
|
app.get(/\/([a-z0-9]+\/stats)/, function(req, res) {
|
||||||
var link = req.params["0"];
|
var link = req.params["0"].replace("/stats", "");
|
||||||
log(req.ip, "open note", link, "from", req.get("Referer"));
|
log(req.ip, "calls /stats on", link);
|
||||||
if (CACHE.has(link)) {
|
var promise = link in MODELS
|
||||||
log(link, "is cached!");
|
? new Promise(resolve => resolve(MODELS[link]))
|
||||||
var note = MODELS[link];
|
: storage.getNote(link);
|
||||||
if (!note) return notFound(res);
|
promise.then(note => note
|
||||||
note.views++;
|
? res.send(view.renderStats(note))
|
||||||
res.send(CACHE.get(link));
|
: notFound(res));
|
||||||
} else storage.getNote(link).then(note => {
|
});
|
||||||
log(link, "is not cached, resolving...");
|
|
||||||
if (!note) {
|
app.get(/\/([a-z0-9]+)/, function(req, res) {
|
||||||
CACHE.set(link, null);
|
var link = req.params["0"];
|
||||||
return notFound(res);
|
log(req.ip, "open note", link, "from", req.get("Referer"));
|
||||||
}
|
if (CACHE.has(link)) {
|
||||||
var content = view.renderNote(note);
|
log(link, "is cached!");
|
||||||
CACHE.set(link, content);
|
var note = MODELS[link];
|
||||||
MODELS[link] = note;
|
if (!note) return notFound(res);
|
||||||
note.views++;
|
note.views++;
|
||||||
res.send(content);
|
res.send(CACHE.get(link));
|
||||||
});
|
} else storage.getNote(link).then(note => {
|
||||||
|
log(link, "is not cached, resolving...");
|
||||||
|
if (!note) {
|
||||||
|
CACHE.set(link, null);
|
||||||
|
return notFound(res);
|
||||||
|
}
|
||||||
|
var content = view.renderNote(note);
|
||||||
|
CACHE.set(link, content);
|
||||||
|
MODELS[link] = note;
|
||||||
|
note.views++;
|
||||||
|
res.send(content);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
var sendResponse = (res, code, message) => {
|
var sendResponse = (res, code, message) => {
|
||||||
log("sending response", code, message);
|
log("sending response", code, message);
|
||||||
res.status(code).send(view.renderPage(message, "<h1>" + message + "</h1>", ""));
|
res.status(code).send(view.renderPage(message, "<h1>" + message + "</h1>", ""));
|
||||||
}
|
}
|
||||||
|
|
||||||
var notFound = res => sendResponse(res, 404, "Not found");
|
var notFound = res => sendResponse(res, 404, "Not found");
|
||||||
|
|
||||||
var server = app.listen(process.env.PORT || 3000, function () {
|
var server = app.listen(process.env.PORT || 3000, function() {
|
||||||
log('NoteHub server listening on port %s', server.address().port);
|
log('NoteHub server listening on port', server.address().port);
|
||||||
});
|
});
|
||||||
|
|
||||||
setInterval(() => {
|
setInterval(() => {
|
||||||
var keys = Object.keys(MODELS);
|
var keys = Object.keys(MODELS);
|
||||||
log("saving stats for", keys.length, "models...");
|
log("saving stats for", keys.length, "models...");
|
||||||
keys.forEach(id => MODELS[id].save())
|
keys.forEach(id => MODELS[id].save())
|
||||||
}, 60 * 5 * 1000);
|
}, 60 * 5 * 1000);
|
||||||
+40
-33
@@ -1,22 +1,22 @@
|
|||||||
var Sequelize = require('sequelize');
|
var Sequelize = require('sequelize');
|
||||||
var sequelize = new Sequelize('database', null, null, {
|
var sequelize = new Sequelize('database', null, null, {
|
||||||
dialect: 'sqlite',
|
dialect: 'sqlite',
|
||||||
pool: {
|
pool: {
|
||||||
max: 5,
|
max: 5,
|
||||||
min: 0,
|
min: 0,
|
||||||
idle: 10000
|
idle: 10000
|
||||||
},
|
},
|
||||||
storage: 'database.sqlite'
|
storage: 'database.sqlite'
|
||||||
});
|
});
|
||||||
|
|
||||||
var Note = sequelize.define('Note', {
|
var Note = sequelize.define('Note', {
|
||||||
id: { type: Sequelize.STRING(6), unique: true, primaryKey: true },
|
id: { type: Sequelize.STRING(6), unique: true, primaryKey: true },
|
||||||
deprecatedId: Sequelize.TEXT,
|
deprecatedId: Sequelize.TEXT,
|
||||||
text: Sequelize.TEXT,
|
text: Sequelize.TEXT,
|
||||||
published: { type: Sequelize.DATE, defaultValue: Sequelize.NOW },
|
published: { type: Sequelize.DATE, defaultValue: Sequelize.NOW },
|
||||||
edited: { type: Sequelize.DATE, allowNull: true, defaultValue: null },
|
edited: { type: Sequelize.DATE, allowNull: true, defaultValue: null },
|
||||||
password: Sequelize.STRING(16),
|
password: Sequelize.STRING(16),
|
||||||
views: { type: Sequelize.INTEGER, defaultValue: 0 }
|
views: { type: Sequelize.INTEGER, defaultValue: 0 }
|
||||||
});
|
});
|
||||||
|
|
||||||
sequelize.sync();
|
sequelize.sync();
|
||||||
@@ -25,31 +25,38 @@ module.exports.getNote = id => Note.findById(id);
|
|||||||
|
|
||||||
module.exports.getNoteId = deprecatedId => Note.findOne({
|
module.exports.getNoteId = deprecatedId => Note.findOne({
|
||||||
where: { deprecatedId: deprecatedId }
|
where: { deprecatedId: deprecatedId }
|
||||||
});
|
});
|
||||||
|
|
||||||
var generateId = () => [1, 1, 1, 1, 1]
|
var generateId = () => [1, 1, 1, 1, 1]
|
||||||
.map(() => {
|
.map(() => {
|
||||||
var code = Math.floor(Math.random() * 36);
|
var code = Math.floor(Math.random() * 36);
|
||||||
return String.fromCharCode(code + (code < 10 ? 48 : 87));
|
return String.fromCharCode(code + (code < 10 ? 48 : 87));
|
||||||
})
|
})
|
||||||
.join("");
|
.join("");
|
||||||
|
|
||||||
var getFreeId = () => {
|
var getFreeId = () => {
|
||||||
var id = generateId();
|
var id = generateId();
|
||||||
return Note.findById(id).then(result => result ? getFreeId() : id);
|
return Note.findById(id).then(result => result ? getFreeId() : id);
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports.addNote = (note, password) => getFreeId().then(id => Note.create({
|
module.exports.addNote = (note, password) => getFreeId().then(id => Note.create({
|
||||||
id: id,
|
id: id,
|
||||||
text: note,
|
text: note,
|
||||||
password: password
|
password: password
|
||||||
}));
|
}));
|
||||||
|
|
||||||
module.exports.updateNote = (id, password, text) => Note.findById(id).then(note => {
|
var passwordCheck = (note, password, callback) => (!note || note.password !== password)
|
||||||
if (!note || note.password !== password) return new Promise((resolve, reject) => {
|
? new Promise((resolve, reject) => reject({ message: "Password is wrong" }))
|
||||||
reject({ message: "Password is wrong" });
|
: callback();
|
||||||
});
|
|
||||||
note.text = text;
|
module.exports.updateNote = (id, password, text) =>
|
||||||
note.edited = new Date();
|
Note.findById(id).then(note =>
|
||||||
return note.save();
|
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()));
|
||||||
Reference in New Issue
Block a user