diff --git a/app/controllers/follows_controller.rb b/app/controllers/follows_controller.rb index 2b981d49e..a0e62ab02 100644 --- a/app/controllers/follows_controller.rb +++ b/app/controllers/follows_controller.rb @@ -10,12 +10,12 @@ class FollowsController < ApplicationController return end - following_them_check = FollowChecker.new(current_user, params[:followable_type], params[:id]).cached_follow_check + following_them_check = Follows::CheckCached.call(current_user, params[:followable_type], params[:id]) return render plain: following_them_check unless params[:followable_type] == "User" - following_you_check = FollowChecker.new(User.find_by(id: params[:id]), params[:followable_type], - current_user.id).cached_follow_check + following_you_check = Follows::CheckCached.call(User.find_by(id: params[:id]), params[:followable_type], + current_user.id) if following_them_check && following_you_check render plain: "mutual" @@ -34,9 +34,9 @@ class FollowsController < ApplicationController if current_user.id == id "self" else - following_them_check = FollowChecker.new(current_user, params[:followable_type], id).cached_follow_check - following_you_check = FollowChecker.new(User.find_by(id: id), params[:followable_type], - current_user.id).cached_follow_check + following_them_check = Follows::CheckCached.call(current_user, params[:followable_type], id) + following_you_check = Follows::CheckCached.call(User.find_by(id: id), params[:followable_type], + current_user.id) if following_them_check && following_you_check "mutual" elsif following_you_check diff --git a/app/labor/follow_checker.rb b/app/labor/follow_checker.rb deleted file mode 100644 index 4f7180cf0..000000000 --- a/app/labor/follow_checker.rb +++ /dev/null @@ -1,28 +0,0 @@ -class FollowChecker - attr_accessor :follower, :followable_type, :followable_id - - def initialize(follower, followable_type, followable_id) - @follower = follower - @followable_type = followable_type - @followable_id = followable_id - end - - def cached_follow_check - return false unless follower - - cache_key = "user-#{follower.id}-#{follower.updated_at.rfc3339}/is_following_#{followable_type}_#{followable_id}" - Rails.cache.fetch(cache_key, expires_in: 20.hours) do - followable = case followable_type - when "Tag" - Tag.find(followable_id) - when "Organization" - Organization.find(followable_id) - when "Podcast" - Podcast.find(followable_id) - else - User.find(followable_id) - end - follower.following?(followable) - end - end -end diff --git a/app/services/follows/check_cached.rb b/app/services/follows/check_cached.rb new file mode 100644 index 000000000..d62e87f7d --- /dev/null +++ b/app/services/follows/check_cached.rb @@ -0,0 +1,35 @@ +module Follows + class CheckCached + def self.call(follower, followable_type, followable_id) + new(follower, followable_type, followable_id).call + end + + def initialize(follower, followable_type, followable_id) + @follower = follower + @followable_type = followable_type + @followable_id = followable_id + end + + def call + return false unless follower + + cache_key = "user-#{follower.id}-#{follower.updated_at.rfc3339}/is_following_#{followable_type}_#{followable_id}" + Rails.cache.fetch(cache_key, expires_in: 20.hours) do + follower.following?(followable.find(followable_id)) + end + end + + private + + attr_accessor :follower, :followable_type, :followable_id + + def followable + case followable_type + when "Tag", "Organization", "Podcast" + followable_type.constantize + else + User + end + end + end +end diff --git a/spec/labor/follow_checker_spec.rb b/spec/services/follows/check_cached_spec.rb similarity index 56% rename from spec/labor/follow_checker_spec.rb rename to spec/services/follows/check_cached_spec.rb index a5c83564c..ea4d1f3a2 100644 --- a/spec/labor/follow_checker_spec.rb +++ b/spec/services/follows/check_cached_spec.rb @@ -1,16 +1,16 @@ require "rails_helper" -RSpec.describe FollowChecker, type: :labor do +RSpec.describe Follows::CheckCached, type: :service do let(:user) { create(:user) } it "checks if following a thing and returns true if they are" do user2 = create(:user) user.follow(user2) - expect(described_class.new(user, "User", user2.id).cached_follow_check).to eq(true) + expect(described_class.call(user, "User", user2.id)).to eq(true) end it "checks if following a thing and returns false if they are not" do user2 = create(:user) - expect(described_class.new(user, "User", user2.id).cached_follow_check).to eq(false) + expect(described_class.call(user, "User", user2.id)).to eq(false) end end