[enh] The delete action should work

This commit is contained in:
alban 2020-05-23 23:26:51 +02:00
parent 3ce2e4514d
commit b0c6043741
3 changed files with 168 additions and 102 deletions

View File

@ -29,8 +29,6 @@ dbInit.init({
seed : process.env.DB_SEED seed : process.env.DB_SEED
}); });
console.log( "exit")
const express = require('express'); const express = require('express');
const app = express(); const app = express();
@ -71,7 +69,7 @@ app.post('/*', routes.add);
app.get('/*', routes.main); app.get('/*', routes.main);
app.patch('/*', routes.main); app.patch('/*', routes.main);
app.put('/*', routes.main); app.put('/*', routes.main);
app.delete('/*', routes.main); app.delete('/delete/:id', routes.delete);
app.listen(port, () => { app.listen(port, () => {

View File

@ -1,107 +1,128 @@
/* global initData, authorizationToken */ /* global initData, authorizationToken */
// List of HTML entities for escaping. $(function(){
var htmlEscapes = {
'&': '&', // List of HTML entities for escaping.
'<': '&lt;', var htmlEscapes = {
'>': '&gt;', '&': '&amp;',
'"': '&quot;', '<': '&lt;',
"'": '&#x27;', '>': '&gt;',
'/': '&#x2F;' '"': '&quot;',
}; "'": '&#x27;',
'/': '&#x2F;'
};
// Regex containing the keys listed immediately above. // Regex containing the keys listed immediately above.
var htmlEscaper = /[&<>"'\/]/g; var htmlEscaper = /[&<>"'\/]/g;
// Escape a string for HTML interpolation. // Escape a string for HTML interpolation.
escape = function(string) { escape = function(string) {
return ('' + string).replace(htmlEscaper, function(match) { return ('' + string).replace(htmlEscaper, function(match) {
return htmlEscapes[match]; return htmlEscapes[match];
}); });
}; };
var urlRegex = /(\S+): (https?:&#x2F;&#x2F;[^\s]+)/g; var urlRegex = /(\S+): (https?:&#x2F;&#x2F;[^\s]+)/g;
url = function(string){ url = function(string){
return ''+string.replace(urlRegex, '<a target="_blank" href="$2">$1</a>'); return ''+string.replace(urlRegex, '<a target="_blank" href="$2">$1</a>');
}; };
var titleRegex = /^(.*\n)/; var titleRegex = /^(.*\n)/;
title = function(string){ title = function(string){
return ''+string.replace(titleRegex, '<b>$1</b>'); return ''+string.replace(titleRegex, '<b>$1</b>');
}; };
var cmdRegex = /```([^`]*?)```/g; var cmdRegex = /```([^`]*?)```/g;
cmd = function(string) { cmd = function(string) {
return ''+string.replace(cmdRegex, '<span class="cmd">$1</span>'); return ''+string.replace(cmdRegex, '<span class="cmd">$1</span>');
}; };
date = function(string){ date = function(string){
var D = new Date(string); var D = new Date(string);
return D.toLocaleDateString()+" "+D.toLocaleTimeString(); return D.toLocaleDateString()+" "+D.toLocaleTimeString();
}; };
mailRegexp = /(.*) &lt;(.+@.+)&gt;/; mailRegexp = /(.*) &lt;(.+@.+)&gt;/;
mail = function( string ){ mail = function( string ){
return ''+string.replace(mailRegexp, '<a href="mailto:$2">$1</a>'); return ''+string.replace(mailRegexp, `<a href="mailto:${string}">$1</a>`);
}; };
function updatePage(data){ 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 += `
<div class="log row"> var content = "";
<div class="meta col-lg-2 "> var item = {};
<p class="server"> ${escape(item.server)} </p> var id = '';
<a href="/log/${id}"> // If the log entry is unique, simulate a search result
${date(escape(item.created_at))} <br/> if( ! data['hits'] ){
</a> data = {hits:{hits:[data]}};
<div class="d-none d-lg-block">
<p class="author"> ${mail(escape(item.author))} </p>
<p>
<a class="actions-toggle btn-link btn-sm">Actions</a>
</p>
<div class="actions btn-group btn-group-sm" role="group" aria-label="log actions">
<a class="destroy btn btn btn-outline-secondary" href="/destroy/${id}">Remove</a>
<a class="edit btn btn btn-outline-secondary" href="/edit/${id}">Edit</a>
</div>
</div>
</div>
<div class="col-lg">
<pre> ${cmd(title(url(escape(item.content))))}</pre>
</div>
</div>
`;
});
$("#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,
} }
}) $.each(data.hits.hits, (k,v)=>{
.done(function(data) {
updatePage(data); item = v._source;
}) id = v._id;
.fail(function() { content += `
alert( "error" );
<div class="log row">
<div class="meta col-lg-2 ">
<p class="server"> ${escape(item.server)} </p>
<a href="/log/${id}">
${date(escape(item.created_at))} <br/>
</a>
<div class="d-none d-lg-block">
<p class="author"> ${mail(escape(item.author))} </p>
<p>
<a class="actions-toggle btn-link btn-sm">Actions</a>
</p>
<div class="actions btn-group btn-group-sm" role="group" aria-label="log actions">
<a class="delete btn btn btn-outline-secondary" href="/delete/${id}">Remove</a>
<a class="edit btn btn btn-outline-secondary" href="/edit/${id}">Edit</a>
</div>
</div>
</div>
<div class="col-lg">
<pre> ${cmd(title(url(escape(item.content))))}</pre>
</div>
</div>
`;
});
$("#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(); } )

View File

@ -54,6 +54,53 @@ const routes = {
res.json({"health":0,"msg":"Lost connection to ES"}); 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) => { add: (req, res) => {
const body = req.body; const body = req.body;