Skip to content

Node.js based forum software built for the modern web

nested reply improvements

barisusaklieca850c4b11d
diff --git a/public/src/client/topic/events.js b/public/src/client/topic/events.js
index f6a6aecc6d..3f2f1a6887 100644
--- a/public/src/client/topic/events.js
+++ b/public/src/client/topic/events.js
@@ -166,15 +166,16 @@ define('forum/topic/events', [
return false;
}
- function onPostPurged(pid) {
- components.get('post', 'pid', pid).fadeOut(500, function () {
+ function onPostPurged(postData) {
+ components.get('post', 'pid', postData.pid).fadeOut(500, function () {
$(this).remove();
- ajaxify.data.postcount --;
- postTools.updatePostCount(ajaxify.data.postcount);
posts.showBottomPostBar();
});
-
- postTools.updatePostCount();
+ ajaxify.data.postcount --;
+ postTools.updatePostCount(ajaxify.data.postcount);
+ require(['forum/topic/replies'], function (replies) {
+ replies.onPostPurged(postData);
+ });
}
function togglePostDeleteState(data) {
diff --git a/public/src/client/topic/posts.js b/public/src/client/topic/posts.js
index ca12c815f4..e6ed0f6bd7 100644
--- a/public/src/client/topic/posts.js
+++ b/public/src/client/topic/posts.js
@@ -37,6 +37,10 @@ define('forum/topic/posts', [
} else {
onNewPostInfiniteScroll(data);
}
+
+ require(['forum/topic/replies'], function (replies) {
+ replies.onNewPost(data);
+ });
};
Posts.modifyPostsByPrivileges = function (posts) {
diff --git a/public/src/client/topic/replies.js b/public/src/client/topic/replies.js
index e7357bcbd7..f229e20dcc 100644
--- a/public/src/client/topic/replies.js
+++ b/public/src/client/topic/replies.js
@@ -42,6 +42,8 @@ define('forum/topic/replies', ['navigator', 'components', 'forum/topic/posts'],
var tplData = {
posts: data,
privileges: ajaxify.data.privileges,
+ 'downvote:disabled': ajaxify.data['downvote:disabled'],
+ 'reputation:disabled': ajaxify.data['reputation:disabled'],
loggedIn: !!app.user.uid,
hideReplies: true
};
@@ -60,5 +62,34 @@ define('forum/topic/replies', ['navigator', 'components', 'forum/topic/posts'],
}
}
+ Replies.onNewPost = function (data) {
+ var post = data.posts[0];
+ if (!post) {
+ return;
+ }
+ incrementCount(post, 1);
+ data.hideReplies = true;
+ app.parseAndTranslate('topic', 'posts', data, function (html) {
+ var replies = $('[component="post"][data-pid="' + post.toPid + '"] [component="post/replies"]').first();
+ if (replies.length) {
+ replies.append(html);
+ posts.processPage(html);
+ }
+ });
+ };
+
+ Replies.onPostPurged = function (post) {
+ incrementCount(post, -1);
+ };
+
+ function incrementCount(post, inc) {
+ var replyCount = $('[component="post"][data-pid="' + post.toPid + '"]').find('[component="post/reply-count"]').first();
+ var countEl = replyCount.find('[component="post/reply-count/text"]');
+ var count = Math.max(0, parseInt(countEl.attr('data-replies'), 10) + inc);
+ countEl.attr('data-replies', count);
+ replyCount.toggleClass('hidden', !count);
+ countEl.translateText('[[topic:replies_to_this_post, ' + count + ']]');
+ }
+
return Replies;
});
diff --git a/src/socket.io/posts/tools.js b/src/socket.io/posts/tools.js
index ae04ed6c6d..06f7e0b22c 100644
--- a/src/socket.io/posts/tools.js
+++ b/src/socket.io/posts/tools.js
@@ -1,7 +1,6 @@
'use strict';
var async = require('async');
-var winston = require('winston');
var validator = require('validator');
var posts = require('../../posts');
@@ -137,28 +136,29 @@ module.exports = function (SocketPosts) {
SocketPosts.purge = function (socket, data, callback) {
function purgePost() {
- posts.tools.purge(socket.uid, data.pid, function (err) {
- if (err) {
- return callback(err);
- }
-
- websockets.in('topic_' + data.tid).emit('event:post_purged', data.pid);
-
- topics.getTopicField(data.tid, 'title', function (err, title) {
- if (err) {
- return winston.error(err);
- }
+ var postData;
+ async.waterfall([
+ function (next) {
+ posts.getPostField(data.pid, 'toPid', next);
+ },
+ function (toPid, next) {
+ postData = {pid: data.pid, toPid: toPid};
+ posts.tools.purge(socket.uid, data.pid, next);
+ },
+ function (next) {
+ websockets.in('topic_' + data.tid).emit('event:post_purged', postData);
+ topics.getTopicField(data.tid, 'title', next);
+ },
+ function (title, next) {
events.log({
type: 'post-purge',
uid: socket.uid,
pid: data.pid,
ip: socket.ip,
title: validator.escape(String(title))
- });
- });
-
- callback();
- });
+ }, next);
+ }
+ ], callback);
}
if (!data || !parseInt(data.pid, 10)) {
diff --git a/test/posts.js b/test/posts.js
index 3750a27939..b121eed0c0 100644
--- a/test/posts.js
+++ b/test/posts.js
@@ -8,6 +8,7 @@ var db = require('./mocks/databasemock');
var topics = require('../src/topics');
var posts = require('../src/posts');
var categories = require('../src/categories');
+var privileges = require('../src/privileges');
var user = require('../src/user');
describe('Post\'s', function () {
@@ -135,6 +136,7 @@ describe('Post\'s', function () {
describe('delete/restore/purge', function () {
var pid;
+ var socketPosts = require('../src/socket.io/posts');
before(function (done) {
topics.reply({
uid: voterUid,
@@ -144,14 +146,13 @@ describe('Post\'s', function () {
}, function (err, data) {
assert.ifError(err);
pid = data.pid;
- done();
+ privileges.categories.give(['purge'], cid, 'registered-users', done);
});
});
it('should delete a post', function (done) {
- posts.delete(pid, voterUid, function (err, postData) {
+ socketPosts.delete({uid: voterUid}, {pid: pid, tid: topicData.tid}, function (err) {
assert.ifError(err);
- assert(postData);
posts.getPostField(pid, 'deleted', function (err, isDeleted) {
assert.ifError(err);
assert.equal(parseInt(isDeleted, 10), 1);
@@ -161,9 +162,8 @@ describe('Post\'s', function () {
});
it('should restore a post', function (done) {
- posts.restore(pid, voterUid, function (err, postData) {
+ socketPosts.restore({uid: voterUid}, {pid: pid, tid: topicData.tid}, function (err) {
assert.ifError(err);
- assert(postData);
posts.getPostField(pid, 'deleted', function (err, isDeleted) {
assert.ifError(err);
assert.equal(parseInt(isDeleted, 10), 0);
@@ -173,9 +173,8 @@ describe('Post\'s', function () {
});
it('should purge a post', function (done) {
- posts.purge(pid, voterUid, function (err) {
+ socketPosts.purge({uid: voterUid}, {pid: pid}, function (err) {
assert.ifError(err);
- assert.equal(arguments.length, 1);
posts.exists('post:' + pid, function (err, exists) {
assert.ifError(err);
assert.equal(exists, false);