mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-02 03:55:55 +01:00
optimize topics.markTopicNotificationsRead
let's not call it once for each tid
This commit is contained in:
@@ -220,7 +220,7 @@ define('forum/topic/events', [
|
||||
function onNewNotification(data) {
|
||||
var tid = ajaxify.data.tid;
|
||||
if (data && data.tid && parseInt(data.tid, 10) === parseInt(tid, 10)) {
|
||||
socket.emit('topics.markTopicNotificationsRead', tid);
|
||||
socket.emit('topics.markTopicNotificationsRead', [tid]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -283,7 +283,7 @@ topicsController.get = function(req, res, callback) {
|
||||
}
|
||||
if (markedRead) {
|
||||
topics.pushUnreadCount(req.uid);
|
||||
topics.markTopicNotificationsRead(tid, req.uid);
|
||||
topics.markTopicNotificationsRead([tid], req.uid);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -19,18 +19,17 @@ module.exports = function(SocketTopics) {
|
||||
|
||||
topics.pushUnreadCount(socket.uid);
|
||||
|
||||
for (var i=0; i<tids.length; ++i) {
|
||||
topics.markTopicNotificationsRead(tids[i], socket.uid);
|
||||
}
|
||||
topics.markTopicNotificationsRead(tids, socket.uid);
|
||||
|
||||
callback();
|
||||
});
|
||||
};
|
||||
|
||||
SocketTopics.markTopicNotificationsRead = function(socket, tid, callback) {
|
||||
if (!tid || !socket.uid) {
|
||||
SocketTopics.markTopicNotificationsRead = function(socket, tids, callback) {
|
||||
if (!Array.isArray(tids) || !socket.uid) {
|
||||
return callback(new Error('[[error:invalid-data]]'));
|
||||
}
|
||||
topics.markTopicNotificationsRead(tid, socket.uid);
|
||||
topics.markTopicNotificationsRead(tids, socket.uid);
|
||||
};
|
||||
|
||||
SocketTopics.markAllRead = function(socket, data, callback) {
|
||||
|
||||
@@ -279,9 +279,7 @@ module.exports = function(Topics) {
|
||||
db.getSortedSetRevRangeByScore('topics:recent', 0, -1, '+inf', Topics.unreadCutoff(), next);
|
||||
},
|
||||
function (tids, next) {
|
||||
for (var i=0; i<tids.length; ++i) {
|
||||
Topics.markTopicNotificationsRead(tids[i], uid);
|
||||
}
|
||||
Topics.markTopicNotificationsRead(tids, uid);
|
||||
Topics.markAsRead(tids, uid, next);
|
||||
},
|
||||
function (markedRead, next) {
|
||||
@@ -290,18 +288,24 @@ module.exports = function(Topics) {
|
||||
], callback);
|
||||
};
|
||||
|
||||
Topics.markTopicNotificationsRead = function(tid, uid) {
|
||||
if (!tid) {
|
||||
Topics.markTopicNotificationsRead = function(tids, uid) {
|
||||
if (!Array.isArray(tids) || !tids.length) {
|
||||
return;
|
||||
}
|
||||
user.notifications.getUnreadByField(uid, 'tid', tid, function(err, nids) {
|
||||
if (err) {
|
||||
return winston.error(err.stack);
|
||||
|
||||
async.waterfall([
|
||||
function(next) {
|
||||
user.notifications.getUnreadByField(uid, 'tid', tids, next);
|
||||
},
|
||||
function(nids, next) {
|
||||
notifications.markReadMultiple(nids, uid, next);
|
||||
}
|
||||
], function(err) {
|
||||
if (err) {
|
||||
return winston.error(err);
|
||||
}
|
||||
notifications.markReadMultiple(nids, uid, function() {
|
||||
user.notifications.pushCount(uid);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
Topics.markCategoryUnreadForAll = function(tid, callback) {
|
||||
|
||||
@@ -154,7 +154,7 @@ var privileges = require('../privileges');
|
||||
});
|
||||
};
|
||||
|
||||
UserNotifications.getUnreadByField = function(uid, field, value, callback) {
|
||||
UserNotifications.getUnreadByField = function(uid, field, values, callback) {
|
||||
db.getSortedSetRevRange('uid:' + uid + ':notifications:unread', 0, 99, function(err, nids) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
@@ -173,9 +173,9 @@ var privileges = require('../privileges');
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
value = value ? value.toString() : '';
|
||||
values = values.map(function() { return values.toString(); });
|
||||
nids = notifications.filter(function(notification) {
|
||||
return notification && notification[field] && notification[field].toString() === value;
|
||||
return notification && notification[field] && values.indexOf(notification[field].toString()) !== -1;
|
||||
}).map(function(notification) {
|
||||
return notification.nid;
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user