2022-12-27 17:59:51 -08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class User
|
|
|
|
module Relationship
|
|
|
|
module Mute
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
|
|
|
has_many :active_mute_relationships, class_name: "Relationships::Mute",
|
2022-12-28 20:17:04 -08:00
|
|
|
foreign_key: "source_id",
|
|
|
|
dependent: :destroy
|
2022-12-27 17:59:51 -08:00
|
|
|
has_many :passive_mute_relationships, class_name: "Relationships::Mute",
|
2022-12-28 20:17:04 -08:00
|
|
|
foreign_key: "target_id",
|
|
|
|
dependent: :destroy
|
2022-12-27 17:59:51 -08:00
|
|
|
has_many :muted_users, through: :active_mute_relationships, source: :target
|
2022-12-28 20:17:04 -08:00
|
|
|
has_many :muted_by_users, through: :passive_mute_relationships, source: :source
|
2022-12-27 17:59:51 -08:00
|
|
|
end
|
|
|
|
|
2023-03-08 12:37:39 -08:00
|
|
|
# Mute a user
|
2022-12-27 17:59:51 -08:00
|
|
|
def mute(target_user)
|
|
|
|
raise Errors::MutingSelf if target_user == self
|
|
|
|
|
2023-02-20 12:06:10 -08:00
|
|
|
create_relationship(active_mute_relationships, target_user).tap do
|
|
|
|
expire_muted_user_ids_cache
|
|
|
|
end
|
2022-12-27 17:59:51 -08:00
|
|
|
end
|
|
|
|
|
2023-02-20 12:06:10 -08:00
|
|
|
# Unmute an user
|
2022-12-27 17:59:51 -08:00
|
|
|
def unmute(target_user)
|
2023-02-20 12:06:10 -08:00
|
|
|
destroy_relationship(active_mute_relationships, target_user).tap do
|
|
|
|
expire_muted_user_ids_cache
|
|
|
|
end
|
2022-12-27 17:59:51 -08:00
|
|
|
end
|
|
|
|
|
2023-02-20 12:06:10 -08:00
|
|
|
# Is <tt>self</tt> muting <tt>target_user</tt>?
|
2022-12-27 17:59:51 -08:00
|
|
|
def muting?(target_user)
|
|
|
|
relationship_active?(muted_users, target_user)
|
|
|
|
end
|
2023-02-20 12:06:10 -08:00
|
|
|
|
|
|
|
# Expires the muted user ids cache
|
|
|
|
def expire_muted_user_ids_cache = Rails.cache.delete(cache_key_muted_user_ids)
|
|
|
|
|
|
|
|
# Cached ids of the muted users
|
|
|
|
def muted_user_ids_cached
|
|
|
|
Rails.cache.fetch(cache_key_muted_user_ids, expires_in: 1.hour) do
|
|
|
|
muted_user_ids
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
# Cache key for the muted_user_ids
|
|
|
|
def cache_key_muted_user_ids = "#{cache_key}/muted_user_ids"
|
2022-12-27 17:59:51 -08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|