Skip to content

Commit

Permalink
cap max creation time used in ranking calculation (#630)
Browse files Browse the repository at this point in the history
added a cap on creation timestamp used in ranking calculation to not
exceeds the current time while performing calculation to cope with
posts' that have funny created date far into the future, also cap the
final score value to not exceed int32 max size (2**31 - 1)

the current ranking function involves using the posts' object creation
timestamp as part of the calculation, but if the datetime is too far in
the future (e.g. 4200-06-09, as seen in [this example][ex1]), it can
cause the ranking score value to exceeds the ranking score field size,
which appears to be int32 sized

technically we'll still be running into year 2038 problem on this field,
but there's some discussion on adjusting the ranking score and keep
that below int32 sized altogether, so that's left for the future exercise

also see #622 for more context

[ex1]: https://myfriendsare.gay/notice/APx5K7aeglknMfm1A0
  • Loading branch information
asdfzdfj committed Mar 25, 2024
1 parent 6e899c7 commit b0a3abf
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/Entity/Traits/RankingTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@ public function updateRanking(): void

$advantage = max(min($scoreAdvantage + $commentAdvantage, self::MAX_ADVANTAGE), -self::MAX_PENALTY);

$this->ranking = $this->getCreatedAt()->getTimestamp() + $advantage;
// cap max date advantage at the time of calculation to cope with posts
// that have funny dates (e.g. 4200-06-09)
// which can cause int overflow (int32?) on ranking score
$dateAdvantage = min($this->getCreatedAt()->getTimestamp(), (new \DateTimeImmutable())->getTimestamp());

// also cap the final score to not exceed int32 size for the time being
$this->ranking = min($dateAdvantage + $advantage, 2 ** 31 - 1);
}

public function getRanking(): int
Expand Down

0 comments on commit b0a3abf

Please sign in to comment.