diff --git a/index.js b/index.js index b453b21..a63b304 100644 --- a/index.js +++ b/index.js @@ -29,8 +29,6 @@ dbInit.init({ seed : process.env.DB_SEED }); -console.log( "exit") - const express = require('express'); const app = express(); @@ -71,7 +69,7 @@ app.post('/*', routes.add); app.get('/*', routes.main); app.patch('/*', routes.main); app.put('/*', routes.main); -app.delete('/*', routes.main); +app.delete('/delete/:id', routes.delete); app.listen(port, () => { diff --git a/public/js/app.js b/public/js/app.js index 5a1a728..7838bd4 100644 --- a/public/js/app.js +++ b/public/js/app.js @@ -1,107 +1,128 @@ /* global initData, authorizationToken */ -// List of HTML entities for escaping. -var htmlEscapes = { - '&': '&', - '<': '<', - '>': '>', - '"': '"', - "'": ''', - '/': '/' -}; +$(function(){ + + // List of HTML entities for escaping. + var htmlEscapes = { + '&': '&', + '<': '<', + '>': '>', + '"': '"', + "'": ''', + '/': '/' + }; -// Regex containing the keys listed immediately above. -var htmlEscaper = /[&<>"'\/]/g; + // Regex containing the keys listed immediately above. + var htmlEscaper = /[&<>"'\/]/g; -// Escape a string for HTML interpolation. -escape = function(string) { - return ('' + string).replace(htmlEscaper, function(match) { - return htmlEscapes[match]; - }); -}; -var urlRegex = /(\S+): (https?://[^\s]+)/g; -url = function(string){ - return ''+string.replace(urlRegex, '$1'); -}; -var titleRegex = /^(.*\n)/; -title = function(string){ - return ''+string.replace(titleRegex, '$1'); -}; -var cmdRegex = /```([^`]*?)```/g; -cmd = function(string) { - return ''+string.replace(cmdRegex, '$1'); -}; -date = function(string){ - var D = new Date(string); - return D.toLocaleDateString()+" "+D.toLocaleTimeString(); -}; -mailRegexp = /(.*) <(.+@.+)>/; -mail = function( string ){ - return ''+string.replace(mailRegexp, '$1'); -}; -function updatePage(data){ - - var content = ""; - var item = {}; - var id = ''; - // If the log entry is unique, simulate a search result - if( ! data['hits'] ){ - data = {hits:{hits:[data]}}; - } - $.each(data.hits.hits, (k,v)=>{ - - item = v._source; - id = v._id; - content += ` + // Escape a string for HTML interpolation. + escape = function(string) { + return ('' + string).replace(htmlEscaper, function(match) { + return htmlEscapes[match]; + }); + }; + var urlRegex = /(\S+): (https?://[^\s]+)/g; + url = function(string){ + return ''+string.replace(urlRegex, '$1'); + }; + var titleRegex = /^(.*\n)/; + title = function(string){ + return ''+string.replace(titleRegex, '$1'); + }; + var cmdRegex = /```([^`]*?)```/g; + cmd = function(string) { + return ''+string.replace(cmdRegex, '$1'); + }; + date = function(string){ + var D = new Date(string); + return D.toLocaleDateString()+" "+D.toLocaleTimeString(); + }; + mailRegexp = /(.*) <(.+@.+)>/; + mail = function( string ){ + return ''+string.replace(mailRegexp, `$1`); + }; + function updatePage(data){ -
-
-

${escape(item.server)}

- - ${date(escape(item.created_at))}
-
-
-

${mail(escape(item.author))}

-

- Actions -

-
- Remove - Edit -
-
-
-
-
 ${cmd(title(url(escape(item.content))))}
-
-
- `; - }); - $("#content").html(content); - -} - -$("input").on("keyup",function(e){ - const el = $(e.target); - const val = el.val(); - if( val.length < 3 ){ return; } - $.ajax("/search",{ - beforeSend: function(request) { - request.setRequestHeader("authorizationToken", authorizationToken); - }, - data: { - q:val, + var content = ""; + var item = {}; + var id = ''; + // If the log entry is unique, simulate a search result + if( ! data['hits'] ){ + data = {hits:{hits:[data]}}; } - }) - .done(function(data) { - updatePage(data); - }) - .fail(function() { - alert( "error" ); + $.each(data.hits.hits, (k,v)=>{ + + item = v._source; + id = v._id; + content += ` + +
+
+

${escape(item.server)}

+ + ${date(escape(item.created_at))}
+
+
+

${mail(escape(item.author))}

+

+ Actions +

+
+ Remove + Edit +
+
+
+
+
 ${cmd(title(url(escape(item.content))))}
+
+
+ `; + }); + $("#content").html(content); + + } + + $("input").on("keyup",function(e){ + const el = $(e.target); + const val = el.val(); + if( val.length < 3 ){ return; } + $.ajax("/search",{ + beforeSend: function(request) { + request.setRequestHeader("authorizationToken", authorizationToken); + }, + data: { + q:val, + } + }) + .done(function(data) { + updatePage(data); + }) + .fail(function() { + alert( "error" ); + }); }); + + updatePage( initData ); + + $(".actions-toggle").on("click",(e) => { var el=e.target; $(el).parent().siblings('.actions').show(); $(el).hide(); } ) + + $('.delete').on('click', (e) => { + const el = $(e.target); + const url = el.attr('href'); + $.ajax(url,{ + method: "DELETE", + beforeSend: function(request) { + request.setRequestHeader("authorizationToken", authorizationToken); + } + }) + .done(function(data) { + $(el).parents('.log').remove(); + }) + .fail(function() { + alert( "error" ); + }); + return false; + }); + }); - -updatePage( initData ); - -$(".actions-toggle").on("click",(e) => { var el=e.target; $(el).parent().siblings('.actions').show(); $(el).hide(); } ) - diff --git a/routes/index.js b/routes/index.js index 6514d2c..6ac1b64 100644 --- a/routes/index.js +++ b/routes/index.js @@ -54,6 +54,53 @@ const routes = { res.json({"health":0,"msg":"Lost connection to ES"}); }); }, + delete: (req,res) => { + const id= req.params.id; + // Reindex the doc to the "trash" index + var log = client.reindex({ + refresh: true, + max_docs: 1, + body: { + source: { + index: 'changelog', + query: { + term: { + _id: id + } + } + }, + dest: { + index: 'changelog-trash', + } + } + }) + .then( (results, err) => { + + console.log(`reindexing success for id ${id}`) + // Remove it from the original index + return client.delete({ + index: "changelog", + id: id + }); + + }, (e) => { + + console.log("reindexing error") + res.status(400); + res.end("error"); + + }) + .then( (results, err) => { + + console.log(`Delete success for id ${id}`) + res.end("ok"); + + },(results, err) => { + console.log(`Delete error for id ${id}`) + res.status(400); + res.end("error"); + }); + }, add: (req, res) => { const body = req.body;