remove stale comments from DB, closes #9

This commit is contained in:
Martin Zimmermann 2013-09-08 17:21:33 +02:00
parent f1ffe444bc
commit f0c1574c15
4 changed files with 61 additions and 18 deletions

View File

@ -81,7 +81,7 @@ define(["lib/q", "app/models"], function(Q, models) {
return curl("DELETE", endpoint + "/?" + qs({uri: location, id: id}), null)
.then(function(rv) {
if (rv.status == 200) {
return rv;
return JSON.parse(rv.body) == null;
} else {
throw {status: rv.status, reason: rv.body}
}

View File

@ -73,7 +73,11 @@ define(["lib/q", "lib/HTML", "helper/utils", "./api", "./forms", "./logging"], f
delbtn.addEventListener("click", function(event) {
if (delbtn.textContent == "Bestätigen") {
api.remove(comment.id).then(function(rv) {
console.log(rv);
if (rv) {
node.remove();
} else {
// XXX recursively remove deleted comments
}
})
} else {
delbtn.textContent = "Bestätigen"
@ -83,20 +87,6 @@ define(["lib/q", "lib/HTML", "helper/utils", "./api", "./forms", "./logging"], f
})
}
// if (read(path + '-' + post['id'])) {
// $('#isso_' + post['id'] + '> footer > a:first-child')
// .after(brew(['a', {'class': 'delete', 'href': '#'}, 'Löschen']))
// .after(brew(['a', {'class': 'edit', 'href': '#'}, 'Bearbeiten']));
//
// // DELETE
// $('#isso_' + post['id'] + ' > footer .delete').on('click', function(event) {
// isso.remove(post['id'], function(status, rv) {
// // XXX comment might not actually deleted
// $('#isso_' + post['id']).remove();
// });
// event.stop();
// });
//
// // EDIT
// $('#isso_' + post['id'] + ' > footer .edit').on('click', function(event) {
//

View File

@ -157,19 +157,39 @@ class SQLite(Abstract):
return self.query2comment(con.execute(
'SELECT * FROM comments WHERE path=? AND id=?;', (path, id)).fetchone())
def _remove_stale(self, con, path):
sql = ('DELETE FROM',
' comments',
'WHERE',
' path=? AND mode=4 AND id NOT IN (',
' SELECT',
' parent',
' FROM',
' comments',
' WHERE path=? AND parent IS NOT NULL)')
while con.execute(' '.join(sql), (path, path)).rowcount:
continue
def delete(self, path, id):
with sqlite3.connect(self.dbpath) as con:
refs = con.execute('SELECT * FROM comments WHERE path=? AND parent=?', (path, id)).fetchone()
sql = 'SELECT * FROM comments WHERE path=? AND parent=?'
refs = con.execute(sql, (path, id)).fetchone()
if refs is None:
con.execute('DELETE FROM comments WHERE path=? AND id=?', (path, id))
self._remove_stale(con, path)
return None
con.execute('UPDATE comments SET text=? WHERE path=? AND id=?', ('', path, id))
con.execute('UPDATE comments SET mode=? WHERE path=? AND id=?', (4, path, id))
for field in ('text', 'author', 'website'):
for field in ('author', 'website'):
con.execute('UPDATE comments SET %s=? WHERE path=? AND id=?' % field,
(None, path, id))
self._remove_stale(con, path)
return self.get(path, id)
def like(self, path, id, remote_addr):

View File

@ -117,6 +117,39 @@ class TestComments(unittest.TestCase):
assert self.get('/?uri=%2Fpath%2F&id=1').status_code == 200
assert self.get('/?uri=%2Fpath%2F&id=2').status_code == 200
r = client.delete('/?uri=%2Fpath%2F&id=2')
assert self.get('/?uri=%2Fpath%2F').status_code == 404
def testDeleteWithMultipleReferences(self):
"""
[ comment 1 ]
|
--- [ comment 2, ref 1 ]
|
--- [ comment 3, ref 2 ]
|
--- [ comment 4, ref 2 ]
[ comment 5 ]
"""
client = Client(self.app, Response)
client.post('/new?uri=%2Fpath%2F', data=json.dumps({'text': 'First'}))
client.post('/new?uri=%2Fpath%2F', data=json.dumps({'text': 'Second', 'parent': 1}))
client.post('/new?uri=%2Fpath%2F', data=json.dumps({'text': 'Third 1', 'parent': 2}))
client.post('/new?uri=%2Fpath%2F', data=json.dumps({'text': 'Third 2', 'parent': 2}))
client.post('/new?uri=%2Fpath%2F', data=json.dumps({'text': '...'}))
client.delete('/?uri=%2Fpath%2F&id=1')
assert self.get('/?uri=%2Fpath%2F').status_code == 200
client.delete('/?uri=%2Fpath%2F&id=2')
assert self.get('/?uri=%2Fpath%2F').status_code == 200
client.delete('/?uri=%2Fpath%2F&id=3')
assert self.get('/?uri=%2Fpath%2F').status_code == 200
client.delete('/?uri=%2Fpath%2F&id=4')
assert self.get('/?uri=%2Fpath%2F').status_code == 200
client.delete('/?uri=%2Fpath%2F&id=5')
assert self.get('/?uri=%2Fpath%2F').status_code == 404
def testPathVariations(self):
paths = ['/sub/path/', '/path.html', '/sub/path.html', 'path', '/']