about summary refs log tree commit diff
path: root/app/models
diff options
context:
space:
mode:
authorDavid Yip <yipdw@member.fsf.org>2018-06-02 16:15:36 -0500
committerDavid Yip <yipdw@member.fsf.org>2018-06-02 16:15:36 -0500
commit3550470c18425e80a322e6fe56245c9ede3d0689 (patch)
tree10c7539e0bf80a9a5d20aa2c8acf227803d3789b /app/models
parenta641d1b5b8e9f20104ab16e5419e6dee4e5af37f (diff)
parent00512ecf87e1098f5632eeec2cd116344a787523 (diff)
Merge remote-tracking branch 'origin/master' into gs-master
  Conflicts:
 	app/javascript/mastodon/locales/en.json
 	app/javascript/mastodon/locales/ja.json
 	app/javascript/mastodon/locales/pl.json

The above conflicts appear to be a text conflict introduced by
glitch-soc's additional level of columns (i.e. moving a bunch of columns
under the Misc option).  They were resolved via accept-ours.
Diffstat (limited to 'app/models')
-rw-r--r--app/models/trending_tags.rb25
1 files changed, 13 insertions, 12 deletions
diff --git a/app/models/trending_tags.rb b/app/models/trending_tags.rb
index eedd92644..287de2a8a 100644
--- a/app/models/trending_tags.rb
+++ b/app/models/trending_tags.rb
@@ -2,17 +2,16 @@
 
 class TrendingTags
   KEY                  = 'trending_tags'
-  HALF_LIFE            = 1.day.to_i
-  MAX_ITEMS            = 500
   EXPIRE_HISTORY_AFTER = 7.days.seconds
+  THRESHOLD            = 5
 
   class << self
     def record_use!(tag, account, at_time = Time.now.utc)
-      return if disallowed_hashtags.include?(tag.name) || account.silenced?
+      return if disallowed_hashtags.include?(tag.name) || account.silenced? || account.bot?
 
-      increment_vote!(tag.id, at_time)
       increment_historical_use!(tag.id, at_time)
       increment_unique_use!(tag.id, account.id, at_time)
+      increment_vote!(tag.id, at_time)
     end
 
     def get(limit)
@@ -24,8 +23,16 @@ class TrendingTags
     private
 
     def increment_vote!(tag_id, at_time)
-      redis.zincrby(KEY, (2**((at_time.to_i - epoch) / HALF_LIFE)).to_f, tag_id.to_s)
-      redis.zremrangebyrank(KEY, 0, -MAX_ITEMS) if rand < (2.to_f / MAX_ITEMS)
+      expected = redis.pfcount("activity:tags:#{tag_id}:#{(at_time - 1.day).beginning_of_day.to_i}:accounts").to_f
+      expected = 1.0 if expected.zero?
+      observed = redis.pfcount("activity:tags:#{tag_id}:#{at_time.beginning_of_day.to_i}:accounts").to_f
+
+      if expected > observed || observed < THRESHOLD
+        redis.zrem(KEY, tag_id.to_s)
+      else
+        score = ((observed - expected)**2) / expected
+        redis.zadd(KEY, score, tag_id.to_s)
+      end
     end
 
     def increment_historical_use!(tag_id, at_time)
@@ -40,12 +47,6 @@ class TrendingTags
       redis.expire(key, EXPIRE_HISTORY_AFTER)
     end
 
-    # The epoch needs to be 2.5 years in the future if the half-life is one day
-    # While dynamic, it will always be the same within one year
-    def epoch
-      @epoch ||= Date.new(Date.current.year + 2.5, 10, 1).to_datetime.to_i
-    end
-
     def disallowed_hashtags
       return @disallowed_hashtags if defined?(@disallowed_hashtags)