redirection to note id added
This commit is contained in:
@@ -14,7 +14,8 @@ app.get('/new', function (req, res) {
|
||||
|
||||
app.get("/:year/:month/:day/:title", function (req, res) {
|
||||
var P = req.params;
|
||||
res.send("opening note " + P.title);
|
||||
storage.getNoteId(P.year + "/" + P.month + "/" + P.day + "/" + P.title)
|
||||
.then(id => res.redirect("/" + id));
|
||||
});
|
||||
|
||||
app.get(/\/([a-zA-Z0-9]*)/, function (req, res) {
|
||||
|
||||
+8
-72
@@ -13,52 +13,20 @@ var sequelize = new Sequelize('database', null, null, {
|
||||
});
|
||||
|
||||
var Note = sequelize.define('Note', {
|
||||
id: {
|
||||
type: Sequelize.INTEGER,
|
||||
autoIncrement: true,
|
||||
unique: true,
|
||||
primaryKey: true
|
||||
},
|
||||
id: { type: Sequelize.STRING(6), unique: true, primaryKey: true },
|
||||
deprecatedId: Sequelize.TEXT,
|
||||
text: Sequelize.TEXT,
|
||||
published: {
|
||||
type: Sequelize.DATE,
|
||||
defaultValue: Sequelize.NOW
|
||||
},
|
||||
edited: {
|
||||
type: Sequelize.DATE,
|
||||
allowNull: true,
|
||||
defaultValue: null
|
||||
},
|
||||
publisher: Sequelize.STRING(32),
|
||||
published: { type: Sequelize.DATE, defaultValue: Sequelize.NOW },
|
||||
edited: { type: Sequelize.DATE, allowNull: true, defaultValue: null },
|
||||
password: Sequelize.STRING(16),
|
||||
views: Sequelize.INTEGER,
|
||||
});
|
||||
|
||||
var Link = sequelize.define('Link', {
|
||||
id: {
|
||||
type: Sequelize.STRING,
|
||||
unique: true,
|
||||
primaryKey: true
|
||||
},
|
||||
lastUsage: {
|
||||
type: Sequelize.DATE,
|
||||
allowNull: true,
|
||||
defaultValue: null
|
||||
},
|
||||
params: Sequelize.STRING,
|
||||
deprecatedId: Sequelize.STRING
|
||||
});
|
||||
|
||||
Note.hasMany(Link);
|
||||
Link.belongsTo(Note);
|
||||
|
||||
sequelize.sync().then(function() {
|
||||
client.hgetall("note", function(err, notes) {
|
||||
console.log("notes retrieved:", Object.keys(notes).length);
|
||||
client.hgetall("published", function(err, published) {
|
||||
console.log("published retrieved:", Object.keys(published).length);
|
||||
client.hgetall("publisher", function(err, publisher) {
|
||||
console.log("publisher retrieved:", Object.keys(publisher).length);
|
||||
client.hgetall("password", function(err, password) {
|
||||
console.log("password retrieved:", Object.keys(password).length);
|
||||
client.hgetall("views", function(err, views) {
|
||||
@@ -68,43 +36,16 @@ sequelize.sync().then(function() {
|
||||
Object.keys(notes).forEach(function(id) {
|
||||
client.smembers(id + ":urls", function(err, links) {
|
||||
|
||||
var createLink = LinkId => {
|
||||
Note.create({
|
||||
id: LinkId,
|
||||
deprecatedId: id,
|
||||
text: notes[id],
|
||||
published: published[id] && new Date(published[id] * 1000) || new Date(),
|
||||
publisher: publisher[id].indexOf("NPY") == -1 && publisher[id] || "NoteHub",
|
||||
password: password[id] && password[id].length == 32 && password[id],
|
||||
edited: !isNaN(edited[id]) && edited[id] && new Date(edited[id] * 1000) || null,
|
||||
views: views[id],
|
||||
}).then(note => {
|
||||
|
||||
var createLink = LinkId => {
|
||||
client.hget("short-url", LinkId, function(err, result) {
|
||||
|
||||
|
||||
var obj = {};
|
||||
if (result) {
|
||||
result = result.replace(/:([\w_-]+)\s/g, '"$1":');
|
||||
try {
|
||||
obj = JSON.parse(result);
|
||||
delete obj.title;
|
||||
delete obj.day;
|
||||
delete obj.year;
|
||||
delete obj.month;
|
||||
} catch (e) {
|
||||
return console.log("PARSE ERROR FOR", result)
|
||||
}
|
||||
}
|
||||
Link.create({
|
||||
id: LinkId,
|
||||
deprecatedId: id,
|
||||
params: Object.keys(obj).length == 0 ? null : JSON.stringify(obj)
|
||||
}).then(link => {
|
||||
|
||||
link.setNote(note);
|
||||
note.addLink(link);
|
||||
|
||||
});
|
||||
});
|
||||
})
|
||||
};
|
||||
|
||||
if (links.length == 0) {
|
||||
@@ -117,10 +58,6 @@ sequelize.sync().then(function() {
|
||||
});
|
||||
} else createLink(links[links.length - 1]);
|
||||
|
||||
|
||||
|
||||
|
||||
});
|
||||
})
|
||||
});
|
||||
});
|
||||
@@ -128,5 +65,4 @@ sequelize.sync().then(function() {
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
})
|
||||
|
||||
+3
-2
@@ -2,8 +2,9 @@ var marked = require("marked");
|
||||
var fs = require("fs");
|
||||
|
||||
var template = fs.readFileSync("resources/template.html", "utf-8");
|
||||
var buildHTML = (title, content) => template
|
||||
var buildHTML = (id, title, content) => template
|
||||
.replace("%TITLE%", title)
|
||||
.replace(/%LINK%/g, id)
|
||||
.replace("%CONTENT%", content);
|
||||
|
||||
module.exports.build = note => buildHTML(note.title, marked(note.text));
|
||||
module.exports.build = note => buildHTML(note.id, note.title, marked(note.text));
|
||||
|
||||
+11
-13
@@ -10,25 +10,23 @@ var sequelize = new Sequelize('database', null, null, {
|
||||
});
|
||||
|
||||
var Note = sequelize.define('Note', {
|
||||
id: { type: Sequelize.INTEGER, autoIncrement: true, unique: true, primaryKey: true },
|
||||
id: { type: Sequelize.STRING(6), unique: true, primaryKey: true },
|
||||
deprecatedId: Sequelize.TEXT,
|
||||
text: Sequelize.TEXT,
|
||||
published: { type: Sequelize.DATE, defaultValue: Sequelize.NOW },
|
||||
edited: { type: Sequelize.DATE, allowNull: true, defaultValue: null },
|
||||
publisher: Sequelize.STRING(32),
|
||||
password: Sequelize.STRING(16),
|
||||
views: Sequelize.INTEGER,
|
||||
});
|
||||
|
||||
var Link = sequelize.define('Link', {
|
||||
id: { type: Sequelize.STRING, unique: true, primaryKey: true },
|
||||
lastUsage: { type: Sequelize.DATE, allowNull: true, defaultValue: null },
|
||||
params: Sequelize.STRING
|
||||
});
|
||||
module.exports.getNote = id => {
|
||||
console.log("resolving note", id);
|
||||
return Note.findById(id);
|
||||
}
|
||||
|
||||
Note.hasMany(Link);
|
||||
Link.belongsTo(Note);
|
||||
|
||||
module.exports.getNote = linkId => {
|
||||
console.log("resolving note", linkId);
|
||||
return Link.findById(linkId).then(link => Note.findById(link.NoteId));
|
||||
module.exports.getNoteId = deprecatedId => {
|
||||
console.log("resolving deprecated Id", deprecatedId);
|
||||
return Note.findOne({
|
||||
where: { deprecatedId: deprecatedId }
|
||||
}).then(note => note.id);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user