Skip to content

Commit

Permalink
Create Redisable#redis (mastodon#9633)
Browse files Browse the repository at this point in the history
* Create Redisable

* Use #redis instead of Redis.current
  • Loading branch information
ysksn authored and Gargron committed Feb 2, 2019
1 parent 70b075c commit 5b0f970
Show file tree
Hide file tree
Showing 13 changed files with 39 additions and 59 deletions.
6 changes: 2 additions & 4 deletions app/lib/activity_tracker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ class ActivityTracker
EXPIRE_AFTER = 90.days.seconds

class << self
include Redisable

def increment(prefix)
key = [prefix, current_week].join(':')

Expand All @@ -20,10 +22,6 @@ def record(prefix, value)

private

def redis
Redis.current
end

def current_week
Time.zone.today.cweek
end
Expand Down
5 changes: 1 addition & 4 deletions app/lib/activitypub/activity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

class ActivityPub::Activity
include JsonLdHelper
include Redisable

def initialize(json, account, **options)
@json = json
Expand Down Expand Up @@ -70,10 +71,6 @@ def object_uri
@object_uri ||= value_or_id(@object)
end

def redis
Redis.current
end

def distribute(status)
crawl_links(status)

Expand Down
9 changes: 3 additions & 6 deletions app/lib/feed_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

class FeedManager
include Singleton
include Redisable

MAX_ITEMS = 400

Expand Down Expand Up @@ -35,7 +36,7 @@ def push_to_home(account, status)

def unpush_from_home(account, status)
return false unless remove_from_feed(:home, account.id, status)
Redis.current.publish("timeline:#{account.id}", Oj.dump(event: :delete, payload: status.id.to_s))
redis.publish("timeline:#{account.id}", Oj.dump(event: :delete, payload: status.id.to_s))
true
end

Expand All @@ -53,7 +54,7 @@ def push_to_list(list, status)

def unpush_from_list(list, status)
return false unless remove_from_feed(:list, list.id, status)
Redis.current.publish("timeline:list:#{list.id}", Oj.dump(event: :delete, payload: status.id.to_s))
redis.publish("timeline:list:#{list.id}", Oj.dump(event: :delete, payload: status.id.to_s))
true
end

Expand Down Expand Up @@ -142,10 +143,6 @@ def populate_feed(account)

private

def redis
Redis.current
end

def push_update_required?(timeline_id)
redis.exists("subscribed:#{timeline_id}")
end
Expand Down
6 changes: 2 additions & 4 deletions app/lib/ostatus/activity/base.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

class OStatus::Activity::Base
include Redisable

def initialize(xml, account = nil, **options)
@xml = xml
@account = account
Expand Down Expand Up @@ -66,8 +68,4 @@ def find_activitypub_status(uri, href)
Status.find_by(uri: uri)
end
end

def redis
Redis.current
end
end
8 changes: 2 additions & 6 deletions app/lib/potential_friendship_tracker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class PotentialFriendshipTracker
}.freeze

class << self
include Redisable

def record(account_id, target_account_id, action)
return if account_id == target_account_id

Expand All @@ -31,11 +33,5 @@ def get(account_id, limit: 20, offset: 0)
return [] if account_ids.empty?
Account.searchable.where(id: account_ids)
end

private

def redis
Redis.current
end
end
end
11 changes: 11 additions & 0 deletions app/models/concerns/redisable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

module Redisable
extend ActiveSupport::Concern

private

def redis
Redis.current
end
end
6 changes: 2 additions & 4 deletions app/models/feed.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

class Feed
include Redisable

def initialize(type, id)
@type = type
@id = id
Expand All @@ -27,8 +29,4 @@ def from_redis(limit, max_id, since_id, min_id)
def key
FeedManager.instance.key(@type, @id)
end

def redis
Redis.current
end
end
6 changes: 2 additions & 4 deletions app/models/trending_tags.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ class TrendingTags
THRESHOLD = 5

class << self
include Redisable

def record_use!(tag, account, at_time = Time.now.utc)
return if disallowed_hashtags.include?(tag.name) || account.silenced? || account.bot?

