1
0
mirror of https://github.com/krawieck/lemmur/ synced 2024-12-17 10:59:29 +01:00
lemmur-app-android/lib/util/hot_rank.dart
2020-09-03 12:29:39 +02:00

22 lines
705 B
Dart

import 'dart:math' show log, max, pow, ln10;
import 'package:lemmy_api_client/lemmy_api_client.dart';
/// 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
double _calculateHotRank(int score, DateTime time) {
log10(num x) => log(x) / ln10;
final elapsed = (time.difference(DateTime.now()).inMilliseconds).abs() / 36e5;
return (10000 * log10(max(1, 3 + score))) / pow(elapsed + 2, 1.8);
}
extension CommentHotRank on CommentView {
double get computedHotRank => _calculateHotRank(score, published);
}