Change trending hashtags to not disappear instantly after midnight (#11712)
parent
ac6935b629
commit
70ddef2654
@ -0,0 +1,11 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class Scheduler::TrendingTagsScheduler
|
||||
include Sidekiq::Worker
|
||||
|
||||
sidekiq_options unique: :until_executed, retry: 0
|
||||
|
||||
def perform
|
||||
TrendingTags.update! if Setting.trends
|
||||
end
|
||||
end
|
@ -0,0 +1,6 @@
|
||||
class AddMaxScoreToTags < ActiveRecord::Migration[5.2]
|
||||
def change
|
||||
add_column :tags, :max_score, :float
|
||||
add_column :tags, :max_score_at, :datetime
|
||||
end
|
||||
end
|
@ -0,0 +1,12 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class RemoveScoreFromTags < ActiveRecord::Migration[5.2]
|
||||
disable_ddl_transaction!
|
||||
|
||||
def change
|
||||
safety_assured do
|
||||
remove_column :tags, :score, :int
|
||||
remove_column :tags, :last_trend_at, :datetime
|
||||
end
|
||||
end
|
||||
end
|
@ -0,0 +1,68 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe TrendingTags do
|
||||
describe '.record_use!' do
|
||||
pending
|
||||
end
|
||||
|
||||
describe '.update!' do
|
||||
let!(:at_time) { Time.now.utc }
|
||||
let!(:tag1) { Fabricate(:tag, name: 'Catstodon') }
|
||||
let!(:tag2) { Fabricate(:tag, name: 'DogsOfMastodon') }
|
||||
let!(:tag3) { Fabricate(:tag, name: 'OCs') }
|
||||
|
||||
before do
|
||||
allow(Redis.current).to receive(:pfcount) do |key|
|
||||
case key
|
||||
when "activity:tags:#{tag1.id}:#{(at_time - 1.day).beginning_of_day.to_i}:accounts"
|
||||
2
|
||||
when "activity:tags:#{tag1.id}:#{at_time.beginning_of_day.to_i}:accounts"
|
||||
16
|
||||
when "activity:tags:#{tag2.id}:#{(at_time - 1.day).beginning_of_day.to_i}:accounts"
|
||||
0
|
||||
when "activity:tags:#{tag2.id}:#{at_time.beginning_of_day.to_i}:accounts"
|
||||
4
|
||||
when "activity:tags:#{tag3.id}:#{(at_time - 1.day).beginning_of_day.to_i}:accounts"
|
||||
13
|
||||
end
|
||||
end
|
||||
|
||||
Redis.current.zadd('trending_tags', 0.9, tag3.id)
|
||||
Redis.current.sadd("trending_tags:used:#{at_time.beginning_of_day.to_i}", [tag1.id, tag2.id])
|
||||
|
||||
tag3.update(max_score: 0.9, max_score_at: (at_time - 1.day).beginning_of_day + 12.hours)
|
||||
|
||||
described_class.update!(at_time)
|
||||
end
|
||||
|
||||
it 'calculates and re-calculates scores' do
|
||||
expect(described_class.get(10, filtered: false)).to eq [tag1, tag3]
|
||||
end
|
||||
|
||||
it 'omits hashtags below threshold' do
|
||||
expect(described_class.get(10, filtered: false)).to_not include(tag2)
|
||||
end
|
||||
|
||||
it 'decays scores' do
|
||||
expect(Redis.current.zscore('trending_tags', tag3.id)).to be < 0.9
|
||||
end
|
||||
end
|
||||
|
||||
describe '.trending?' do
|
||||
let(:tag) { Fabricate(:tag) }
|
||||
|
||||
before do
|
||||
10.times { |i| Redis.current.zadd('trending_tags', i + 1, Fabricate(:tag).id) }
|
||||
end
|
||||
|
||||
it 'returns true if the hashtag is within limit' do
|
||||
Redis.current.zadd('trending_tags', 11, tag.id)
|
||||
expect(described_class.trending?(tag)).to be true
|
||||
end
|
||||
|
||||
it 'returns false if the hashtag is outside the limit' do
|
||||
Redis.current.zadd('trending_tags', 0, tag.id)
|
||||
expect(described_class.trending?(tag)).to be false
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in new issue