Move app/labor/follow_checker.rb to service (#11613)
* Move app/labor/follow_checker.rb to service * Move to Follows:: namespace
This commit is contained in:
parent
ab045700e1
commit
5185de4c91
4 changed files with 44 additions and 37 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
35
app/services/follows/check_cached.rb
Normal file
35
app/services/follows/check_cached.rb
Normal file
|
|
@ -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
|
||||
|
|
@ -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
|
||||
Loading…
Add table
Reference in a new issue