Retrospring/app/models/user/relationship/block.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

65 lines
2.3 KiB
Ruby
Raw Normal View History

2022-04-18 12:24:15 -07:00
# frozen_string_literal: true
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
2022-06-12 04:46:48 -07:00
unfollow_and_remove(target_user)
create_relationship(active_block_relationships, target_user).tap do
expire_blocked_user_ids_cache
end
2022-04-18 12:24:15 -07:00
end
# Unblock an user
def unblock(target_user)
destroy_relationship(active_block_relationships, target_user).tap do
expire_blocked_user_ids_cache
end
2022-04-18 12:24:15 -07:00
end
# Is <tt>self</tt> blocking <tt>target_user</tt>?
def blocking?(target_user)
relationship_active?(blocked_users, target_user)
end
2022-06-12 04:46:48 -07:00
# Expire the blocked user ids cache
def expire_blocked_user_ids_cache = Rails.cache.delete(cache_key_blocked_user_ids)
# Cached ids of the blocked users
def blocked_user_ids_cached
Rails.cache.fetch(cache_key_blocked_user_ids, expires_in: 1.hour) do
blocked_user_ids
end
end
2022-06-12 04:46:48 -07:00
private
def unfollow_and_remove(target_user)
unfollow(target_user) if following?(target_user)
target_user.unfollow(self) if target_user.following?(self)
target_user.inbox_entries.joins(:question).where(question: { user_id: id }).destroy_all
inbox_entries.joins(:question).where(questions: { user_id: target_user.id, author_is_anonymous: false }).destroy_all
2022-06-12 04:46:48 -07:00
ListMember.joins(:list).where(list: { user_id: target_user.id }, user_id: id).destroy_all
end
def cache_key_blocked_user_ids = "#{cache_key}/blocked_user_ids"
2022-04-18 12:24:15 -07:00
end
end
end