Skip to content

Commit 5b0f970

Browse files
ysksnGargron
authored andcommitted
Create Redisable#redis (mastodon#9633)
* Create Redisable * Use #redis instead of Redis.current
1 parent 70b075c commit 5b0f970

13 files changed

Lines changed: 39 additions & 59 deletions

app/lib/activity_tracker.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ class ActivityTracker
44
EXPIRE_AFTER = 90.days.seconds
55

66
class << self
7+
include Redisable
8+
79
def increment(prefix)
810
key = [prefix, current_week].join(':')
911

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

2123
private
2224

23-
def redis
24-
Redis.current
25-
end
26-
2725
def current_week
2826
Time.zone.today.cweek
2927
end

app/lib/activitypub/activity.rb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
class ActivityPub::Activity
44
include JsonLdHelper
5+
include Redisable
56

67
def initialize(json, account, **options)
78
@json = json
@@ -70,10 +71,6 @@ def object_uri
7071
@object_uri ||= value_or_id(@object)
7172
end
7273

73-
def redis
74-
Redis.current
75-
end
76-
7774
def distribute(status)
7875
crawl_links(status)
7976

app/lib/feed_manager.rb

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
class FeedManager
66
include Singleton
7+
include Redisable
78

89
MAX_ITEMS = 400
910

@@ -35,7 +36,7 @@ def push_to_home(account, status)
3536

3637
def unpush_from_home(account, status)
3738
return false unless remove_from_feed(:home, account.id, status)
38-
Redis.current.publish("timeline:#{account.id}", Oj.dump(event: :delete, payload: status.id.to_s))
39+
redis.publish("timeline:#{account.id}", Oj.dump(event: :delete, payload: status.id.to_s))
3940
true
4041
end
4142

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

5455
def unpush_from_list(list, status)
5556
return false unless remove_from_feed(:list, list.id, status)
56-
Redis.current.publish("timeline:list:#{list.id}", Oj.dump(event: :delete, payload: status.id.to_s))
57+
redis.publish("timeline:list:#{list.id}", Oj.dump(event: :delete, payload: status.id.to_s))
5758
true
5859
end
5960

@@ -142,10 +143,6 @@ def populate_feed(account)
142143

143144
private
144145

145-
def redis
146-
Redis.current
147-
end
148-
149146
def push_update_required?(timeline_id)
150147
redis.exists("subscribed:#{timeline_id}")
151148
end

app/lib/ostatus/activity/base.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# frozen_string_literal: true
22

33
class OStatus::Activity::Base
4+
include Redisable
5+
46
def initialize(xml, account = nil, **options)
57
@xml = xml
68
@account = account
@@ -66,8 +68,4 @@ def find_activitypub_status(uri, href)
6668
Status.find_by(uri: uri)
6769
end
6870
end
69-
70-
def redis
71-
Redis.current
72-
end
7371
end

app/lib/potential_friendship_tracker.rb

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ class PotentialFriendshipTracker
1111
}.freeze
1212

1313
class << self
14+
include Redisable
15+
1416
def record(account_id, target_account_id, action)
1517
return if account_id == target_account_id
1618

@@ -31,11 +33,5 @@ def get(account_id, limit: 20, offset: 0)
3133
return [] if account_ids.empty?
3234
Account.searchable.where(id: account_ids)
3335
end
34-
35-
private
36-
37-
def redis
38-
Redis.current
39-
end
4036
end
4137
end

app/models/concerns/redisable.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# frozen_string_literal: true
2+
3+
module Redisable
4+
extend ActiveSupport::Concern
5+
6+
private
7+
8+
def redis
9+
Redis.current
10+
end
11+
end

app/models/feed.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# frozen_string_literal: true
22

33
class Feed
4+
include Redisable
5+
46
def initialize(type, id)
57
@type = type
68
@id = id
@@ -27,8 +29,4 @@ def from_redis(limit, max_id, since_id, min_id)
2729
def key
2830
FeedManager.instance.key(@type, @id)
2931
end
30-
31-
def redis
32-
Redis.current
33-
end
3432
end

app/models/trending_tags.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ class TrendingTags
77
THRESHOLD = 5
88

99
class << self
10+
include Redisable
11+
1012
def record_use!(tag, account, at_time = Time.now.utc)
1113
return if disallowed_hashtags.include?(tag.name) || account.silenced? || account.bot?
1214

@@ -59,9 +61,5 @@ def disallowed_hashtags
5961
@disallowed_hashtags = @disallowed_hashtags.split(' ') if @disallowed_hashtags.is_a? String
6062
@disallowed_hashtags = @disallowed_hashtags.map(&:downcase)
6163
end
62-
63-
def redis
64-
Redis.current
65-
end
6664
end
6765
end

app/services/batched_remove_status_service.rb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
class BatchedRemoveStatusService < BaseService
44
include StreamEntryRenderer
5+
include Redisable
56

67
# Delete given statuses and reblogs of them
78
# Dispatch PuSH updates of the deleted statuses, but only local ones
@@ -109,10 +110,6 @@ def batch_salmon_slaps(status)
109110
end
110111
end
111112

112-
def redis
113-
Redis.current
114-
end
115-
116113
def build_xml(stream_entry)
117114
return @activity_xml[stream_entry.id] if @activity_xml.key?(stream_entry.id)
118115

app/services/follow_service.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# frozen_string_literal: true
22

33
class FollowService < BaseService
4+
include Redisable
5+
46
# Follow a remote user, notify remote user about the follow
57
# @param [Account] source_account From which to follow
68
# @param [String, Account] uri User URI to follow in the form of username@domain (or account record)
@@ -67,10 +69,6 @@ def direct_follow(source_account, target_account, reblogs: true)
6769
follow
6870
end
6971

70-
def redis
71-
Redis.current
72-
end
73-
7472
def build_follow_request_xml(follow_request)
7573
OStatus::AtomSerializer.render(OStatus::AtomSerializer.new.follow_request_salmon(follow_request))
7674
end

0 commit comments

Comments
 (0)