Adding domain blocks

lolsob-rspec
Eugen Rochko 2016-10-09 14:48:43 +02:00
parent f9a3373faa
commit 9bf5a73968
13 changed files with 92 additions and 10 deletions

View File

@ -13,8 +13,9 @@ class Api::SubscriptionsController < ApiController
def update def update
body = request.body.read 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) ProcessFeedService.new.call(body, @account)
head 201 head 201
else else

View File

@ -24,10 +24,10 @@ class Account < ApplicationRecord
validates :note, length: { maximum: 124 }, if: 'local?' validates :note, length: { maximum: 124 }, if: 'local?'
# Timelines # Timelines
has_many :stream_entries, inverse_of: :account has_many :stream_entries, inverse_of: :account, dependent: :destroy
has_many :statuses, inverse_of: :account has_many :statuses, inverse_of: :account, dependent: :destroy
has_many :favourites, inverse_of: :account has_many :favourites, inverse_of: :account, dependent: :destroy
has_many :mentions, inverse_of: :account has_many :mentions, inverse_of: :account, dependent: :destroy
# Follow relations # Follow relations
has_many :active_relationships, class_name: 'Follow', foreign_key: 'account_id', dependent: :destroy 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('@') username, domain = uri.split('@')
return Account.find_local(username) if TagManager.instance.local_domain?(domain) return Account.find_local(username) if TagManager.instance.local_domain?(domain)
return nil if DomainBlock.blocked?(domain)
account = Account.find_remote(username, domain) account = Account.find_remote(username, domain)

View File

@ -13,6 +13,8 @@ class ProcessInteractionService < BaseService
domain = Addressable::URI.parse(url).host domain = Addressable::URI.parse(url).host
account = Account.find_by(username: username, domain: domain) account = Account.find_by(username: username, domain: domain)
return if DomainBlock.blocked?(domain)
if account.nil? if account.nil?
account = follow_remote_account_service.call("#{username}@#{domain}") account = follow_remote_account_service.call("#{username}@#{domain}")
end 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. # 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 # These are extensions that must be enabled in order to support this database
enable_extension "plpgsql" 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 t.index ["account_id", "target_account_id"], name: "index_blocks_on_account_id_and_target_account_id", unique: true, using: :btree
end 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| create_table "favourites", force: :cascade do |t|
t.integer "account_id", null: false t.integer "account_id", null: false
t.integer "status_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 Fabricator(:media_attachment) do
status_id 1
file ""
remote_url "MyString"
account_id 1
end 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) def request_fixture(name)
File.read(File.join(Rails.root, 'spec', 'fixtures', 'requests', name)) File.read(File.join(Rails.root, 'spec', 'fixtures', 'requests', name))
end 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