40 lines
1.4 KiB
Ruby
40 lines
1.4 KiB
Ruby
|
# frozen_string_literal: true
|
||
|
|
||
|
require "errors"
|
||
|
|
||
|
class User
|
||
|
module Relationship
|
||
|
module Follow
|
||
|
extend ActiveSupport::Concern
|
||
|
|
||
|
included do
|
||
|
has_many :active_follow_relationships, class_name: "Relationships::Follow",
|
||
|
foreign_key: "source_id",
|
||
|
dependent: :destroy
|
||
|
has_many :passive_follow_relationships, class_name: "Relationships::Follow",
|
||
|
foreign_key: "target_id",
|
||
|
dependent: :destroy
|
||
|
has_many :followings, through: :active_follow_relationships, source: :target
|
||
|
has_many :followers, through: :passive_follow_relationships, source: :source
|
||
|
end
|
||
|
|
||
|
# Follow an user
|
||
|
def follow(target_user)
|
||
|
raise Justask::Errors::FollowingOtherBlockedSelf if target_user.blocking?(self)
|
||
|
raise Justask::Errors::FollowingSelfBlockedOther if blocking?(target_user)
|
||
|
raise Justask::Errors::FollowingSelf if target_user == self
|
||
|
create_relationship(active_follow_relationships, target_user)
|
||
|
end
|
||
|
|
||
|
# Unfollow an user
|
||
|
def unfollow(target_user)
|
||
|
destroy_relationship(active_follow_relationships, target_user)
|
||
|
end
|
||
|
|
||
|
def following?(target_user)
|
||
|
relationship_active?(followings, target_user)
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
end
|