diff --git a/src/posts/delete.js b/src/posts/delete.js index f47ff28e88..dd8abe21ac 100644 --- a/src/posts/delete.js +++ b/src/posts/delete.js @@ -33,6 +33,9 @@ module.exports = function(Posts) { }, function(next) { Posts.dismissFlag(pid, next); + }, + function(next) { + topics.updateTeaser(postData.tid, next); } ], function(err) { callback(err, postData); @@ -64,6 +67,9 @@ module.exports = function(Posts) { }, function(next) { db.sortedSetAdd('cid:' + cid + ':pids', postData.timestamp, pid, next); + }, + function(next) { + topics.updateTeaser(postData.tid, next); } ], function(err) { callback(err, postData); diff --git a/src/topics/posts.js b/src/topics/posts.js index b80b0b4935..e895556145 100644 --- a/src/topics/posts.js +++ b/src/topics/posts.js @@ -165,17 +165,23 @@ module.exports = function(Topics) { }; Topics.getLatestUndeletedPid = function(tid, callback) { - Topics.getLatestUndeletedReply(tid, function(err, pid) { - if (err) { - return callback(err); - } - if (parseInt(pid, 10)) { - return callback(null, pid.toString()); + async.waterfall([ + function(next) { + Topics.getLatestUndeletedReply(tid, next); + }, + function(pid, next) { + if (parseInt(pid, 10)) { + return callback(null, pid.toString()); + } + Topics.getTopicField(tid, 'mainPid', next); + }, + function(mainPid, next) { + posts.getPostFields(mainPid, ['pid', 'deleted'], next); + }, + function(mainPost, next) { + next(null, parseInt(mainPost.pid, 10) && parseInt(mainPost.deleted, 10) !== 1 ? mainPost.pid.toString() : null); } - Topics.getTopicField(tid, 'mainPid', function(err, mainPid) { - callback(err, parseInt(mainPid, 10) ? mainPid.toString() : null); - }); - }); + ], callback); }; Topics.getLatestUndeletedReply = function(tid, callback) { @@ -199,8 +205,11 @@ module.exports = function(Topics) { if (err) { return next(err); } - latestPid = pids[0]; + isDeleted = parseInt(deleted, 10) === 1; + if (!isDeleted) { + latestPid = pids[0]; + } ++index; next(); }); diff --git a/src/topics/teaser.js b/src/topics/teaser.js index 5946eef988..c2b4b0e13c 100644 --- a/src/topics/teaser.js +++ b/src/topics/teaser.js @@ -101,11 +101,12 @@ module.exports = function(Topics) { }; Topics.updateTeaser = function(tid, callback) { - db.getSortedSetRevRange('tid:' + tid + ':posts', 0, 0, function(err, pids) { + Topics.getLatestUndeletedPid(tid, function(err, pid) { if (err) { return callback(err); } - var pid = Array.isArray(pids) && pids.length ? pids[0] : null; + + pid = pid || null; Topics.setTopicField(tid, 'teaserPid', pid, callback); }); };