From 0038272417d2c8831ba3b8e02910efe7044f13cc Mon Sep 17 00:00:00 2001 From: Karina Kwiatek Date: Mon, 18 Apr 2022 20:24:15 +0100 Subject: [PATCH] Add Block relationship type --- app/models/relationships/block.rb | 4 +++ app/models/user.rb | 1 + app/models/user/relationship/block.rb | 44 ++++++++++++++++++++++++++ app/models/user/relationship/follow.rb | 2 ++ lib/errors.rb | 32 +++++++++++++++++++ 5 files changed, 83 insertions(+) create mode 100644 app/models/relationships/block.rb create mode 100644 app/models/user/relationship/block.rb diff --git a/app/models/relationships/block.rb b/app/models/relationships/block.rb new file mode 100644 index 00000000..50ceef5b --- /dev/null +++ b/app/models/relationships/block.rb @@ -0,0 +1,4 @@ +# frozen_string_literal: true + +class Relationships::Block < Relationship +end diff --git a/app/models/user.rb b/app/models/user.rb index 1b74c939..de4f37c2 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 diff --git a/app/models/user/relationship/block.rb b/app/models/user/relationship/block.rb new file mode 100644 index 00000000..7cb2cdd3 --- /dev/null +++ b/app/models/user/relationship/block.rb @@ -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 self blocking target_user? + def blocking?(target_user) + relationship_active?(blocked_users, target_user) + end + end + end +end diff --git a/app/models/user/relationship/follow.rb b/app/models/user/relationship/follow.rb index c45d93ca..fb30325b 100644 --- a/app/models/user/relationship/follow.rb +++ b/app/models/user/relationship/follow.rb @@ -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 diff --git a/lib/errors.rb b/lib/errors.rb index 375db88a..ab7a8b7a 100644 --- a/lib/errors.rb +++ b/lib/errors.rb @@ -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