Skip to content

Node.js based forum software built for the modern web

post deletion

Julian Lam156d5dce08a3
diff --git a/public/templates/topic.tpl b/public/templates/topic.tpl
index 63c4771783..9882cab6c7 100644
--- a/public/templates/topic.tpl
+++ b/public/templates/topic.tpl
@@ -9,7 +9,7 @@
<ul id="post-container" class="post-container container">
<!-- BEGIN posts -->
-<li class="row {posts.deleted-class}">
+<li class="row" data-pid="{posts.pid}" data-deleted="{posts.deleted}">
<div class="span1 profile-image-block visible-desktop">
<!--<i class="icon-spinner icon-spin icon-2x pull-left"></i>-->
<a href="/users/{posts.username}">
@@ -210,6 +210,14 @@
}
});
}
+
+ // Fix delete state for this thread's posts
+ var postEls = document.querySelectorAll('#post-container li[data-deleted]');
+ for(var x=0,numPosts=postEls.length;x<numPosts;x++) {
+ console.log(postEls[x].getAttribute('data-pid'));
+ if (postEls[x].getAttribute('data-deleted') === '1') toggle_post_delete_state(postEls[x].getAttribute('data-pid'));
+ postEls[x].removeAttribute('data-deleted');
+ }
});
$('.post-container').delegate('.edit', 'click', function(e) {
@@ -218,11 +226,15 @@
});
$('.post-container').delegate('.delete', 'click', function(e) {
- var pid = ($(this).attr('id') || $(this.parentNode).attr('id')).split('_')[1];
- confirmDel = confirm('Delete this post?');
+ var pid = ($(this).attr('id') || $(this.parentNode).attr('id')).split('_')[1],
+ postEl = $(document.querySelector('#post-container li[data-pid="' + pid + '"]')),
+ deleteAction = !postEl.hasClass('deleted') ? true : false,
+ confirmDel = confirm((deleteAction ? 'Delete' : 'Restore') + ' this post?');
if (confirmDel) {
- socket.emit('api:posts.delete', { pid: pid });
+ deleteAction ?
+ socket.emit('api:posts.delete', { pid: pid }) :
+ socket.emit('api:posts.restore', { pid: pid });
}
});
@@ -320,8 +332,11 @@
});
socket.on('event:post_deleted', function(data) {
- console.log(data);
- if (data.pid) set_post_delete_state(data.pid, true);
+ if (data.pid) toggle_post_delete_state(data.pid, true);
+ });
+
+ socket.on('event:post_restored', function(data) {
+ if (data.pid) toggle_post_delete_state(data.pid, true);
});
function adjust_rep(value, pid, uid) {
@@ -495,8 +510,23 @@
}
}
- function set_post_delete_state(pid, deleted) {
- console.log('SETTING DELETE STATE: ', pid, deleted);
+ function toggle_post_delete_state(pid) {
+ var postEl = $(document.querySelector('#post-container li[data-pid="' + pid + '"]'));
+ quoteEl = $(postEl[0].querySelector('.quote')),
+ favEl = $(postEl[0].querySelector('.favourite')),
+ replyEl = $(postEl[0].querySelector('.post_reply'));
+
+ if (!postEl.hasClass('deleted')) {
+ quoteEl.addClass('none');
+ favEl.addClass('none');
+ replyEl.addClass('none');
+ } else {
+ quoteEl.removeClass('none');
+ favEl.removeClass('none');
+ replyEl.removeClass('none');
+ }
+
+ postEl.toggleClass('deleted');
}
})();
</script>
\ No newline at end of file
diff --git a/src/posts.js b/src/posts.js
index 2cd4b5d08e..2dd84fbe85 100644
--- a/src/posts.js
+++ b/src/posts.js
@@ -23,29 +23,32 @@ marked.setOptions({
function generateThread() {
if (!post_data || !user_data || !thread_data || !vote_data || !viewer_data) return;
- var posts = [];
+ var posts = [],
+ manage_content = viewer_data.reputation >= config.privilege_thresholds.manage_content;
for (var i=0, ii= post_data.pid.length; i<ii; i++) {
var uid = post_data.uid[i],
pid = post_data.pid[i];
- posts.push({
- 'pid' : pid,
- 'uid' : uid,
- 'content' : marked(post_data.content[i] || ''),
- 'post_rep' : post_data.reputation[i] || 0,
- 'timestamp' : post_data.timestamp[i],
- 'relativeTime': utils.relativeTime(post_data.timestamp[i]),
- 'username' : user_data[uid].username || 'anonymous',
- 'user_rep' : user_data[uid].reputation || 0,
- 'gravatar' : user_data[uid].picture,
- 'fav_star_class' : vote_data[pid] ? 'icon-star' : 'icon-star-empty',
- 'display_moderator_tools': (uid == current_user || viewer_data.reputation >= config.privilege_thresholds.manage_content) ? 'show' : 'none',
- 'edited-class': post_data.editor[i] !== null ? '' : 'none',
- 'editor': post_data.editor[i] !== null ? user_data[post_data.editor[i]].username : '',
- 'relativeEditTime': post_data.editTime !== null ? utils.relativeTime(post_data.editTime[i]) : '',
- 'deleted-class': post_data.deleted[i] === '1' ? 'deleted' : ''
- });
+ if (post_data.deleted[i] === null || (post_data.deleted[i] === '1' && manage_content)) {
+ posts.push({
+ 'pid' : pid,
+ 'uid' : uid,
+ 'content' : marked(post_data.content[i] || ''),
+ 'post_rep' : post_data.reputation[i] || 0,
+ 'timestamp' : post_data.timestamp[i],
+ 'relativeTime': utils.relativeTime(post_data.timestamp[i]),
+ 'username' : user_data[uid].username || 'anonymous',
+ 'user_rep' : user_data[uid].reputation || 0,
+ 'gravatar' : user_data[uid].picture,
+ 'fav_star_class' : vote_data[pid] ? 'icon-star' : 'icon-star-empty',
+ 'display_moderator_tools': (uid == current_user || manage_content) ? 'show' : 'none',
+ 'edited-class': post_data.editor[i] !== null ? '' : 'none',
+ 'editor': post_data.editor[i] !== null ? user_data[post_data.editor[i]].username : '',
+ 'relativeEditTime': post_data.editTime !== null ? utils.relativeTime(post_data.editTime[i]) : '',
+ 'deleted': post_data.deleted[i] || '0'
+ });
+ }
}
callback({
@@ -331,4 +334,23 @@ marked.setOptions({
}
});
}
+
+ Posts.restore = function(uid, pid) {
+ RDB.mget(['pid:' + pid + ':tid', 'pid:' + pid + ':uid'], function(err, results) {
+ var tid = results[0],
+ author = results[1],
+ success = function() {
+ RDB.del('pid:' + pid + ':deleted');
+
+ io.sockets.in('topic_' + tid).emit('event:post_restored', { pid: pid });
+ };
+
+ if (uid === author) success();
+ else {
+ user.getUserField(uid, 'reputation', function(reputation) {
+ if (reputation >= config.privilege_thresholds.manage_content) success();
+ });
+ }
+ });
+ }
}(exports));
\ No newline at end of file