mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-12-21 16:00:26 +01:00
Bookmark optimization (#6315)
* Set the user's bookmark if their current bookmark is past the end of the topic. * Optimize forked topic bookmark updating. Remove support for updating bookmarks for users who sort by votes. Don't even consider updating bookmarks for users who have not read the posts being removed. Only compute post indices once per fork operation instead of once per user that has ever read the topic.
This commit is contained in:
committed by
Barış Soner Uşaklı
parent
7f9d9b7654
commit
00776bdd8e
@@ -231,7 +231,7 @@ define('forum/topic', [
|
|||||||
var bookmarkKey = 'topic:' + ajaxify.data.tid + ':bookmark';
|
var bookmarkKey = 'topic:' + ajaxify.data.tid + ':bookmark';
|
||||||
var currentBookmark = ajaxify.data.bookmark || storage.getItem(bookmarkKey);
|
var currentBookmark = ajaxify.data.bookmark || storage.getItem(bookmarkKey);
|
||||||
|
|
||||||
if (ajaxify.data.postcount > ajaxify.data.bookmarkThreshold && (!currentBookmark || parseInt(index, 10) > parseInt(currentBookmark, 10))) {
|
if (ajaxify.data.postcount > ajaxify.data.bookmarkThreshold && (!currentBookmark || parseInt(index, 10) > parseInt(currentBookmark, 10) || ajaxify.data.postcount < parseInt(currentBookmark, 10))) {
|
||||||
if (app.user.uid) {
|
if (app.user.uid) {
|
||||||
socket.emit('topics.bookmark', {
|
socket.emit('topics.bookmark', {
|
||||||
tid: ajaxify.data.tid,
|
tid: ajaxify.data.tid,
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
var async = require('async');
|
var async = require('async');
|
||||||
|
|
||||||
var db = require('../database');
|
var db = require('../database');
|
||||||
var posts = require('../posts');
|
var user = require('../user');
|
||||||
|
|
||||||
module.exports = function (Topics) {
|
module.exports = function (Topics) {
|
||||||
Topics.getUserBookmark = function (tid, uid, callback) {
|
Topics.getUserBookmark = function (tid, uid, callback) {
|
||||||
@@ -34,7 +34,9 @@ module.exports = function (Topics) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
Topics.updateTopicBookmarks = function (tid, pids, callback) {
|
Topics.updateTopicBookmarks = function (tid, pids, callback) {
|
||||||
|
var minIndex;
|
||||||
var maxIndex;
|
var maxIndex;
|
||||||
|
var postIndices;
|
||||||
|
|
||||||
async.waterfall([
|
async.waterfall([
|
||||||
function (next) {
|
function (next) {
|
||||||
@@ -42,38 +44,54 @@ module.exports = function (Topics) {
|
|||||||
},
|
},
|
||||||
function (postcount, next) {
|
function (postcount, next) {
|
||||||
maxIndex = postcount;
|
maxIndex = postcount;
|
||||||
|
|
||||||
|
db.sortedSetRanks('tid:' + tid + ':posts', pids, next);
|
||||||
|
},
|
||||||
|
function (indices, next) {
|
||||||
|
postIndices = indices.map(function (i) {
|
||||||
|
return i === null ? 0 : i + 1;
|
||||||
|
});
|
||||||
|
minIndex = Math.min.apply(Math, postIndices);
|
||||||
|
|
||||||
Topics.getTopicBookmarks(tid, next);
|
Topics.getTopicBookmarks(tid, next);
|
||||||
},
|
},
|
||||||
function (bookmarks, next) {
|
function (bookmarks, next) {
|
||||||
var forkedPosts = pids.map(function (pid) {
|
|
||||||
return { pid: pid, tid: tid };
|
|
||||||
});
|
|
||||||
|
|
||||||
var uidData = bookmarks.map(function (bookmark) {
|
var uidData = bookmarks.map(function (bookmark) {
|
||||||
return {
|
return {
|
||||||
uid: bookmark.value,
|
uid: bookmark.value,
|
||||||
bookmark: bookmark.score,
|
bookmark: parseInt(bookmark.score, 10),
|
||||||
};
|
};
|
||||||
|
}).filter(function (data) {
|
||||||
|
return data.bookmark >= minIndex;
|
||||||
});
|
});
|
||||||
|
|
||||||
async.eachLimit(uidData, 50, function (data, next) {
|
async.eachLimit(uidData, 50, function (data, next) {
|
||||||
posts.getPostIndices(forkedPosts, data.uid, function (err, postIndices) {
|
var bookmark = data.bookmark;
|
||||||
|
bookmark = Math.min(bookmark, maxIndex);
|
||||||
|
|
||||||
|
postIndices.forEach(function (i) {
|
||||||
|
if (i < data.bookmark) {
|
||||||
|
bookmark -= 1;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// make sure the bookmark is valid if we removed the last post
|
||||||
|
bookmark = Math.min(bookmark, maxIndex - pids.length);
|
||||||
|
|
||||||
|
if (bookmark === data.bookmark) {
|
||||||
|
return next();
|
||||||
|
}
|
||||||
|
|
||||||
|
user.getSettings(data.uid, function (err, settings) {
|
||||||
if (err) {
|
if (err) {
|
||||||
return next(err);
|
return next(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
var bookmark = data.bookmark;
|
if (settings.topicPostSort === 'most_votes') {
|
||||||
bookmark = bookmark < maxIndex ? bookmark : maxIndex;
|
return next();
|
||||||
|
|
||||||
for (var i = 0; i < postIndices.length && postIndices[i] < data.bookmark; i += 1) {
|
|
||||||
bookmark -= 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (parseInt(bookmark, 10) !== parseInt(data.bookmark, 10)) {
|
|
||||||
Topics.setUserBookmark(tid, data.uid, bookmark, next);
|
Topics.setUserBookmark(tid, data.uid, bookmark, next);
|
||||||
} else {
|
|
||||||
next();
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}, next);
|
}, next);
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user