mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-11 16:35:47 +01:00
store teaserPid in topic
-update teaser pid as necessary in addPostToTopic, removePostFromTopic, post purge -removed 20x db calls from getTeasers -fixed scroll to post in sub folder install -upgrade script to update topics with teaserPid
This commit is contained in:
@@ -104,7 +104,7 @@ define('forum/topic', [
|
||||
|
||||
function getPostIndex() {
|
||||
var parts = window.location.pathname.split('/');
|
||||
return parts[4] ? parseInt(parts[4], 10) : 0;
|
||||
return parts[parts.length - 1] ? parseInt(parts[parts.length - 1], 10) : 0;
|
||||
}
|
||||
|
||||
function handleSorting() {
|
||||
|
||||
@@ -167,6 +167,9 @@ module.exports = function(Posts) {
|
||||
function (next) {
|
||||
topics.decreasePostCount(postData.tid, next);
|
||||
},
|
||||
function(next) {
|
||||
topics.updateTeaser(postData.tid, next);
|
||||
},
|
||||
function(next) {
|
||||
user.incrementUserPostCountBy(postData.uid, -1, next);
|
||||
},
|
||||
|
||||
@@ -23,7 +23,7 @@ module.exports = function(Topics) {
|
||||
},
|
||||
function(next) {
|
||||
Topics.addPostToTopic(postData.tid, postData.pid, postData.timestamp, 0, next);
|
||||
}
|
||||
},
|
||||
], callback);
|
||||
};
|
||||
|
||||
@@ -134,43 +134,55 @@ module.exports = function(Topics) {
|
||||
});
|
||||
};
|
||||
|
||||
Topics.getLatestUndeletedPost = function(tid, callback) {
|
||||
Topics.getLatestUndeletedPid(tid, function(err, pid) {
|
||||
if(err) {
|
||||
Topics.getLatestUndeletedPid = function(tid, callback) {
|
||||
Topics.getLatestUndeletedReply(tid, function(err, pid) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
posts.getPostData(pid, callback);
|
||||
if (parseInt(pid, 10)) {
|
||||
return callback(null, pid.toString());
|
||||
}
|
||||
Topics.getTopicField(tid, 'mainPid', function(err, mainPid) {
|
||||
callback(err, parseInt(mainPid, 10) ? mainPid.toString() : null);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
Topics.getLatestUndeletedPid = function(tid, callback) {
|
||||
async.parallel({
|
||||
mainPid: function(next) {
|
||||
Topics.getTopicField(tid, 'mainPid', next);
|
||||
},
|
||||
pids: function(next) {
|
||||
db.getSortedSetRevRange('tid:' + tid + ':posts', 0, -1, next);
|
||||
}
|
||||
}, function(err, results) {
|
||||
if(err) {
|
||||
return callback(err);
|
||||
}
|
||||
Topics.getLatestUndeletedReply = function(tid, callback) {
|
||||
var isDeleted = false;
|
||||
var done = false;
|
||||
var latestPid = null;
|
||||
var index = 0;
|
||||
async.doWhilst(
|
||||
function(next) {
|
||||
db.getSortedSetRevRange('tid:' + tid + ':posts', index, index, function(err, pids) {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
|
||||
if (!results.mainPid && (!Array.isArray(results.pids) || !results.pids.length)) {
|
||||
return callback(null, null);
|
||||
}
|
||||
if (!Array.isArray(pids) || !pids.length) {
|
||||
done = true;
|
||||
return next();
|
||||
}
|
||||
|
||||
results.pids.push(results.mainPid);
|
||||
|
||||
async.detectSeries(results.pids, function(pid, next) {
|
||||
posts.getPostField(pid, 'deleted', function(err, deleted) {
|
||||
next(parseInt(deleted, 10) === 0);
|
||||
posts.getPostField(pids[0], 'deleted', function(err, deleted) {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
latestPid = pids[0];
|
||||
isDeleted = deleted;
|
||||
++index;
|
||||
next();
|
||||
});
|
||||
});
|
||||
}, function(pid) {
|
||||
callback(null, pid ? pid.toString() : null);
|
||||
});
|
||||
});
|
||||
},
|
||||
function() {
|
||||
return isDeleted && !done;
|
||||
},
|
||||
function(err) {
|
||||
callback(err, latestPid);
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
Topics.addPostToTopic = function(tid, pid, timestamp, votes, callback) {
|
||||
@@ -188,7 +200,12 @@ module.exports = function(Topics) {
|
||||
function(next) {
|
||||
db.sortedSetAdd('tid:' + tid + ':posts:votes', votes, pid, next);
|
||||
}
|
||||
], callback);
|
||||
], function(err) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
Topics.updateTeaser(tid, callback);
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
@@ -202,7 +219,10 @@ module.exports = function(Topics) {
|
||||
db.sortedSetRemove('tid:' + tid + ':posts:votes', pid, next);
|
||||
}
|
||||
], function(err, results) {
|
||||
callback(err);
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
Topics.updateTeaser(tid, callback);
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
@@ -17,29 +17,21 @@ module.exports = function(Topics) {
|
||||
return callback(null, []);
|
||||
}
|
||||
|
||||
async.parallel({
|
||||
topics: function(next) {
|
||||
Topics.getTopicsFields(tids, ['postcount'], next);
|
||||
},
|
||||
pids: function(next) {
|
||||
async.map(tids, function(tid, next) {
|
||||
db.getSortedSetRevRange('tid:' + tid + ':posts', 0, 0, function(err, data) {
|
||||
next(err, Array.isArray(data) && data.length ? data[0] : null);
|
||||
});
|
||||
}, next);
|
||||
}
|
||||
}, function(err, results) {
|
||||
Topics.getTopicsFields(tids, ['postcount', 'teaserPid'], function(err, topics) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
var counts = [];
|
||||
var teaserPids = [];
|
||||
|
||||
var counts = results.topics.map(function(topic) {
|
||||
return topic && (parseInt(topic.postcount, 10) || 0);
|
||||
topics.forEach(function(topic) {
|
||||
counts.push(topic && (parseInt(topic.postcount, 10) || 0));
|
||||
if (topic && topic.teaserPid) {
|
||||
teaserPids.push(topic.teaserPid);
|
||||
}
|
||||
});
|
||||
|
||||
results.pids = results.pids.filter(Boolean);
|
||||
|
||||
posts.getPostsFields(results.pids, ['pid', 'uid', 'timestamp', 'tid'], function(err, postData) {
|
||||
posts.getPostsFields(teaserPids, ['pid', 'uid', 'timestamp', 'tid'], function(err, postData) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
@@ -81,42 +73,18 @@ module.exports = function(Topics) {
|
||||
};
|
||||
|
||||
Topics.getTeaser = function(tid, callback) {
|
||||
Topics.getLatestUndeletedPid(tid, function(err, pid) {
|
||||
if (err || !pid) {
|
||||
Topics.getTeasers([tid], function(err, teasers) {
|
||||
callback(err, Array.isArray(teasers) && teasers.length ? teasers[0] : null);
|
||||
});
|
||||
};
|
||||
|
||||
Topics.updateTeaser = function(tid, callback) {
|
||||
db.getSortedSetRevRange('tid:' + tid + ':posts', 0, 0, function(err, pids) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
async.parallel({
|
||||
postData: function(next) {
|
||||
posts.getPostFields(pid, ['pid', 'uid', 'timestamp'], function(err, postData) {
|
||||
if (err) {
|
||||
return next(err);
|
||||
} else if(!postData || !utils.isNumber(postData.uid)) {
|
||||
return callback();
|
||||
}
|
||||
|
||||
user.getUserFields(postData.uid, ['username', 'userslug', 'picture'], function(err, userData) {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
postData.user = userData;
|
||||
next(null, postData);
|
||||
});
|
||||
});
|
||||
},
|
||||
postCount: function(next) {
|
||||
Topics.getTopicField(tid, 'post_count', next);
|
||||
}
|
||||
}, function(err, results) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
results.postData.timestamp = utils.toISOString(results.postData.timestamp);
|
||||
results.postData.index = results.postCount;
|
||||
|
||||
callback(null, results.postData);
|
||||
});
|
||||
var pid = Array.isArray(pids) && pids.length ? pids[0] : null;
|
||||
Topics.setTopicField(tid, 'teaserPid', pid, callback);
|
||||
});
|
||||
};
|
||||
};
|
||||
@@ -21,7 +21,7 @@ var db = require('./database'),
|
||||
schemaDate, thisSchemaDate,
|
||||
|
||||
// IMPORTANT: REMEMBER TO UPDATE VALUE OF latestSchema
|
||||
latestSchema = Date.UTC(2014, 11, 2);
|
||||
latestSchema = Date.UTC(2014, 11, 12);
|
||||
|
||||
Upgrade.check = function(callback) {
|
||||
db.get('schemaDate', function(err, value) {
|
||||
@@ -389,6 +389,32 @@ Upgrade.upgrade = function(callback) {
|
||||
winston.info('[2014/12/2] Removing register user fields skipped');
|
||||
next();
|
||||
}
|
||||
},
|
||||
function(next) {
|
||||
thisSchemaDate = Date.UTC(2014, 11, 12);
|
||||
if (schemaDate < thisSchemaDate) {
|
||||
winston.info('[2014/12/12] Updating teasers');
|
||||
|
||||
db.getSortedSetRange('topics:tid', 0, -1, function(err, tids) {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
|
||||
async.eachLimit(tids, 50, function(tid, next) {
|
||||
Topics.updateTeaser(tid, next);
|
||||
}, function(err) {
|
||||
if (err) {
|
||||
winston.error('[2014/12/12] Error encountered while updating teasers');
|
||||
return next(err);
|
||||
}
|
||||
winston.info('[2014/12/12] Updating teasers done');
|
||||
Upgrade.update(thisSchemaDate, next);
|
||||
});
|
||||
});
|
||||
} else {
|
||||
winston.info('[2014/12/12] Updating teasers skipped skipped');
|
||||
next();
|
||||
}
|
||||
}
|
||||
|
||||
// Add new schema updates here
|
||||
|
||||
Reference in New Issue
Block a user