From bd8fcb527b75d17886b727bc3aaec19237ed2d89 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Fri, 29 Apr 2016 12:54:53 -0400 Subject: [PATCH] closes #4583 --- src/notifications.js | 15 ++++++++++++ src/posts/flags.js | 19 ++++++++-------- src/upgrade.js | 48 ++++++++++++++++++++++++++++++++++++++- src/user/notifications.js | 1 + 4 files changed, 72 insertions(+), 11 deletions(-) diff --git a/src/notifications.js b/src/notifications.js index ebf2d20585..8a6b41d303 100644 --- a/src/notifications.js +++ b/src/notifications.js @@ -81,6 +81,21 @@ var plugins = require('./plugins'); }); }; + Notifications.filterExists = function(nids, callback) { + // Removes nids that have been pruned + db.isSortedSetMembers('notifications', nids, function(err, exists) { + if (err) { + return callbacK(err); + } + + nids = nids.filter(function(notifId, idx) { + return exists[idx]; + }); + + callback(null, nids); + }); + }; + Notifications.findRelated = function(mergeIds, set, callback) { // A related notification is one in a zset that has the same mergeId var _nids; diff --git a/src/posts/flags.js b/src/posts/flags.js index 3adb6541f2..2334032ddd 100644 --- a/src/posts/flags.js +++ b/src/posts/flags.js @@ -67,13 +67,17 @@ module.exports = function(Posts) { } Posts.dismissFlag = function(pid, callback) { + var uid; + async.parallel([ function(next) { - db.getObjectField('post:' + pid, 'uid', function(err, uid) { + db.getObjectField('post:' + pid, 'uid', function(err, _uid) { if (err) { return next(err); } + uid = _uid; + db.sortedSetsRemove([ 'posts:flagged', 'posts:flags:count', @@ -81,15 +85,10 @@ module.exports = function(Posts) { ], pid, next); }); }, - function(next) { - db.deleteObjectField('post:' + pid, 'flags', next); - }, - function(next) { - db.delete('pid:' + pid + ':flag:uids', next); - }, - function(next) { - db.delete('pid:' + pid + ':flag:uid:reason', next); - } + async.apply(db.deleteObjectField, 'post:' + pid, 'flags'), + async.apply(db.delete, 'pid:' + pid + ':flag:uids'), + async.apply(db.delete, 'pid:' + pid + ':flag:uid:reason'), + async.apply(db.sortedSetRemove, 'notifications', 'post_flag:' + pid + ':uid:' + uid) ], function(err) { callback(err); }); diff --git a/src/upgrade.js b/src/upgrade.js index 36f5c14b39..87d2dc7c78 100644 --- a/src/upgrade.js +++ b/src/upgrade.js @@ -10,7 +10,7 @@ var db = require('./database'), schemaDate, thisSchemaDate, // IMPORTANT: REMEMBER TO UPDATE VALUE OF latestSchema - latestSchema = Date.UTC(2016, 3, 18); + latestSchema = Date.UTC(2016, 3, 29); Upgrade.check = function(callback) { db.get('schemaDate', function(err, value) { @@ -530,6 +530,52 @@ Upgrade.upgrade = function(callback) { winston.info('[2016/04/19] Users post count per tid skipped!'); next(); } + }, + function(next) { + thisSchemaDate = Date.UTC(2016, 3, 29); + + if (schemaDate < thisSchemaDate) { + updatesMade = true; + winston.info('[2016/04/29] Dismiss flags from deleted topics'); + + var posts = require('./posts'), + topics = require('./topics'); + + var pids, tids; + + async.waterfall([ + async.apply(db.getSortedSetRange, 'posts:flagged', 0, -1), + function(_pids, next) { + pids = _pids; + posts.getPostsFields(pids, ['tid'], next); + }, + function(_tids, next) { + tids = _tids.map(function(a) { + return a.tid; + }); + + topics.getTopicsFields(tids, ['deleted'], next); + }, + function(state, next) { + var toDismiss = state.map(function(a, idx) { + return parseInt(a.deleted, 10) === 1 ? pids[idx] : null; + }).filter(Boolean); + + winston.info('[2016/04/29] ' + toDismiss.length + ' dismissable flags found'); + async.each(toDismiss, posts.dismissFlag, next); + } + ], function(err) { + if (err) { + return next(err); + } + + winston.info('[2016/04/29] Dismiss flags from deleted topics done'); + Upgrade.update(thisSchemaDate, next); + }); + } else { + winston.info('[2016/04/29] Dismiss flags from deleted topics skipped!'); + next(); + } } // Add new schema updates here // IMPORTANT: REMEMBER TO UPDATE VALUE OF latestSchema IN LINE 24!!! diff --git a/src/user/notifications.js b/src/user/notifications.js index 9ad100db2b..2f766a72de 100644 --- a/src/user/notifications.js +++ b/src/user/notifications.js @@ -211,6 +211,7 @@ var async = require('async'), // Collapse any notifications with identical mergeIds async.waterfall([ async.apply(db.getSortedSetRevRange, 'uid:' + uid + ':notifications:unread', 0, 99), + async.apply(notifications.filterExists), function(nids, next) { var keys = nids.map(function(nid) { return 'notifications:' + nid;