2020-09-03 12:23:46 +02:00
|
|
|
import 'dart:math' show log, max, pow, ln10;
|
2020-09-02 13:47:45 +02:00
|
|
|
|
|
|
|
import 'package:lemmy_api_client/lemmy_api_client.dart';
|
|
|
|
|
2020-09-03 12:29:39 +02:00
|
|
|
/// Calculates hot rank
|
|
|
|
/// because API always claims it's `0`
|
|
|
|
/// and web version of lemmy also calculates it when loading comments
|
|
|
|
///
|
|
|
|
/// implementation taken from here:
|
|
|
|
/// https://github.com/LemmyNet/lemmy/blob/main/ui/src/utils.ts#L182-L203
|
2020-09-02 13:47:45 +02:00
|
|
|
double _calculateHotRank(int score, DateTime time) {
|
2020-09-03 12:23:46 +02:00
|
|
|
log10(num x) => log(x) / ln10;
|
|
|
|
|
2020-09-02 13:47:45 +02:00
|
|
|
final elapsed = (time.difference(DateTime.now()).inMilliseconds).abs() / 36e5;
|
|
|
|
|
2020-09-03 12:23:46 +02:00
|
|
|
return (10000 * log10(max(1, 3 + score))) / pow(elapsed + 2, 1.8);
|
2020-09-02 13:47:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
extension CommentHotRank on CommentView {
|
|
|
|
double get computedHotRank => _calculateHotRank(score, published);
|
|
|
|
}
|