mirror of
https://github.com/Retrospring/retrospring.git
synced 2025-02-07 13:43:36 +01:00
Add Block relationship type
This commit is contained in:
parent
47aeaa158c
commit
0038272417
5 changed files with 83 additions and 0 deletions
4
app/models/relationships/block.rb
Normal file
4
app/models/relationships/block.rb
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
class Relationships::Block < Relationship
|
||||||
|
end
|
|
@ -1,6 +1,7 @@
|
||||||
class User < ApplicationRecord
|
class User < ApplicationRecord
|
||||||
include User::Relationship
|
include User::Relationship
|
||||||
include User::Relationship::Follow
|
include User::Relationship::Follow
|
||||||
|
include User::Relationship::Block
|
||||||
include User::AnswerMethods
|
include User::AnswerMethods
|
||||||
include User::InboxMethods
|
include User::InboxMethods
|
||||||
include User::QuestionMethods
|
include User::QuestionMethods
|
||||||
|
|
44
app/models/user/relationship/block.rb
Normal file
44
app/models/user/relationship/block.rb
Normal 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
|
|
@ -21,6 +21,8 @@ class User
|
||||||
# Follow an user
|
# Follow an user
|
||||||
def follow(target_user)
|
def follow(target_user)
|
||||||
raise Errors::FollowingSelf if target_user == self
|
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)
|
create_relationship(active_follow_relationships, target_user)
|
||||||
end
|
end
|
||||||
|
|
|
@ -42,4 +42,36 @@ module Errors
|
||||||
|
|
||||||
class UserNotFound < NotFound
|
class UserNotFound < NotFound
|
||||||
end
|
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
|
end
|
||||||
|
|
Loading…
Reference in a new issue