about summary refs log tree commit diff
path: root/app/models
diff options
context:
space:
mode:
authorKit Redgrave <qwertyitis@gmail.com>2017-02-05 19:51:56 -0600
committerKit Redgrave <qwertyitis@gmail.com>2017-03-01 22:31:21 -0600
commit442fdbfc5309f46c23a073829e5fe16d10c7c6ca (patch)
treeee8195e874e83722990cfaca7b65fc54383caaf9 /app/models
parent89fc2d7f4810ecdf66b17543f4603c1089a0c3f5 (diff)
Mute button progress so far. WIP, doesn't entirely work correctly.
Diffstat (limited to 'app/models')
-rw-r--r--app/models/account.rb21
-rw-r--r--app/models/mute.rb32
-rw-r--r--app/models/status.rb13
3 files changed, 62 insertions, 4 deletions
diff --git a/app/models/account.rb b/app/models/account.rb
index a93a0668a..2fa6bab71 100644
--- a/app/models/account.rb
+++ b/app/models/account.rb
@@ -46,6 +46,10 @@ class Account < ApplicationRecord
   has_many :block_relationships, class_name: 'Block', foreign_key: 'account_id', dependent: :destroy
   has_many :blocking, -> { order('blocks.id desc') }, through: :block_relationships, source: :target_account
 
+  # Mute relationships
+  has_many :mute_relationships, class_name: 'Mute', foreign_key: 'account_id', dependent: :destroy
+  has_many :muting, -> { order('mutes.id desc') }, through: :mute_relationships, source: :target_account
+
   # Media
   has_many :media_attachments, dependent: :destroy
 
@@ -73,6 +77,10 @@ class Account < ApplicationRecord
     block_relationships.where(target_account: other_account).first_or_create!(target_account: other_account)
   end
 
+  def mute!(other_account)
+    mute_relationships.where(target_account: other_account).first_or_create!(target_account: other_account)
+  end
+
   def unfollow!(other_account)
     follow = active_relationships.find_by(target_account: other_account)
     follow&.destroy
@@ -83,6 +91,11 @@ class Account < ApplicationRecord
     block&.destroy
   end
 
+  def unmute!(other_account)
+    mute = mute_relationships.find_by(target_account: other_account)
+    mute&.destroy
+  end
+
   def following?(other_account)
     following.include?(other_account)
   end
@@ -91,6 +104,10 @@ class Account < ApplicationRecord
     blocking.include?(other_account)
   end
 
+  def muting?(other_account)
+    muting.include?(other_account)
+  end
+
   def requested?(other_account)
     follow_requests.where(target_account: other_account).exists?
   end
@@ -188,6 +205,10 @@ class Account < ApplicationRecord
       follow_mapping(Block.where(target_account_id: target_account_ids, account_id: account_id), :target_account_id)
     end
 
+    def muting_map(target_account_ids, account_id)
+      follow_mapping(Mute.where(target_account_id: target_account_ids, account_id: account_id), :target_account_id)
+    end
+
     def requested_map(target_account_ids, account_id)
       follow_mapping(FollowRequest.where(target_account_id: target_account_ids, account_id: account_id), :target_account_id)
     end
diff --git a/app/models/mute.rb b/app/models/mute.rb
new file mode 100644
index 000000000..505196453
--- /dev/null
+++ b/app/models/mute.rb
@@ -0,0 +1,32 @@
+# frozen_string_literal: true
+
+class Mute < ApplicationRecord
+  include Paginable
+  include Streamable
+
+  belongs_to :account
+  belongs_to :target_account, class_name: 'Account'
+
+  validates :account, :target_account, presence: true
+  validates :account_id, uniqueness: { scope: :target_account_id }
+
+  def verb
+    destroyed? ? :unmute : :mute
+  end
+
+  def target
+    target_account
+  end
+
+  def object_type
+    :person
+  end
+
+  def hidden?
+    true
+  end
+
+  def title
+    destroyed? ? "#{account.acct} is no longer muting #{target_account.acct}" : "#{account.acct} muted #{target_account.acct}"
+  end
+end
diff --git a/app/models/status.rb b/app/models/status.rb
index 1b40897f3..b3424f36d 100644
--- a/app/models/status.rb
+++ b/app/models/status.rb
@@ -103,7 +103,10 @@ class Status < ApplicationRecord
 
   class << self
     def as_home_timeline(account)
-      where(account: [account] + account.following)
+      muted = Mute.where(account: account).pluck(:target_account_id)
+      query = where(account: [account] + account.following)
+      query = query.where('statuses.account_id NOT IN (?)', muted) unless muted.empty?
+      query
     end
 
     def as_public_timeline(account = nil, local_only = false)
@@ -169,8 +172,10 @@ class Status < ApplicationRecord
 
     def filter_timeline(query, account)
       blocked = Block.where(account: account).pluck(:target_account_id) + Block.where(target_account: account).pluck(:account_id)
-      query   = query.where('statuses.account_id NOT IN (?)', blocked) unless blocked.empty?
-      query   = query.where('accounts.silenced = TRUE') if account.silenced?
+      muted   = Mute.where(account: account).pluck(:target_account_id)
+      query   = query.where('statuses.account_id NOT IN (?)', blocked) unless blocked.empty?  # Only give us statuses from people we haven't blocked
+      query   = query.where('statuses.account_id NOT IN (?)', muted) unless muted.empty?      # and out of those, only people we haven't muted
+      query   = query.where('accounts.silenced = TRUE') if account.silenced?                  # and if we're hellbanned, only people who are also hellbanned
       query
     end
 
@@ -192,6 +197,6 @@ class Status < ApplicationRecord
   private
 
   def filter_from_context?(status, account)
-    account&.blocking?(status.account_id) || (status.account.silenced? && !account&.following?(status.account_id)) || !status.permitted?(account)
+    account&.blocking?(status.account_id) || account&.muting?(status.account_id) || (status.account.silenced? && !account&.following?(status.account_id)) || !status.permitted?(account)
   end
 end