2021-12-31 13:19:21 -08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require "errors"
|
|
|
|
|
|
|
|
class User
|
|
|
|
module Relationship
|
|
|
|
module Follow
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
2022-01-16 09:51:27 -08:00
|
|
|
has_many :active_follow_relationships, class_name: "Relationships::Follow",
|
2021-12-31 13:19:21 -08:00
|
|
|
foreign_key: "source_id",
|
2022-01-16 09:51:27 -08:00
|
|
|
dependent: :destroy
|
|
|
|
has_many :passive_follow_relationships, class_name: "Relationships::Follow",
|
2021-12-31 13:19:21 -08:00
|
|
|
foreign_key: "target_id",
|
2022-01-16 09:51:27 -08:00
|
|
|
dependent: :destroy
|
2021-12-31 13:19:21 -08:00
|
|
|
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)
|
2022-01-02 15:31:55 -08:00
|
|
|
raise Errors::FollowingSelf if target_user == self
|
2022-06-09 14:08:38 -07:00
|
|
|
raise Errors::FollowingOtherBlockedSelf if self.blocking?(target_user)
|
|
|
|
raise Errors::FollowingSelfBlockedOther if target_user.blocking?(self)
|
2022-01-16 09:51:27 -08:00
|
|
|
|
2021-12-31 13:19:21 -08:00
|
|
|
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
|