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
v1.18.x
barisusakli 10 years ago
parent 0cb0dafae3
commit 2c7d9e1a7c

@ -104,7 +104,7 @@ define('forum/topic', [
function getPostIndex() { function getPostIndex() {
var parts = window.location.pathname.split('/'); 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() { function handleSorting() {

@ -167,6 +167,9 @@ module.exports = function(Posts) {
function (next) { function (next) {
topics.decreasePostCount(postData.tid, next); topics.decreasePostCount(postData.tid, next);
}, },
function(next) {
topics.updateTeaser(postData.tid, next);
},
function(next) { function(next) {
user.incrementUserPostCountBy(postData.uid, -1, next); user.incrementUserPostCountBy(postData.uid, -1, next);
}, },

@ -23,7 +23,7 @@ module.exports = function(Topics) {
}, },
function(next) { function(next) {
Topics.addPostToTopic(postData.tid, postData.pid, postData.timestamp, 0, next); Topics.addPostToTopic(postData.tid, postData.pid, postData.timestamp, 0, next);
} },
], callback); ], callback);
}; };
@ -134,43 +134,55 @@ module.exports = function(Topics) {
}); });
}; };
Topics.getLatestUndeletedPost = function(tid, callback) { Topics.getLatestUndeletedPid = function(tid, callback) {
Topics.getLatestUndeletedPid(tid, function(err, pid) { Topics.getLatestUndeletedReply(tid, function(err, pid) {
if(err) { if (err) {
return callback(err); return callback(err);
} }
if (parseInt(pid, 10)) {
posts.getPostData(pid, callback); 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) { Topics.getLatestUndeletedReply = function(tid, callback) {
async.parallel({ var isDeleted = false;
mainPid: function(next) { var done = false;
Topics.getTopicField(tid, 'mainPid', next); var latestPid = null;
}, var index = 0;
pids: function(next) { async.doWhilst(
db.getSortedSetRevRange('tid:' + tid + ':posts', 0, -1, next); function(next) {
} db.getSortedSetRevRange('tid:' + tid + ':posts', index, index, function(err, pids) {
}, function(err, results) { if (err) {
if(err) { return next(err);
return callback(err); }
}
if (!results.mainPid && (!Array.isArray(results.pids) || !results.pids.length)) {
return callback(null, null);
}
results.pids.push(results.mainPid); if (!Array.isArray(pids) || !pids.length) {
done = true;
return next();
}
async.detectSeries(results.pids, function(pid, next) { posts.getPostField(pids[0], 'deleted', function(err, deleted) {
posts.getPostField(pid, 'deleted', function(err, deleted) { if (err) {
next(parseInt(deleted, 10) === 0); 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) { Topics.addPostToTopic = function(tid, pid, timestamp, votes, callback) {
@ -188,7 +200,12 @@ module.exports = function(Topics) {
function(next) { function(next) {
db.sortedSetAdd('tid:' + tid + ':posts:votes', votes, pid, 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); db.sortedSetRemove('tid:' + tid + ':posts:votes', pid, next);
} }
], function(err, results) { ], 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, []); return callback(null, []);
} }
async.parallel({ Topics.getTopicsFields(tids, ['postcount', 'teaserPid'], function(err, topics) {
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) {
if (err) { if (err) {
return callback(err); return callback(err);
} }
var counts = [];
var teaserPids = [];
var counts = results.topics.map(function(topic) { topics.forEach(function(topic) {
return topic && (parseInt(topic.postcount, 10) || 0); counts.push(topic && (parseInt(topic.postcount, 10) || 0));
if (topic && topic.teaserPid) {
teaserPids.push(topic.teaserPid);
}
}); });
results.pids = results.pids.filter(Boolean); posts.getPostsFields(teaserPids, ['pid', 'uid', 'timestamp', 'tid'], function(err, postData) {
posts.getPostsFields(results.pids, ['pid', 'uid', 'timestamp', 'tid'], function(err, postData) {
if (err) { if (err) {
return callback(err); return callback(err);
} }
@ -81,42 +73,18 @@ module.exports = function(Topics) {
}; };
Topics.getTeaser = function(tid, callback) { Topics.getTeaser = function(tid, callback) {
Topics.getLatestUndeletedPid(tid, function(err, pid) { Topics.getTeasers([tid], function(err, teasers) {
if (err || !pid) { 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); return callback(err);
} }
var pid = Array.isArray(pids) && pids.length ? pids[0] : null;
async.parallel({ Topics.setTopicField(tid, 'teaserPid', pid, callback);
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);
});
}); });
}; };
}; };

@ -21,7 +21,7 @@ var db = require('./database'),
schemaDate, thisSchemaDate, schemaDate, thisSchemaDate,
// IMPORTANT: REMEMBER TO UPDATE VALUE OF latestSchema // IMPORTANT: REMEMBER TO UPDATE VALUE OF latestSchema
latestSchema = Date.UTC(2014, 11, 2); latestSchema = Date.UTC(2014, 11, 12);
Upgrade.check = function(callback) { Upgrade.check = function(callback) {
db.get('schemaDate', function(err, value) { db.get('schemaDate', function(err, value) {
@ -389,6 +389,32 @@ Upgrade.upgrade = function(callback) {
winston.info('[2014/12/2] Removing register user fields skipped'); winston.info('[2014/12/2] Removing register user fields skipped');
next(); 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 // Add new schema updates here

Loading…
Cancel
Save