mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-01 11:35:55 +01:00
markAsRead improvement
takes in an array of tids instead of marking topics read 1 by 1 same for the category.markAsRead function
This commit is contained in:
@@ -149,7 +149,7 @@ define('forum/topic', dependencies, function(pagination, infinitescroll, threadT
|
|||||||
var postcount = $('.user_postcount_' + data.posts[i].uid);
|
var postcount = $('.user_postcount_' + data.posts[i].uid);
|
||||||
postcount.html(parseInt(postcount.html(), 10) + 1);
|
postcount.html(parseInt(postcount.html(), 10) + 1);
|
||||||
}
|
}
|
||||||
socket.emit('topics.markAsRead', tid);
|
socket.emit('topics.markAsRead', [tid]);
|
||||||
createNewPosts(data);
|
createNewPosts(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ define('forum/unread', ['forum/recent', 'topicSelect', 'forum/infinitescroll'],
|
|||||||
if(!tids.length) {
|
if(!tids.length) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
socket.emit('topics.markTidsRead', tids, function(err) {
|
socket.emit('topics.markAsRead', tids, function(err) {
|
||||||
if(err) {
|
if(err) {
|
||||||
return app.alertError(err.message);
|
return app.alertError(err.message);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -74,7 +74,7 @@ var db = require('./database'),
|
|||||||
var category = categories[0];
|
var category = categories[0];
|
||||||
|
|
||||||
if (parseInt(uid, 10)) {
|
if (parseInt(uid, 10)) {
|
||||||
Categories.markAsRead(cid, uid);
|
Categories.markAsRead([cid], uid);
|
||||||
}
|
}
|
||||||
|
|
||||||
async.parallel({
|
async.parallel({
|
||||||
@@ -250,8 +250,14 @@ var db = require('./database'),
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
Categories.markAsRead = function(cid, uid, callback) {
|
Categories.markAsRead = function(cids, uid, callback) {
|
||||||
db.setAdd('cid:' + cid + ':read_by_uid', uid, callback);
|
if (!Array.isArray(cids) || !cids.length) {
|
||||||
|
return callback();
|
||||||
|
}
|
||||||
|
var keys = cids.map(function(cid) {
|
||||||
|
return 'cid:' + cid + ':read_by_uid';
|
||||||
|
});
|
||||||
|
db.setsAdd(keys, uid, callback);
|
||||||
};
|
};
|
||||||
|
|
||||||
Categories.markAsUnreadForAll = function(cid, callback) {
|
Categories.markAsUnreadForAll = function(cid, callback) {
|
||||||
|
|||||||
@@ -19,6 +19,10 @@ module.exports = function(db, module) {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
module.setsAdd = function(keys, value, callback) {
|
||||||
|
throw new Error('not-implemented');
|
||||||
|
};
|
||||||
|
|
||||||
module.setRemove = function(key, value, callback) {
|
module.setRemove = function(key, value, callback) {
|
||||||
module.getListRange(key, 0, -1, function(err, set) {
|
module.getListRange(key, 0, -1, function(err, set) {
|
||||||
module.set(key, set.splice(set.indexOf(value), 1), callback);
|
module.set(key, set.splice(set.indexOf(value), 1), callback);
|
||||||
|
|||||||
@@ -31,6 +31,31 @@ module.exports = function(db, module) {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
module.setsAdd = function(keys, value, callback) {
|
||||||
|
callback = callback || helpers.noop;
|
||||||
|
if(!Array.isArray(value)) {
|
||||||
|
value = [value];
|
||||||
|
}
|
||||||
|
|
||||||
|
value.forEach(function(element, index, array) {
|
||||||
|
array[index] = helpers.valueToString(element);
|
||||||
|
});
|
||||||
|
|
||||||
|
var bulk = db.collection('objects').initializeUnorderedBulkOp();
|
||||||
|
|
||||||
|
for(var i=0; i<keys.length; ++i) {
|
||||||
|
bulk.find({_key: keys[i]}).upsert().updateOne({ $addToSet: {
|
||||||
|
members: {
|
||||||
|
$each: value
|
||||||
|
}
|
||||||
|
}});
|
||||||
|
}
|
||||||
|
|
||||||
|
bulk.execute(function(err) {
|
||||||
|
return callback(err);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
module.setRemove = function(key, value, callback) {
|
module.setRemove = function(key, value, callback) {
|
||||||
callback = callback || helpers.noop;
|
callback = callback || helpers.noop;
|
||||||
if(!Array.isArray(value)) {
|
if(!Array.isArray(value)) {
|
||||||
|
|||||||
@@ -8,6 +8,17 @@ module.exports = function(redisClient, module) {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
module.setsAdd = function(keys, value, callback) {
|
||||||
|
callback = callback || function() {};
|
||||||
|
var multi = redisClient.multi();
|
||||||
|
for (var i=0; i<keys.length; ++i) {
|
||||||
|
multi.sadd(keys[i], value);
|
||||||
|
}
|
||||||
|
multi.exec(function(err) {
|
||||||
|
callback(err);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
module.setRemove = function(key, value, callback) {
|
module.setRemove = function(key, value, callback) {
|
||||||
redisClient.srem(key, value, callback);
|
redisClient.srem(key, value, callback);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -61,8 +61,7 @@ SocketTopics.enter = function(socket, tid, callback) {
|
|||||||
if (!tid || !socket.uid) {
|
if (!tid || !socket.uid) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
SocketTopics.markAsRead(socket, tid);
|
SocketTopics.markAsRead(socket, [tid], callback);
|
||||||
topics.markTopicNotificationsRead(tid, socket.uid);
|
|
||||||
topics.increaseViewCount(tid);
|
topics.increaseViewCount(tid);
|
||||||
websockets.updateRoomBrowsingText('topic_' + tid);
|
websockets.updateRoomBrowsingText('topic_' + tid);
|
||||||
};
|
};
|
||||||
@@ -75,23 +74,17 @@ SocketTopics.increaseViewCount = function(socket, tid) {
|
|||||||
topics.increaseViewCount(tid);
|
topics.increaseViewCount(tid);
|
||||||
};
|
};
|
||||||
|
|
||||||
SocketTopics.markAsRead = function(socket, tid) {
|
SocketTopics.markAsRead = function(socket, tids, callback) {
|
||||||
if(!tid || !socket.uid) {
|
if(!Array.isArray(tids) || !socket.uid) {
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
topics.markAsRead(tid, socket.uid, function(err) {
|
|
||||||
topics.pushUnreadCount(socket.uid);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
SocketTopics.markTidsRead = function(socket, tids, callback) {
|
|
||||||
if (!Array.isArray(tids)) {
|
|
||||||
return callback(new Error('[[error:invalid-data]]'));
|
return callback(new Error('[[error:invalid-data]]'));
|
||||||
}
|
}
|
||||||
|
|
||||||
topics.markTidsRead(socket.uid, tids, function(err) {
|
if (!tids.length) {
|
||||||
if(err) {
|
return callback();
|
||||||
|
}
|
||||||
|
|
||||||
|
topics.markAsRead(tids, socket.uid, function(err) {
|
||||||
|
if (err) {
|
||||||
return callback(err);
|
return callback(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -100,7 +93,6 @@ SocketTopics.markTidsRead = function(socket, tids, callback) {
|
|||||||
for (var i=0; i<tids.length; ++i) {
|
for (var i=0; i<tids.length; ++i) {
|
||||||
topics.markTopicNotificationsRead(tids[i], socket.uid);
|
topics.markTopicNotificationsRead(tids[i], socket.uid);
|
||||||
}
|
}
|
||||||
|
|
||||||
callback();
|
callback();
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
@@ -118,7 +110,7 @@ SocketTopics.markAllRead = function(socket, data, callback) {
|
|||||||
return callback(err);
|
return callback(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
SocketTopics.markTidsRead(socket, tids, callback);
|
SocketTopics.markAsRead(socket, tids, callback);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -138,14 +130,13 @@ SocketTopics.markCategoryTopicsRead = function(socket, cid, callback) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
tids = topicData.filter(function(topic) {
|
tids = topicData.filter(function(topic) {
|
||||||
return parseInt(topic.cid, 10) === parseInt(cid, 10);
|
return topic && parseInt(topic.cid, 10) === parseInt(cid, 10);
|
||||||
}).map(function(topic) {
|
}).map(function(topic) {
|
||||||
return topic.tid;
|
return topic.tid;
|
||||||
});
|
});
|
||||||
|
|
||||||
SocketTopics.markTidsRead(socket, tids, callback);
|
SocketTopics.markAsRead(socket, tids, callback);
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -208,7 +208,7 @@ module.exports = function(Topics) {
|
|||||||
Topics.markAsUnreadForAll(tid, next);
|
Topics.markAsUnreadForAll(tid, next);
|
||||||
},
|
},
|
||||||
function(next) {
|
function(next) {
|
||||||
Topics.markAsRead(tid, uid, next);
|
Topics.markAsRead([tid], uid, next);
|
||||||
},
|
},
|
||||||
function(next) {
|
function(next) {
|
||||||
posts.getUserInfoForPosts([postData.uid], next);
|
posts.getUserInfoForPosts([postData.uid], next);
|
||||||
|
|||||||
@@ -162,7 +162,7 @@ module.exports = function(Topics) {
|
|||||||
} else {
|
} else {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
var websockets = require('./../socket.io');
|
var websockets = require('./../socket.io');
|
||||||
|
|
||||||
if (!uids) {
|
if (!uids) {
|
||||||
@@ -200,29 +200,35 @@ module.exports = function(Topics) {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
Topics.markTidsRead = function(uid, tids, callback) {
|
Topics.markAsRead = function(tids, uid, callback) {
|
||||||
if(!tids || !tids.length) {
|
callback = callback || function() {};
|
||||||
|
if (!Array.isArray(tids) || !tids.length) {
|
||||||
return callback();
|
return callback();
|
||||||
}
|
}
|
||||||
|
tids = tids.filter(Boolean);
|
||||||
|
var keys = tids.map(function(tid) {
|
||||||
|
return 'tid:' + tid + ':read_by_uid';
|
||||||
|
});
|
||||||
|
|
||||||
async.each(tids, function (tid, next) {
|
async.parallel({
|
||||||
Topics.markAsRead(tid, uid, next);
|
markRead: function(next) {
|
||||||
}, callback);
|
db.setsAdd(keys, uid, next);
|
||||||
};
|
},
|
||||||
|
topicData: function(next) {
|
||||||
Topics.markAsRead = function(tid, uid, callback) {
|
Topics.getTopicsFields(tids, ['cid'], next);
|
||||||
db.setAdd('tid:' + tid + ':read_by_uid', uid, function(err) {
|
}
|
||||||
|
}, function(err, results) {
|
||||||
if (err) {
|
if (err) {
|
||||||
return callback(err);
|
return callback(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
Topics.getTopicField(tid, 'cid', function(err, cid) {
|
var cids = results.topicData.map(function(topic) {
|
||||||
if (err) {
|
return topic && topic.cid;
|
||||||
return callback(err);
|
}).filter(function(topic, index, array) {
|
||||||
}
|
return topic && array.indexOf(topic) === index;
|
||||||
|
|
||||||
categories.markAsRead(cid, uid, callback);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
categories.markAsRead(cids, uid, callback);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user