Skip to content

Commit

Permalink
closes #4583
Browse files Browse the repository at this point in the history
  • Loading branch information
julianlam committed Apr 29, 2016
1 parent 1cdd4ea commit bd8fcb5
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 11 deletions.
15 changes: 15 additions & 0 deletions src/notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
19 changes: 9 additions & 10 deletions src/posts/flags.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,29 +67,28 @@ 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',
'uid:' + uid + ':flag:pids'
], 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);
});
Expand Down
48 changes: 47 additions & 1 deletion src/upgrade.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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!!!
Expand Down
1 change: 1 addition & 0 deletions src/user/notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit bd8fcb5

Please sign in to comment.