Add Block relationship type

This commit is contained in:
Karina Kwiatek 2022-04-18 20:24:15 +01:00 committed by Karina Kwiatek
parent 47aeaa158c
commit 0038272417
5 changed files with 83 additions and 0 deletions

View File

@ -0,0 +1,4 @@
# frozen_string_literal: true
class Relationships::Block < Relationship
end

View File

@ -1,6 +1,7 @@
class User < ApplicationRecord
include User::Relationship
include User::Relationship::Follow
include User::Relationship::Block
include User::AnswerMethods
include User::InboxMethods
include User::QuestionMethods

View File

@ -0,0 +1,44 @@
# frozen_string_literal: true
require 'errors'
class User
module Relationship
module Block
extend ActiveSupport::Concern
included do
has_many :active_block_relationships, class_name: "Relationships::Block",
foreign_key: "source_id",
dependent: :destroy
has_many :passive_block_relationships, class_name: "Relationships::Block",
foreign_key: "target_id",
dependent: :destroy
has_many :blocked_users, through: :active_block_relationships, source: :target
has_many :blocked_by_users, through: :passive_block_relationships, source: :source
end
# Block an user
def block(target_user)
raise Errors::BlockingSelf if target_user == self
unfollow(target_user) if following?(target_user)
target_user.unfollow(self) if target_user.following?(self)
target_user.inboxes.where(question: { user_id: id }).destroy_all
inboxes.where(question: { user_id: target_user.id, author_is_anonymous: false }).destroy_all
ListMember.where(list: { user_id: target_user.id }, user_id: id).destroy_all
create_relationship(active_block_relationships, target_user)
end
# Unblock an user
def unblock(target_user)
destroy_relationship(active_block_relationships, target_user)
end
# Is <tt>self</tt> blocking <tt>target_user</tt>?
def blocking?(target_user)
relationship_active?(blocked_users, target_user)
end
end
end
end

View File

@ -21,6 +21,8 @@ class User
# Follow an user
def follow(target_user)
raise Errors::FollowingSelf if target_user == self
raise Errors::FollowingOtherBlockedSelf if target_user.blocked_by?(self)
raise Errors::FollowingSelfBlockedOther if blocked_by?(target_user)
create_relationship(active_follow_relationships, target_user)
end

View File

@ -42,4 +42,36 @@ module Errors
class UserNotFound < NotFound
end
# region User Blocking
class Blocked < Forbidden
end
class OtherBlockedSelf < Blocked
end
class BlockingSelf < SelfAction
end
class AskingOtherBlockedSelf < OtherBlockedSelf
end
class FollowingOtherBlockedSelf < OtherBlockedSelf
end
class SelfBlockedOther < Blocked
end
class AskingSelfBlockedOther < SelfBlockedOther
end
class FollowingSelfBlockedOther < SelfBlockedOther
end
class AnsweringOtherBlockedSelf < OtherBlockedSelf
end
class AnsweringSelfBlockedOther < SelfBlockedOther
end
# endregion
end