From 3c32d8600f3c0f09f8dd98559edb5fe02ae5178c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Tue, 23 Jul 2019 21:11:04 -0400 Subject: [PATCH] fix: #7788 No new posts (#7793) * feat: debug no-new-posts * feat: add test for user delete * fix: timeout for flag test * feat: shorter --- src/categories/recentreplies.js | 3 ++- src/posts/delete.js | 20 ++++++++++++-------- src/topics/delete.js | 4 ++++ src/topics/tools.js | 1 - test/flags.js | 2 +- test/posts.js | 31 +++++++++++++++++++++++++++++++ 6 files changed, 50 insertions(+), 11 deletions(-) diff --git a/src/categories/recentreplies.js b/src/categories/recentreplies.js index b624c88aa8..6ddf6b95c1 100644 --- a/src/categories/recentreplies.js +++ b/src/categories/recentreplies.js @@ -29,7 +29,8 @@ module.exports = function (Categories) { return await db.sortedSetAdd('cid:' + cid + ':recent_tids', Date.now(), tid); } const data = await db.getSortedSetRangeWithScores('cid:' + cid + ':recent_tids', 0, count - numRecentReplies); - if (data.length) { + const shouldRemove = !(data.length === 1 && count === 1 && data[0].value === String(tid)); + if (data.length && shouldRemove) { await db.sortedSetsRemoveRangeByScore(['cid:' + cid + ':recent_tids'], '-inf', data[data.length - 1].score); } await db.sortedSetAdd('cid:' + cid + ':recent_tids', Date.now(), tid); diff --git a/src/posts/delete.js b/src/posts/delete.js index e3c135a211..8bd17e02b8 100644 --- a/src/posts/delete.js +++ b/src/posts/delete.js @@ -36,6 +36,7 @@ module.exports = function (Posts) { db.sortedSetRemove('cid:' + topicData.cid + ':pids', pid) : db.sortedSetAdd('cid:' + topicData.cid + ':pids', postData.timestamp, pid), ]); + await categories.updateRecentTidForCid(postData.cid); plugins.fireHook('action:post.' + type, { post: _.clone(postData), uid: uid }); return postData; } @@ -45,11 +46,12 @@ module.exports = function (Posts) { if (!postData) { return; } - + const topicData = await topics.getTopicFields(postData.tid, ['tid', 'cid', 'pinned']); + postData.cid = topicData.cid; await plugins.fireHook('filter:post.purge', { post: postData, pid: pid, uid: uid }); await Promise.all([ - deletePostFromTopicUserNotification(postData), - deletePostFromCategoryRecentPosts(pid), + deletePostFromTopicUserNotification(postData, topicData), + deletePostFromCategoryRecentPosts(postData), deletePostFromUsersBookmarks(pid), deletePostFromUsersVotes(pid), deletePostFromReplies(postData), @@ -60,13 +62,13 @@ module.exports = function (Posts) { await db.delete('post:' + pid); }; - async function deletePostFromTopicUserNotification(postData) { + async function deletePostFromTopicUserNotification(postData, topicData) { await db.sortedSetsRemove([ 'tid:' + postData.tid + ':posts', 'tid:' + postData.tid + ':posts:votes', 'uid:' + postData.uid + ':posts', ], postData.pid); - const topicData = await topics.getTopicFields(postData.tid, ['tid', 'cid', 'pinned']); + const tasks = [ db.decrObjectField('global', 'postCount'), db.decrObjectField('category:' + topicData.cid, 'post_count'), @@ -79,16 +81,18 @@ module.exports = function (Posts) { user.incrementUserPostCountBy(postData.uid, -1), notifications.rescind('new_post:tid:' + postData.tid + ':pid:' + postData.pid + ':uid:' + postData.uid), ]; + if (!topicData.pinned) { - tasks.push(db.sortedSetIncrBy, 'cid:' + topicData.cid + ':tids:posts', -1, postData.tid); + tasks.push(db.sortedSetIncrBy('cid:' + topicData.cid + ':tids:posts', -1, postData.tid)); } await Promise.all(tasks); } - async function deletePostFromCategoryRecentPosts(pid) { + async function deletePostFromCategoryRecentPosts(postData) { const cids = await categories.getAllCidsFromSet('categories:cid'); const sets = cids.map(cid => 'cid:' + cid + ':pids'); - await db.sortedSetsRemove(sets, pid); + await db.sortedSetsRemove(sets, postData.pid); + await categories.updateRecentTidForCid(postData.cid); } async function deletePostFromUsersBookmarks(pid) { diff --git a/src/topics/delete.js b/src/topics/delete.js index c83b11d4b5..f04d24e778 100644 --- a/src/topics/delete.js +++ b/src/topics/delete.js @@ -5,6 +5,7 @@ var db = require('../database'); var user = require('../user'); var posts = require('../posts'); +const categories = require('../categories'); var plugins = require('../plugins'); var batch = require('../batch'); @@ -33,6 +34,7 @@ module.exports = function (Topics) { Topics.getPids(tid), ]); await db.sortedSetRemove('cid:' + cid + ':pids', pids); + await categories.updateRecentTidForCid(cid); } async function addTopicPidsToCid(tid) { @@ -49,6 +51,7 @@ module.exports = function (Topics) { scores.push(post.timestamp); }); await db.sortedSetAdd('cid:' + cid + ':pids', scores, pidsToAdd); + await categories.updateRecentTidForCid(cid); } Topics.restore = async function (tid) { @@ -137,6 +140,7 @@ module.exports = function (Topics) { ], tid), user.decrementUserFieldBy(topicData.uid, 'topiccount', 1), ]); + await categories.updateRecentTidForCid(topicData.cid); } async function reduceCounters(tid) { diff --git a/src/topics/tools.js b/src/topics/tools.js index 8e47e65391..a46d468209 100644 --- a/src/topics/tools.js +++ b/src/topics/tools.js @@ -43,7 +43,6 @@ module.exports = function (Topics) { } else { await Topics.restore(tid); } - await categories.updateRecentTidForCid(topicData.cid); topicData.deleted = isDelete ? 1 : 0; diff --git a/test/flags.js b/test/flags.js index a7996b9933..b157ba26c0 100644 --- a/test/flags.js +++ b/test/flags.js @@ -449,7 +449,7 @@ describe('Flags', function () { } assert.strictEqual('[1,"this is my note"]', notes[0]); - done(); + setTimeout(done, 10); }); }); }); diff --git a/test/posts.js b/test/posts.js index 673a46c173..09043e2650 100644 --- a/test/posts.js +++ b/test/posts.js @@ -73,6 +73,37 @@ describe('Post\'s', function () { }); }); + it('should update category teaser properly', async function () { + const util = require('util'); + const getCategoriesAsync = util.promisify(async function getCategories(callback) { + request(nconf.get('url') + '/api/categories', { json: true }, function (err, res, body) { + callback(err, body); + }); + }); + + const postResult = await topics.post({ uid: globalModUid, cid: cid, title: 'topic title', content: '123456789' }); + + let data = await getCategoriesAsync(); + assert.equal(data.categories[0].teaser.pid, postResult.postData.pid); + assert.equal(data.categories[0].posts[0].content, '123456789'); + assert.equal(data.categories[0].posts[0].pid, postResult.postData.pid); + + const newUid = await user.create({ username: 'teaserdelete' }); + const newPostResult = await topics.post({ uid: newUid, cid: cid, title: 'topic title', content: 'xxxxxxxx' }); + + data = await getCategoriesAsync(); + assert.equal(data.categories[0].teaser.pid, newPostResult.postData.pid); + assert.equal(data.categories[0].posts[0].content, 'xxxxxxxx'); + assert.equal(data.categories[0].posts[0].pid, newPostResult.postData.pid); + + await user.delete(1, newUid); + + data = await getCategoriesAsync(); + assert.equal(data.categories[0].teaser.pid, postResult.postData.pid); + assert.equal(data.categories[0].posts[0].content, '123456789'); + assert.equal(data.categories[0].posts[0].pid, postResult.postData.pid); + }); + it('should change owner of post and topic properly', async function () { const oldUid = await user.create({ username: 'olduser' }); const newUid = await user.create({ username: 'newuser' });