From 2f1169d95b28388314cd23c64498436ed25160d3 Mon Sep 17 00:00:00 2001 From: Flummi Date: Tue, 20 Feb 2018 10:16:06 +0100 Subject: [PATCH] reply.. --- src/routes/r/index.mjs | 41 ++++++++++++++++++++--------- views/index.handlebars | 4 +-- views/partials/pasteview.handlebars | 2 +- views/viewsingle.handlebars | 2 +- 4 files changed, 32 insertions(+), 17 deletions(-) diff --git a/src/routes/r/index.mjs b/src/routes/r/index.mjs index f908849..dfb6e46 100644 --- a/src/routes/r/index.mjs +++ b/src/routes/r/index.mjs @@ -6,7 +6,8 @@ const queries = { getlangs: "select * from `languages` order by `name` asc", add: "insert into `pastes` (`uuid`,`lang`,`desc`,`paste`,`hidden`,`reply`) " + "values (LEFT(UUID(), 8), ?, ?, ?, ?, ?)", - getuuid: "select `uuid` from `pastes` where `id` = ? limit 1" + getuuid: "select `uuid` from `pastes` where `id` = ? limit 1", + getid: "select `id` from `pastes` where `uuid` = ? limit 1" }; let languages = new Map(); db.exec(queries.getlangs) @@ -14,9 +15,9 @@ db.exec(queries.getlangs) .catch(err => console.log(err)); export default express.Router() - .get("/", (req, res) => res.render("index", { languages: [...languages.values()] })) + .get(["/", "/r/:uuid"], (req, res) => res.render("index", { languages: [...languages.values()], reply: req.params.uuid || false })) .post("/", (req, res) => { - const data = { + let data = { desc: req.body.desc || "", content: req.body.content || false, lang: languages.getKey(req.body.lang) || 1, @@ -25,18 +26,32 @@ export default express.Router() }; if(!data.content) return res.render("index", { languages: [...languages.values()], notification: "nope" }); - db.exec(queries.add, [ - data.lang, - data.desc, - new Buffer(data.content).toString("base64"), - data.hidden, - data.reply - ]).then(insert => db.exec(queries.getuuid, [insert.insertId]) - .then(rows => res.redirect(`/${rows[0].uuid}/`)) - .catch(err => res.render("index", { languages: [...languages.values()], notification: "lol" })) - ).catch(err => res.render("index", { languages: [...languages.values()], notification: "didn't insert your paste lol" })); + + if(data.reply.length === 8) { + db.exec(queries.getid, [data.reply]).then(rows => { + data.reply = rows.length === 1 ? rows[0].id : 0; + savePaste(data, res); + }).catch(err => console.log(err)); + } + else { + data.reply = 0; + savePaste(data, res); + } }); +const savePaste = (data, res) => { + db.exec(queries.add, [ + data.lang, + data.desc, + new Buffer(data.content).toString("base64"), + data.hidden, + data.reply + ]).then(insert => db.exec(queries.getuuid, [insert.insertId]) + .then(rows => res.redirect(`/${rows[0].uuid}/`)) + .catch(err => res.render("index", { languages: [...languages.values()], notification: "lol" })) + ).catch(err => res.render("index", { languages: [...languages.values()], notification: "didn't insert your paste lol" })); +}; + Map.prototype.getKey = function(val) { if(!val) return 1; diff --git a/views/index.handlebars b/views/index.handlebars index 65514bb..0504ccd 100644 --- a/views/index.handlebars +++ b/views/index.handlebars @@ -1,4 +1,4 @@ -

New paste

+

New paste{{#if reply}} (reply to {{reply}}){{/if}}

Description:

@@ -8,6 +8,6 @@
  • Hide from recent pastes:

  • - +
    \ No newline at end of file diff --git a/views/partials/pasteview.handlebars b/views/partials/pasteview.handlebars index e4adcbb..df3ea9f 100644 --- a/views/partials/pasteview.handlebars +++ b/views/partials/pasteview.handlebars @@ -2,7 +2,7 @@

    {{language}}

    -

    Paste: /{{uuid}}/Show related

    +

    Paste: /{{uuid}}/Reply

    {{{desc}}}

    diff --git a/views/viewsingle.handlebars b/views/viewsingle.handlebars index 18d8920..8d2dd60 100644 --- a/views/viewsingle.handlebars +++ b/views/viewsingle.handlebars @@ -1,7 +1,7 @@