]> cat aescling's git repositories - mastodon.git/commitdiff
Integrating block relationships into the API (read-only for now)
authorEugen Rochko <eugen@zeonfederated.com>
Mon, 3 Oct 2016 15:16:58 +0000 (17:16 +0200)
committerEugen Rochko <eugen@zeonfederated.com>
Mon, 3 Oct 2016 15:16:58 +0000 (17:16 +0200)
app/controllers/api/v1/accounts_controller.rb
app/models/account.rb

index 6d385767515686990b46adc20d2736dce2cbddd9..850d00d2eb82c8d482bc87c45d442c7d2ffbf486 100644 (file)
@@ -40,7 +40,7 @@ class Api::V1::AccountsController < ApiController
     @accounts    = Account.find(ids)
     @following   = Account.following_map(ids, current_user.account_id)
     @followed_by = Account.followed_by_map(ids, current_user.account_id)
-    @blocking    = {}
+    @blocking    = Account.blocking_map(ids, current_user.account_id)
   end
 
   private
@@ -52,6 +52,6 @@ class Api::V1::AccountsController < ApiController
   def set_relationship
     @following   = Account.following_map([@account.id], current_user.account_id)
     @followed_by = Account.followed_by_map([@account.id], current_user.account_id)
-    @blocking    = {}
+    @blocking    = Account.blocking_map([@account.id], current_user.account_id)
   end
 end
index 518b55f1923d451747a279027694c3fc66358f66..12e7be05d7463e90deb0ee3ad41bfd7935bb154b 100644 (file)
@@ -52,11 +52,20 @@ class Account < ApplicationRecord
     active_relationships.where(target_account: other_account).first_or_create!(target_account: other_account)
   end
 
+  def block!(other_account)
+    block_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 unless follow.nil?
   end
 
+  def unblock!(other_account)
+    block = block_relationships.find_by(target_account: other_account)
+    block.destroy unless block.nil?
+  end
+
   def following?(other_account)
     following.include?(other_account)
   end
@@ -139,6 +148,10 @@ class Account < ApplicationRecord
     Follow.where(account_id: target_account_ids).where(target_account_id: account_id).map { |f| [f.account_id, true] }.to_h
   end
 
+  def self.blocking_map(target_account_ids, account_id)
+    Block.where(target_account_id: target_account_ids).where(account_id: account_id).map { |b| [b.target_account_id, true] }.to_h
+  end
+
   before_create do
     if local?
       keypair = OpenSSL::PKey::RSA.new(Rails.env.test? ? 1024 : 2048)