Domain blocks now have varying severity - auto-suspend vs auto-silence
This commit is contained in:
		
							parent
							
								
									ef2b924679
								
							
						
					
					
						commit
						6d98a73180
					
				| 
						 | 
					@ -1,6 +1,8 @@
 | 
				
			||||||
# frozen_string_literal: true
 | 
					# frozen_string_literal: true
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class DomainBlock < ApplicationRecord
 | 
					class DomainBlock < ApplicationRecord
 | 
				
			||||||
 | 
					  enum severity: [:silence, :suspend]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  validates :domain, presence: true, uniqueness: true
 | 
					  validates :domain, presence: true, uniqueness: true
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  def self.blocked?(domain)
 | 
					  def self.blocked?(domain)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,15 +1,16 @@
 | 
				
			||||||
# frozen_string_literal: true
 | 
					# frozen_string_literal: true
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class BlockDomainService < BaseService
 | 
					class BlockDomainService < BaseService
 | 
				
			||||||
  def call(domain)
 | 
					  def call(domain, severity)
 | 
				
			||||||
    DomainBlock.find_or_create_by!(domain: domain)
 | 
					    DomainBlock.where(domain: domain).first_or_create!(domain: domain, severity: severity)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    Account.where(domain: domain).find_each do |account|
 | 
					    if severity == :silence
 | 
				
			||||||
      if account.subscribed?
 | 
					      Account.where(domain: domain).update_all(silenced: true)
 | 
				
			||||||
        account.subscription(api_subscription_url(account.id)).unsubscribe
 | 
					    else
 | 
				
			||||||
 | 
					      Account.where(domain: domain).find_each do |account|
 | 
				
			||||||
 | 
					        account.subscription(api_subscription_url(account.id)).unsubscribe if account.subscribed?
 | 
				
			||||||
 | 
					        SuspendAccountService.new.call(account)
 | 
				
			||||||
      end
 | 
					      end
 | 
				
			||||||
 | 
					 | 
				
			||||||
      account.destroy!
 | 
					 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -35,12 +35,15 @@ class FollowRemoteAccountService < BaseService
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    Rails.logger.debug "Creating new remote account for #{uri}"
 | 
					    Rails.logger.debug "Creating new remote account for #{uri}"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    domain_block = DomainBlock.find_by(domain: domain)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    account.remote_url  = data.link('http://schemas.google.com/g/2010#updates-from').href
 | 
					    account.remote_url  = data.link('http://schemas.google.com/g/2010#updates-from').href
 | 
				
			||||||
    account.salmon_url  = data.link('salmon').href
 | 
					    account.salmon_url  = data.link('salmon').href
 | 
				
			||||||
    account.url         = data.link('http://webfinger.net/rel/profile-page').href
 | 
					    account.url         = data.link('http://webfinger.net/rel/profile-page').href
 | 
				
			||||||
    account.public_key  = magic_key_to_pem(data.link('magic-public-key').href)
 | 
					    account.public_key  = magic_key_to_pem(data.link('magic-public-key').href)
 | 
				
			||||||
    account.private_key = nil
 | 
					    account.private_key = nil
 | 
				
			||||||
    account.suspended   = true if DomainBlock.blocked?(domain)
 | 
					    account.suspended   = true if domain_block && domain_block.suspend?
 | 
				
			||||||
 | 
					    account.silenced    = true if domain_block && domain_block.silence?
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    xml  = get_feed(account.remote_url)
 | 
					    xml  = get_feed(account.remote_url)
 | 
				
			||||||
    hubs = get_hubs(xml)
 | 
					    hubs = get_hubs(xml)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -18,7 +18,6 @@ class SuspendAccountService < BaseService
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @account.media_attachments.destroy_all
 | 
					    @account.media_attachments.destroy_all
 | 
				
			||||||
    @account.stream_entries.destroy_all
 | 
					    @account.stream_entries.destroy_all
 | 
				
			||||||
    @account.mentions.destroy_all
 | 
					 | 
				
			||||||
    @account.notifications.destroy_all
 | 
					    @account.notifications.destroy_all
 | 
				
			||||||
    @account.favourites.destroy_all
 | 
					    @account.favourites.destroy_all
 | 
				
			||||||
    @account.active_relationships.destroy_all
 | 
					    @account.active_relationships.destroy_all
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -5,10 +5,12 @@
 | 
				
			||||||
  %thead
 | 
					  %thead
 | 
				
			||||||
    %tr
 | 
					    %tr
 | 
				
			||||||
      %th Domain
 | 
					      %th Domain
 | 
				
			||||||
 | 
					      %th Severity
 | 
				
			||||||
  %tbody
 | 
					  %tbody
 | 
				
			||||||
    - @blocks.each do |block|
 | 
					    - @blocks.each do |block|
 | 
				
			||||||
      %tr
 | 
					      %tr
 | 
				
			||||||
        %td
 | 
					        %td
 | 
				
			||||||
          %samp= block.domain
 | 
					          %samp= block.domain
 | 
				
			||||||
 | 
					        %td= block.severity
 | 
				
			||||||
 | 
					
 | 
				
			||||||
= will_paginate @blocks, pagination_options
 | 
					= will_paginate @blocks, pagination_options
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,5 @@
 | 
				
			||||||
 | 
					class AddSeverityToDomainBlocks < ActiveRecord::Migration[5.0]
 | 
				
			||||||
 | 
					  def change
 | 
				
			||||||
 | 
					    add_column :domain_blocks, :severity, :integer, default: 0
 | 
				
			||||||
 | 
					  end
 | 
				
			||||||
 | 
					end
 | 
				
			||||||
| 
						 | 
					@ -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: 20170119214911) do
 | 
					ActiveRecord::Schema.define(version: 20170123162658) 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"
 | 
				
			||||||
| 
						 | 
					@ -58,6 +58,7 @@ ActiveRecord::Schema.define(version: 20170119214911) do
 | 
				
			||||||
    t.string   "domain",     default: "", null: false
 | 
					    t.string   "domain",     default: "", null: false
 | 
				
			||||||
    t.datetime "created_at",              null: false
 | 
					    t.datetime "created_at",              null: false
 | 
				
			||||||
    t.datetime "updated_at",              null: false
 | 
					    t.datetime "updated_at",              null: false
 | 
				
			||||||
 | 
					    t.integer  "severity",   default: 0
 | 
				
			||||||
    t.index ["domain"], name: "index_domain_blocks_on_domain", unique: true, using: :btree
 | 
					    t.index ["domain"], name: "index_domain_blocks_on_domain", unique: true, using: :btree
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -14,7 +14,7 @@ RSpec.describe BlockDomainService do
 | 
				
			||||||
    bad_status2
 | 
					    bad_status2
 | 
				
			||||||
    bad_attachment
 | 
					    bad_attachment
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    subject.call('evil.org')
 | 
					    subject.call('evil.org', :suspend)
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  it 'creates a domain block' do
 | 
					  it 'creates a domain block' do
 | 
				
			||||||
| 
						 | 
					@ -22,7 +22,7 @@ RSpec.describe BlockDomainService do
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  it 'removes remote accounts from that domain' do
 | 
					  it 'removes remote accounts from that domain' do
 | 
				
			||||||
    expect(Account.find_remote('badguy666', 'evil.org')).to be_nil
 | 
					    expect(Account.find_remote('badguy666', 'evil.org').suspended?).to be true
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  it 'removes the remote accounts\'s statuses and media attachments' do
 | 
					  it 'removes the remote accounts\'s statuses and media attachments' do
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue