From 9e85fd5a2d93a7d1c1cf0f3cc77b2fe16b965f00 Mon Sep 17 00:00:00 2001 From: krawieck Date: Wed, 2 Sep 2020 13:47:45 +0200 Subject: [PATCH] Add util extension for calculating hot rank of comments --- lib/util/hot_rank.dart | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 lib/util/hot_rank.dart diff --git a/lib/util/hot_rank.dart b/lib/util/hot_rank.dart new file mode 100644 index 0000000..975e664 --- /dev/null +++ b/lib/util/hot_rank.dart @@ -0,0 +1,15 @@ +// import 'dart:core' show ab; +import 'dart:math' show log, max, pow; + +import 'package:lemmy_api_client/lemmy_api_client.dart'; + +double _calculateHotRank(int score, DateTime time) { + final elapsed = (time.difference(DateTime.now()).inMilliseconds).abs() / 36e5; + + // log instead of log10 in web version. does log == log10? + return (10000 * log(max(1, 3 + score))) / pow(elapsed + 2, 1.8); +} + +extension CommentHotRank on CommentView { + double get computedHotRank => _calculateHotRank(score, published); +}