about summary refs log tree commit diff
path: root/app
diff options
context:
space:
mode:
authorEugen Rochko <eugen@zeonfederated.com>2016-10-09 14:48:43 +0200
committerEugen Rochko <eugen@zeonfederated.com>2016-10-09 14:48:59 +0200
commit22a8801dbc77d2d01b326a7cb89d1a28b054e073 (patch)
tree11a3a99e98df33cbb73da818419fa0227b1dc664 /app
parent52d7f862d365acfd4eacbe448238699d9662708d (diff)
Adding domain blocks
Diffstat (limited to 'app')
-rw-r--r--app/controllers/api/subscriptions_controller.rb3
-rw-r--r--app/models/account.rb8
-rw-r--r--app/models/domain_block.rb7
-rw-r--r--app/services/block_domain_service.rb13
-rw-r--r--app/services/follow_remote_account_service.rb1
-rw-r--r--app/services/process_interaction_service.rb2
6 files changed, 29 insertions, 5 deletions
diff --git a/app/controllers/api/subscriptions_controller.rb b/app/controllers/api/subscriptions_controller.rb
index c5190b136..c3aeee94d 100644
--- a/app/controllers/api/subscriptions_controller.rb
+++ b/app/controllers/api/subscriptions_controller.rb
@@ -13,8 +13,9 @@ class Api::SubscriptionsController < ApiController
 
   def update
     body = request.body.read
+    subscription = @account.subscription(api_subscription_url(@account.id))
 
-    if @account.subscription(api_subscription_url(@account.id)).verify(body, request.headers['HTTP_X_HUB_SIGNATURE'])
+    if subscription.verify(body, request.headers['HTTP_X_HUB_SIGNATURE'])
       ProcessFeedService.new.call(body, @account)
       head 201
     else
diff --git a/app/models/account.rb b/app/models/account.rb
index 12e7be05d..e43d51b1c 100644
--- a/app/models/account.rb
+++ b/app/models/account.rb
@@ -24,10 +24,10 @@ class Account < ApplicationRecord
   validates :note, length: { maximum: 124 }, if: 'local?'
 
   # Timelines
-  has_many :stream_entries, inverse_of: :account
-  has_many :statuses, inverse_of: :account
-  has_many :favourites, inverse_of: :account
-  has_many :mentions, inverse_of: :account
+  has_many :stream_entries, inverse_of: :account, dependent: :destroy
+  has_many :statuses, inverse_of: :account, dependent: :destroy
+  has_many :favourites, inverse_of: :account, dependent: :destroy
+  has_many :mentions, inverse_of: :account, dependent: :destroy
 
   # Follow relations
   has_many :active_relationships,  class_name: 'Follow', foreign_key: 'account_id',        dependent: :destroy
diff --git a/app/models/domain_block.rb b/app/models/domain_block.rb
new file mode 100644
index 000000000..8f9eb1182
--- /dev/null
+++ b/app/models/domain_block.rb
@@ -0,0 +1,7 @@
+class DomainBlock < ApplicationRecord
+  validates :domain, presence: true, uniqueness: true
+
+  def self.blocked?(domain)
+    where(domain: domain).exists?
+  end
+end
diff --git a/app/services/block_domain_service.rb b/app/services/block_domain_service.rb
new file mode 100644
index 000000000..075460605
--- /dev/null
+++ b/app/services/block_domain_service.rb
@@ -0,0 +1,13 @@
+class BlockDomainService < BaseService
+  def call(domain)
+    block = DomainBlock.find_or_create_by!(domain: domain)
+
+    Account.where(domain: domain).find_each do |account|
+      if account.subscribed?
+        account.subscription('').unsubscribe
+      end
+
+      account.destroy!
+    end
+  end
+end
diff --git a/app/services/follow_remote_account_service.rb b/app/services/follow_remote_account_service.rb
index 3b305504c..43a598635 100644
--- a/app/services/follow_remote_account_service.rb
+++ b/app/services/follow_remote_account_service.rb
@@ -8,6 +8,7 @@ class FollowRemoteAccountService < BaseService
     username, domain = uri.split('@')
 
     return Account.find_local(username) if TagManager.instance.local_domain?(domain)
+    return nil if DomainBlock.blocked?(domain)
 
     account = Account.find_remote(username, domain)
 
diff --git a/app/services/process_interaction_service.rb b/app/services/process_interaction_service.rb
index 0768579ef..75051c5df 100644
--- a/app/services/process_interaction_service.rb
+++ b/app/services/process_interaction_service.rb
@@ -13,6 +13,8 @@ class ProcessInteractionService < BaseService
     domain   = Addressable::URI.parse(url).host
     account  = Account.find_by(username: username, domain: domain)
 
+    return if DomainBlock.blocked?(domain)
+
     if account.nil?
       account = follow_remote_account_service.call("#{username}@#{domain}")
     end