Adding domain blocks

This commit is contained in:
Eugen Rochko 2016-10-09 14:48:43 +02:00
parent 52d7f862d3
commit 22a8801dbc
13 changed files with 92 additions and 10 deletions

View file

@ -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

View file

@ -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

View file

@ -0,0 +1,7 @@
class DomainBlock < ApplicationRecord
validates :domain, presence: true, uniqueness: true
def self.blocked?(domain)
where(domain: domain).exists?
end
end

View file

@ -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

View file

@ -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)

View file

@ -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

View file

@ -0,0 +1,10 @@
class CreateDomainBlocks < ActiveRecord::Migration[5.0]
def change
create_table :domain_blocks do |t|
t.string :domain, null: false, default: ''
t.timestamps
end
add_index :domain_blocks, :domain, unique: true
end
end

View file

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20161006213403) do
ActiveRecord::Schema.define(version: 20161009120834) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -51,6 +51,13 @@ ActiveRecord::Schema.define(version: 20161006213403) do
t.index ["account_id", "target_account_id"], name: "index_blocks_on_account_id_and_target_account_id", unique: true, using: :btree
end
create_table "domain_blocks", force: :cascade do |t|
t.string "domain", default: "", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["domain"], name: "index_domain_blocks_on_domain", unique: true, using: :btree
end
create_table "favourites", force: :cascade do |t|
t.integer "account_id", null: false
t.integer "status_id", null: false

View file

@ -0,0 +1,3 @@
Fabricator(:domain_block) do
domain "MyString"
end

View file

@ -1,6 +1,2 @@
Fabricator(:media_attachment) do
status_id 1
file ""
remote_url "MyString"
account_id 1
end

View file

@ -0,0 +1,5 @@
require 'rails_helper'
RSpec.describe DomainBlock, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end

View file

@ -30,3 +30,7 @@ end
def request_fixture(name)
File.read(File.join(Rails.root, 'spec', 'fixtures', 'requests', name))
end
def attachment_fixture(name)
File.open(File.join(Rails.root, 'spec', 'fixtures', 'files', name))
end

View file

@ -0,0 +1,33 @@
require 'rails_helper'
RSpec.describe BlockDomainService do
let(:bad_account) { Fabricate(:account, username: 'badguy666', domain: 'evil.org') }
let(:bad_status1) { Fabricate(:status, account: bad_account, text: 'You suck') }
let(:bad_status2) { Fabricate(:status, account: bad_account, text: 'Hahaha') }
let(:bad_attachment) { Fabricate(:media_attachment, account: bad_account, status: bad_status2, file: attachment_fixture('attachment.jpg')) }
subject { BlockDomainService.new }
before do
bad_account
bad_status1
bad_status2
bad_attachment
subject.call('evil.org')
end
it 'creates a domain block' do
expect(DomainBlock.blocked?('evil.org')).to be true
end
it 'removes remote accounts from that domain' do
expect(Account.find_remote('badguy666', 'evil.org')).to be_nil
end
it 'removes the remote accounts\'s statuses and media attachments' do
expect { bad_status1.reload }.to raise_exception ActiveRecord::RecordNotFound
expect { bad_status2.reload }.to raise_exception ActiveRecord::RecordNotFound
expect { bad_attachment.reload }.to raise_exception ActiveRecord::RecordNotFound
end
end