Expand Down Expand Up @@ -59,9 +61,5 @@ def disallowed_hashtags
@disallowed_hashtags = @disallowed_hashtags.split(' ') if @disallowed_hashtags.is_a? String
@disallowed_hashtags = @disallowed_hashtags.map(&:downcase)
end

def redis
Redis.current
end
end
end
5 changes: 1 addition & 4 deletions app/services/batched_remove_status_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

class BatchedRemoveStatusService < BaseService
include StreamEntryRenderer
include Redisable

# Delete given statuses and reblogs of them
# Dispatch PuSH updates of the deleted statuses, but only local ones
Expand Down Expand Up @@ -109,10 +110,6 @@ def batch_salmon_slaps(status)
end
end

def redis
Redis.current
end

def build_xml(stream_entry)
return @activity_xml[stream_entry.id] if @activity_xml.key?(stream_entry.id)

Expand Down
6 changes: 2 additions & 4 deletions app/services/follow_service.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

class FollowService < BaseService
include Redisable

# Follow a remote user, notify remote user about the follow
# @param [Account] source_account From which to follow
# @param [String, Account] uri User URI to follow in the form of username@domain (or account record)
Expand Down Expand Up @@ -67,10 +69,6 @@ def direct_follow(source_account, target_account, reblogs: true)
follow
end

def redis
Redis.current
end

def build_follow_request_xml(follow_request)
OStatus::AtomSerializer.render(OStatus::AtomSerializer.new.follow_request_salmon(follow_request))
end
Expand Down
6 changes: 2 additions & 4 deletions app/services/post_status_service.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

class PostStatusService < BaseService
include Redisable

MIN_SCHEDULE_OFFSET = 5.minutes.freeze

# Post a text status update, fetch and notify remote users mentioned
Expand Down Expand Up @@ -110,10 +112,6 @@ def process_hashtags_service
ProcessHashtagsService.new
end

def redis
Redis.current
end

def scheduled?
@scheduled_at.present?
end
Expand Down
19 changes: 8 additions & 11 deletions app/services/remove_status_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

class RemoveStatusService < BaseService
include StreamEntryRenderer
include Redisable

def call(status, **options)
@payload = Oj.dump(event: :delete, payload: status.id.to_s)
Expand Down Expand Up @@ -55,7 +56,7 @@ def remove_from_lists

def remove_from_affected
@mentions.map(&:account).select(&:local?).each do |account|
Redis.current.publish("timeline:#{account.id}", @payload)
redis.publish("timeline:#{account.id}", @payload)
end
end

Expand Down Expand Up @@ -133,26 +134,22 @@ def remove_from_hashtags
return unless @status.public_visibility?

@tags.each do |hashtag|
Redis.current.publish("timeline:hashtag:#{hashtag}", @payload)
Redis.current.publish("timeline:hashtag:#{hashtag}:local", @payload) if @status.local?
redis.publish("timeline:hashtag:#{hashtag}", @payload)
redis.publish("timeline:hashtag:#{hashtag}:local", @payload) if @status.local?
end
end

def remove_from_public
return unless @status.public_visibility?

Redis.current.publish('timeline:public', @payload)
Redis.current.publish('timeline:public:local', @payload) if @status.local?
redis.publish('timeline:public', @payload)
redis.publish('timeline:public:local', @payload) if @status.local?
end

def remove_from_media
return unless @status.public_visibility?

Redis.current.publish('timeline:public:media', @payload)
Redis.current.publish('timeline:public:local:media', @payload) if @status.local?
end

def redis
Redis.current
redis.publish('timeline:public:media', @payload)
redis.publish('timeline:public:local:media', @payload) if @status.local?
end
end
5 changes: 1 addition & 4 deletions app/workers/scheduler/feed_cleanup_scheduler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

class Scheduler::FeedCleanupScheduler
include Sidekiq::Worker
include Redisable

sidekiq_options unique: :until_executed, retry: 0

Expand Down Expand Up @@ -57,8 +58,4 @@ def inactive_list_ids
def feed_manager
FeedManager.instance
end

def redis
Redis.current
end
end

0 comments on commit 5b0f970

Please sign in to comment.