Add mute relationship and user methods

This commit is contained in:
Andreas Nedbal 2022-12-28 02:59:51 +01:00 committed by Andreas Nedbal
parent 6e30bc73cf
commit 361b8df798
4 changed files with 44 additions and 0 deletions

View File

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

View File

@ -4,6 +4,7 @@ class User < ApplicationRecord
include User::Relationship include User::Relationship
include User::Relationship::Follow include User::Relationship::Follow
include User::Relationship::Block include User::Relationship::Block
include User::Relationship::Mute
include User::AnswerMethods include User::AnswerMethods
include User::BanMethods include User::BanMethods
include User::InboxMethods include User::InboxMethods

View File

@ -0,0 +1,36 @@
# frozen_string_literal: true
require "errors"
class User
module Relationship
module Mute
extend ActiveSupport::Concern
included do
has_many :active_mute_relationships, class_name: "Relationships::Mute",
foreign_key: "source_id",
dependent: :destroy
has_many :passive_mute_relationships, class_name: "Relationships::Mute",
foreign_key: "target_id",
dependent: :destroy
has_many :muted_users, through: :active_mute_relationships, source: :target
has_many :muted_by_users, through: :passive_mute_relationships, source: :source
end
def mute(target_user)
raise Errors::MutingSelf if target_user == self
create_relationship(active_mute_relationships, target_user)
end
def unmute(target_user)
destroy_relationship(active_mute_relationships, target_user)
end
def muting?(target_user)
relationship_active?(muted_users, target_user)
end
end
end
end

View File

@ -119,4 +119,7 @@ module Errors
end end
end end
# endregion # endregion
class MutingSelf < SelfAction
end
end end