Order of operation fix in scoring.js

This commit is contained in:
Discordius 2017-10-19 21:28:26 -07:00
parent 7ff103f806
commit b496d01d83

View file

@ -10,17 +10,18 @@ Returns how many documents have been updated (1 or 0).
export const updateScore = ({collection, item, forceUpdate}) => { export const updateScore = ({collection, item, forceUpdate}) => {
// Age Check // Age Check
const postedAt = item && item.postedAt && item.postedAt.valueOf();
// If for some reason item doesn't have a "postedAt" property, abort
// Or, if post has been scheduled in the future, don't update its score
if (!item.postedAt || postedAt > now)
return 0;
const postedAt = item.postedAt.valueOf();
const now = new Date().getTime(); const now = new Date().getTime();
const age = now - postedAt; const age = now - postedAt;
const ageInHours = age / (60 * 60 * 1000); const ageInHours = age / (60 * 60 * 1000);
// If for some reason item doesn't have a "postedAt" property, abort
// Or, if post has been scheduled in the future, don't update its score
if (postedAt || postedAt > now)
return 0;
// For performance reasons, the database is only updated if the difference between the old score and the new score // For performance reasons, the database is only updated if the difference between the old score and the new score
// is meaningful enough. To find out, we calculate the "power" of a single vote after n days. // is meaningful enough. To find out, we calculate the "power" of a single vote after n days.
// We assume that after n days, a single vote will not be powerful enough to affect posts' ranking order. // We assume that after n days, a single vote will not be powerful enough to affect posts' ranking order.