Merge commit 'e9385e93e9b4601c87d1f5d6b8ddfd815f7aedcb' into glitch-soc/merge-upstream

pull/62/head
Claire 2023-06-10 14:56:47 +02:00
commit b27a9a5903
20 changed files with 678 additions and 53 deletions

View File

@ -31,31 +31,41 @@ module Admin
@domain_block = DomainBlock.new(resource_params) @domain_block = DomainBlock.new(resource_params)
existing_domain_block = resource_params[:domain].present? ? DomainBlock.rule_for(resource_params[:domain]) : nil existing_domain_block = resource_params[:domain].present? ? DomainBlock.rule_for(resource_params[:domain]) : nil
# Disallow accidentally downgrading a domain block
if existing_domain_block.present? && !@domain_block.stricter_than?(existing_domain_block) if existing_domain_block.present? && !@domain_block.stricter_than?(existing_domain_block)
@domain_block.save @domain_block.save
flash.now[:alert] = I18n.t('admin.domain_blocks.existing_domain_block_html', name: existing_domain_block.domain, unblock_url: admin_domain_block_path(existing_domain_block)).html_safe flash.now[:alert] = I18n.t('admin.domain_blocks.existing_domain_block_html', name: existing_domain_block.domain, unblock_url: admin_domain_block_path(existing_domain_block)).html_safe
@domain_block.errors.delete(:domain) @domain_block.errors.delete(:domain)
render :new return render :new
else end
if existing_domain_block.present?
@domain_block = existing_domain_block
@domain_block.update(resource_params)
end
if @domain_block.save # Allow transparently upgrading a domain block
DomainBlockWorker.perform_async(@domain_block.id) if existing_domain_block.present?
log_action :create, @domain_block @domain_block = existing_domain_block
redirect_to admin_instances_path(limited: '1'), notice: I18n.t('admin.domain_blocks.created_msg') @domain_block.assign_attributes(resource_params)
else end
render :new
end # Require explicit confirmation when suspending
return render :confirm_suspension if requires_confirmation?
if @domain_block.save
DomainBlockWorker.perform_async(@domain_block.id)
log_action :create, @domain_block
redirect_to admin_instances_path(limited: '1'), notice: I18n.t('admin.domain_blocks.created_msg')
else
render :new
end end
end end
def update def update
authorize :domain_block, :update? authorize :domain_block, :update?
if @domain_block.update(update_params) @domain_block.assign_attributes(update_params)
# Require explicit confirmation when suspending
return render :confirm_suspension if requires_confirmation?
if @domain_block.save
DomainBlockWorker.perform_async(@domain_block.id, @domain_block.severity_previously_changed?) DomainBlockWorker.perform_async(@domain_block.id, @domain_block.severity_previously_changed?)
log_action :update, @domain_block log_action :update, @domain_block
redirect_to admin_instances_path(limited: '1'), notice: I18n.t('admin.domain_blocks.created_msg') redirect_to admin_instances_path(limited: '1'), notice: I18n.t('admin.domain_blocks.created_msg')
@ -92,5 +102,9 @@ module Admin
def action_from_button def action_from_button
'save' if params[:save] 'save' if params[:save]
end end
def requires_confirmation?
@domain_block.valid? && (@domain_block.new_record? || @domain_block.severity_changed?) && @domain_block.severity.to_s == 'suspend' && !params[:confirm]
end
end end
end end

View File

@ -0,0 +1,91 @@
import PropTypes from 'prop-types';
import { PureComponent } from 'react';
import { FormattedNumber, FormattedMessage } from 'react-intl';
import classNames from 'classnames';
import api from 'mastodon/api';
import { Skeleton } from 'mastodon/components/skeleton';
export default class ImpactReport extends PureComponent {
static propTypes = {
domain: PropTypes.string.isRequired,
};
state = {
loading: true,
data: null,
};
componentDidMount () {
const { domain } = this.props;
const params = {
domain: domain,
include_subdomains: true,
};
api().post('/api/v1/admin/measures', {
keys: ['instance_accounts', 'instance_follows', 'instance_followers'],
start_at: null,
end_at: null,
instance_accounts: params,
instance_follows: params,
instance_followers: params,
}).then(res => {
this.setState({
loading: false,
data: res.data,
});
}).catch(err => {
console.error(err);
});
}
render () {
const { loading, data } = this.state;
return (
<div className='dimension'>
<h4><FormattedMessage id='admin.impact_report.title' defaultMessage='Impact summary' /></h4>
<table>
<tbody>
<tr className='dimension__item'>
<td className='dimension__item__key'>
<FormattedMessage id='admin.impact_report.instance_accounts' defaultMessage='Accounts profiles this would delete' />
</td>
<td className='dimension__item__value'>
{loading ? <Skeleton width={60} /> : <FormattedNumber value={data[0].total} />}
</td>
</tr>
<tr className={classNames('dimension__item', { negative: !loading && data[1].total > 0 })}>
<td className='dimension__item__key'>
<FormattedMessage id='admin.impact_report.instance_follows' defaultMessage='Followers their users would lose' />
</td>
<td className='dimension__item__value'>
{loading ? <Skeleton width={60} /> : <FormattedNumber value={data[1].total} />}
</td>
</tr>
<tr className={classNames('dimension__item', { negative: !loading && data[2].total > 0 })}>
<td className='dimension__item__key'>
<FormattedMessage id='admin.impact_report.instance_followers' defaultMessage='Followers our users would lose' />
</td>
<td className='dimension__item__value'>
{loading ? <Skeleton width={60} /> : <FormattedNumber value={data[2].total} />}
</td>
</tr>
</tbody>
</table>
</div>
);
}
}

View File

@ -73,6 +73,10 @@
"admin.dashboard.retention.average": "Average", "admin.dashboard.retention.average": "Average",
"admin.dashboard.retention.cohort": "Sign-up month", "admin.dashboard.retention.cohort": "Sign-up month",
"admin.dashboard.retention.cohort_size": "New users", "admin.dashboard.retention.cohort_size": "New users",
"admin.impact_report.instance_accounts": "Accounts profiles this would delete",
"admin.impact_report.instance_followers": "Followers our users would lose",
"admin.impact_report.instance_follows": "Followers their users would lose",
"admin.impact_report.title": "Impact summary",
"alert.rate_limited.message": "Please retry after {retry_time, time, medium}.", "alert.rate_limited.message": "Please retry after {retry_time, time, medium}.",
"alert.rate_limited.title": "Rate limited", "alert.rate_limited.title": "Rate limited",
"alert.unexpected.message": "An unexpected error occurred.", "alert.unexpected.message": "An unexpected error occurred.",

View File

@ -1309,6 +1309,15 @@ a.sparkline {
&:last-child { &:last-child {
border-bottom: 0; border-bottom: 0;
} }
&.negative {
color: $error-value-color;
font-weight: 700;
.dimension__item__value {
color: $error-value-color;
}
}
} }
} }

View File

@ -16,7 +16,9 @@ class Admin::Metrics::Measure::InstanceAccountsMeasure < Admin::Metrics::Measure
protected protected
def perform_total_query def perform_total_query
Account.where(domain: params[:domain]).count domain = params[:domain]
domain = Instance.by_domain_and_subdomains(params[:domain]).select(:domain) if params[:include_subdomains]
Account.where(domain: domain).count
end end
def perform_previous_total_query def perform_previous_total_query
@ -24,13 +26,21 @@ class Admin::Metrics::Measure::InstanceAccountsMeasure < Admin::Metrics::Measure
end end
def perform_data_query def perform_data_query
account_matching_sql = begin
if params[:include_subdomains]
"accounts.domain IN (SELECT domain FROM instances WHERE reverse('.' || domain) LIKE reverse('.' || $3::text))"
else
'accounts.domain = $3::text'
end
end
sql = <<-SQL.squish sql = <<-SQL.squish
SELECT axis.*, ( SELECT axis.*, (
WITH new_accounts AS ( WITH new_accounts AS (
SELECT accounts.id SELECT accounts.id
FROM accounts FROM accounts
WHERE date_trunc('day', accounts.created_at)::date = axis.period WHERE date_trunc('day', accounts.created_at)::date = axis.period
AND accounts.domain = $3::text AND #{account_matching_sql}
) )
SELECT count(*) FROM new_accounts SELECT count(*) FROM new_accounts
) AS value ) AS value
@ -53,6 +63,6 @@ class Admin::Metrics::Measure::InstanceAccountsMeasure < Admin::Metrics::Measure
end end
def params def params
@params.permit(:domain) @params.permit(:domain, :include_subdomains)
end end
end end

View File

@ -16,7 +16,9 @@ class Admin::Metrics::Measure::InstanceFollowersMeasure < Admin::Metrics::Measur
protected protected
def perform_total_query def perform_total_query
Follow.joins(:account).merge(Account.where(domain: params[:domain])).count domain = params[:domain]
domain = Instance.by_domain_and_subdomains(params[:domain]).select(:domain) if params[:include_subdomains]
Follow.joins(:account).merge(Account.where(domain: domain)).count
end end
def perform_previous_total_query def perform_previous_total_query
@ -24,6 +26,14 @@ class Admin::Metrics::Measure::InstanceFollowersMeasure < Admin::Metrics::Measur
end end
def perform_data_query def perform_data_query
account_matching_sql = begin
if params[:include_subdomains]
"accounts.domain IN (SELECT domain FROM instances WHERE reverse('.' || domain) LIKE reverse('.' || $3::text))"
else
'accounts.domain = $3::text'
end
end
sql = <<-SQL.squish sql = <<-SQL.squish
SELECT axis.*, ( SELECT axis.*, (
WITH new_followers AS ( WITH new_followers AS (
@ -31,7 +41,7 @@ class Admin::Metrics::Measure::InstanceFollowersMeasure < Admin::Metrics::Measur
FROM follows FROM follows
INNER JOIN accounts ON follows.account_id = accounts.id INNER JOIN accounts ON follows.account_id = accounts.id
WHERE date_trunc('day', follows.created_at)::date = axis.period WHERE date_trunc('day', follows.created_at)::date = axis.period
AND accounts.domain = $3::text AND #{account_matching_sql}
) )
SELECT count(*) FROM new_followers SELECT count(*) FROM new_followers
) AS value ) AS value
@ -54,6 +64,6 @@ class Admin::Metrics::Measure::InstanceFollowersMeasure < Admin::Metrics::Measur
end end
def params def params
@params.permit(:domain) @params.permit(:domain, :include_subdomains)
end end
end end

View File

@ -16,7 +16,9 @@ class Admin::Metrics::Measure::InstanceFollowsMeasure < Admin::Metrics::Measure:
protected protected
def perform_total_query def perform_total_query
Follow.joins(:target_account).merge(Account.where(domain: params[:domain])).count domain = params[:domain]
domain = Instance.by_domain_and_subdomains(params[:domain]).select(:domain) if params[:include_subdomains]
Follow.joins(:target_account).merge(Account.where(domain: domain)).count
end end
def perform_previous_total_query def perform_previous_total_query
@ -24,6 +26,14 @@ class Admin::Metrics::Measure::InstanceFollowsMeasure < Admin::Metrics::Measure:
end end
def perform_data_query def perform_data_query
account_matching_sql = begin
if params[:include_subdomains]
"accounts.domain IN (SELECT domain FROM instances WHERE reverse('.' || domain) LIKE reverse('.' || $3::text))"
else
'accounts.domain = $3::text'
end
end
sql = <<-SQL.squish sql = <<-SQL.squish
SELECT axis.*, ( SELECT axis.*, (
WITH new_follows AS ( WITH new_follows AS (
@ -31,7 +41,7 @@ class Admin::Metrics::Measure::InstanceFollowsMeasure < Admin::Metrics::Measure:
FROM follows FROM follows
INNER JOIN accounts ON follows.target_account_id = accounts.id INNER JOIN accounts ON follows.target_account_id = accounts.id
WHERE date_trunc('day', follows.created_at)::date = axis.period WHERE date_trunc('day', follows.created_at)::date = axis.period
AND accounts.domain = $3::text AND #{account_matching_sql}
) )
SELECT count(*) FROM new_follows SELECT count(*) FROM new_follows
) AS value ) AS value
@ -54,6 +64,6 @@ class Admin::Metrics::Measure::InstanceFollowsMeasure < Admin::Metrics::Measure:
end end
def params def params
@params.permit(:domain) @params.permit(:domain, :include_subdomains)
end end
end end

View File

@ -26,7 +26,9 @@ class Admin::Metrics::Measure::InstanceMediaAttachmentsMeasure < Admin::Metrics:
protected protected
def perform_total_query def perform_total_query
MediaAttachment.joins(:account).merge(Account.where(domain: params[:domain])).sum('COALESCE(file_file_size, 0) + COALESCE(thumbnail_file_size, 0)') domain = params[:domain]
domain = Instance.by_domain_and_subdomains(params[:domain]).select(:domain) if params[:include_subdomains]
MediaAttachment.joins(:account).merge(Account.where(domain: domain)).sum('COALESCE(file_file_size, 0) + COALESCE(thumbnail_file_size, 0)')
end end
def perform_previous_total_query def perform_previous_total_query
@ -34,6 +36,14 @@ class Admin::Metrics::Measure::InstanceMediaAttachmentsMeasure < Admin::Metrics:
end end
def perform_data_query def perform_data_query
account_matching_sql = begin
if params[:include_subdomains]
"accounts.domain IN (SELECT domain FROM instances WHERE reverse('.' || domain) LIKE reverse('.' || $3::text))"
else
'accounts.domain = $3::text'
end
end
sql = <<-SQL.squish sql = <<-SQL.squish
SELECT axis.*, ( SELECT axis.*, (
WITH new_media_attachments AS ( WITH new_media_attachments AS (
@ -41,7 +51,7 @@ class Admin::Metrics::Measure::InstanceMediaAttachmentsMeasure < Admin::Metrics:
FROM media_attachments FROM media_attachments
INNER JOIN accounts ON accounts.id = media_attachments.account_id INNER JOIN accounts ON accounts.id = media_attachments.account_id
WHERE date_trunc('day', media_attachments.created_at)::date = axis.period WHERE date_trunc('day', media_attachments.created_at)::date = axis.period
AND accounts.domain = $3::text AND #{account_matching_sql}
) )
SELECT SUM(size) FROM new_media_attachments SELECT SUM(size) FROM new_media_attachments
) AS value ) AS value
@ -64,6 +74,6 @@ class Admin::Metrics::Measure::InstanceMediaAttachmentsMeasure < Admin::Metrics:
end end
def params def params
@params.permit(:domain) @params.permit(:domain, :include_subdomains)
end end
end end

View File

@ -16,7 +16,9 @@ class Admin::Metrics::Measure::InstanceReportsMeasure < Admin::Metrics::Measure:
protected protected
def perform_total_query def perform_total_query
Report.where(target_account: Account.where(domain: params[:domain])).count domain = params[:domain]
domain = Instance.by_domain_and_subdomains(params[:domain]).select(:domain) if params[:include_subdomains]
Report.where(target_account: Account.where(domain: domain)).count
end end
def perform_previous_total_query def perform_previous_total_query
@ -24,6 +26,14 @@ class Admin::Metrics::Measure::InstanceReportsMeasure < Admin::Metrics::Measure:
end end
def perform_data_query def perform_data_query
account_matching_sql = begin
if params[:include_subdomains]
"accounts.domain IN (SELECT domain FROM instances WHERE reverse('.' || domain) LIKE reverse('.' || $3::text))"
else
'accounts.domain = $3::text'
end
end
sql = <<-SQL.squish sql = <<-SQL.squish
SELECT axis.*, ( SELECT axis.*, (
WITH new_reports AS ( WITH new_reports AS (
@ -31,7 +41,7 @@ class Admin::Metrics::Measure::InstanceReportsMeasure < Admin::Metrics::Measure:
FROM reports FROM reports
INNER JOIN accounts ON accounts.id = reports.target_account_id INNER JOIN accounts ON accounts.id = reports.target_account_id
WHERE date_trunc('day', reports.created_at)::date = axis.period WHERE date_trunc('day', reports.created_at)::date = axis.period
AND accounts.domain = $3::text AND #{account_matching_sql}
) )
SELECT count(*) FROM new_reports SELECT count(*) FROM new_reports
) AS value ) AS value
@ -54,6 +64,6 @@ class Admin::Metrics::Measure::InstanceReportsMeasure < Admin::Metrics::Measure:
end end
def params def params
@params.permit(:domain) @params.permit(:domain, :include_subdomains)
end end
end end

View File

@ -16,7 +16,9 @@ class Admin::Metrics::Measure::InstanceStatusesMeasure < Admin::Metrics::Measure
protected protected
def perform_total_query def perform_total_query
Status.joins(:account).merge(Account.where(domain: params[:domain])).count domain = params[:domain]
domain = Instance.by_domain_and_subdomains(params[:domain]).select(:domain) if params[:include_subdomains]
Status.joins(:account).merge(Account.where(domain: domain)).count
end end
def perform_previous_total_query def perform_previous_total_query
@ -24,6 +26,14 @@ class Admin::Metrics::Measure::InstanceStatusesMeasure < Admin::Metrics::Measure
end end
def perform_data_query def perform_data_query
account_matching_sql = begin
if params[:include_subdomains]
"accounts.domain IN (SELECT domain FROM instances WHERE reverse('.' || domain) LIKE reverse('.' || $5::text))"
else
'accounts.domain = $5::text'
end
end
sql = <<-SQL.squish sql = <<-SQL.squish
SELECT axis.*, ( SELECT axis.*, (
WITH new_statuses AS ( WITH new_statuses AS (
@ -31,7 +41,7 @@ class Admin::Metrics::Measure::InstanceStatusesMeasure < Admin::Metrics::Measure
FROM statuses FROM statuses
INNER JOIN accounts ON accounts.id = statuses.account_id INNER JOIN accounts ON accounts.id = statuses.account_id
WHERE statuses.id BETWEEN $3 AND $4 WHERE statuses.id BETWEEN $3 AND $4
AND accounts.domain = $5::text AND #{account_matching_sql}
AND date_trunc('day', statuses.created_at)::date = axis.period AND date_trunc('day', statuses.created_at)::date = axis.period
) )
SELECT count(*) FROM new_statuses SELECT count(*) FROM new_statuses
@ -55,6 +65,6 @@ class Admin::Metrics::Measure::InstanceStatusesMeasure < Admin::Metrics::Measure
end end
def params def params
@params.permit(:domain) @params.permit(:domain, :include_subdomains)
end end
end end

View File

@ -0,0 +1,22 @@
- content_for :page_title do
= t('.title', domain: Addressable::IDNA.to_unicode(@domain_block.domain))
= simple_form_for @domain_block, url: admin_domain_blocks_path(@domain_block) do |f|
%p.hint= t('.preamble_html', domain: Addressable::IDNA.to_unicode(@domain_block.domain))
%ul.hint
%li= t('.stop_communication')
%li= t('.remove_all_data')
%li= t('.undo_relationships')
%li.negative-hint= t('.permanent_action')
- %i(domain severity reject_media reject_reports obfuscate private_comment public_comment).each do |key|
= f.hidden_field key
%hr.spacer
= react_admin_component :impact_report, domain: @domain_block.domain
.actions
= link_to t('.cancel'), admin_instances_path, class: 'button button-tertiary'
= f.button :submit, t('.confirm'), class: 'button negative', name: :confirm

View File

@ -382,6 +382,15 @@ en:
undo: Disallow federation with domain undo: Disallow federation with domain
domain_blocks: domain_blocks:
add_new: Add new domain block add_new: Add new domain block
confirm_suspension:
cancel: Cancel
confirm: Suspend
permanent_action: Undoing the suspension will not restore any data or relationship.
preamble_html: You are about to suspend <strong>%{domain}</strong> and its subdomains.
remove_all_data: This will remove all content, media, and profile data for this domain's accounts from your server.
stop_communication: Your server will stop communicating with these servers.
title: Confirm domain block for %{domain}
undo_relationships: This will undo any follow relationship between accounts of these servers and yours.
created_msg: Domain block is now being processed created_msg: Domain block is now being processed
destroyed_msg: Domain block has been undone destroyed_msg: Domain block has been undone
domain: Domain domain: Domain

View File

@ -40,42 +40,135 @@ RSpec.describe Admin::DomainBlocksController do
end end
describe 'POST #create' do describe 'POST #create' do
it 'blocks the domain when succeeded to save' do before do
allow(DomainBlockWorker).to receive(:perform_async).and_return(true) allow(DomainBlockWorker).to receive(:perform_async).and_return(true)
post :create, params: { domain_block: { domain: 'example.com', severity: 'silence' } }
expect(DomainBlockWorker).to have_received(:perform_async)
expect(flash[:notice]).to eq I18n.t('admin.domain_blocks.created_msg')
expect(response).to redirect_to(admin_instances_path(limited: '1'))
end end
it 'renders new when failed to save' do context 'with "silence" severity and no conflict' do
Fabricate(:domain_block, domain: 'example.com', severity: 'suspend') before do
allow(DomainBlockWorker).to receive(:perform_async).and_return(true) post :create, params: { domain_block: { domain: 'example.com', severity: 'silence' } }
end
post :create, params: { domain_block: { domain: 'example.com', severity: 'silence' } } it 'records a block' do
expect(DomainBlock.exists?(domain: 'example.com', severity: 'silence')).to be true
end
expect(DomainBlockWorker).to_not have_received(:perform_async) it 'calls DomainBlockWorker' do
expect(response).to render_template :new expect(DomainBlockWorker).to have_received(:perform_async)
end
it 'redirects with a success message' do
expect(flash[:notice]).to eq I18n.t('admin.domain_blocks.created_msg')
expect(response).to redirect_to(admin_instances_path(limited: '1'))
end
end end
it 'allows upgrading a block' do context 'when the new domain block conflicts with an existing one' do
Fabricate(:domain_block, domain: 'example.com', severity: 'silence') before do
allow(DomainBlockWorker).to receive(:perform_async).and_return(true) Fabricate(:domain_block, domain: 'example.com', severity: 'suspend')
post :create, params: { domain_block: { domain: 'example.com', severity: 'silence' } }
end
post :create, params: { domain_block: { domain: 'example.com', severity: 'silence', reject_media: true, reject_reports: true } } it 'does not record a block' do
expect(DomainBlock.exists?(domain: 'example.com', severity: 'silence')).to be false
end
expect(DomainBlockWorker).to have_received(:perform_async) it 'does not call DomainBlockWorker' do
expect(flash[:notice]).to eq I18n.t('admin.domain_blocks.created_msg') expect(DomainBlockWorker).to_not have_received(:perform_async)
expect(response).to redirect_to(admin_instances_path(limited: '1')) end
it 'renders new' do
expect(response).to render_template :new
end
end
context 'with "suspend" severity and no conflict' do
context 'without a confirmation' do
before do
post :create, params: { domain_block: { domain: 'example.com', severity: 'suspend', reject_media: true, reject_reports: true } }
end
it 'does not record a block' do
expect(DomainBlock.exists?(domain: 'example.com', severity: 'suspend')).to be false
end
it 'does not call DomainBlockWorker' do
expect(DomainBlockWorker).to_not have_received(:perform_async)
end
it 'renders confirm_suspension' do
expect(response).to render_template :confirm_suspension
end
end
context 'with a confirmation' do
before do
post :create, params: { :domain_block => { domain: 'example.com', severity: 'suspend', reject_media: true, reject_reports: true }, 'confirm' => '' }
end
it 'records a block' do
expect(DomainBlock.exists?(domain: 'example.com', severity: 'suspend')).to be true
end
it 'calls DomainBlockWorker' do
expect(DomainBlockWorker).to have_received(:perform_async)
end
it 'redirects with a success message' do
expect(flash[:notice]).to eq I18n.t('admin.domain_blocks.created_msg')
expect(response).to redirect_to(admin_instances_path(limited: '1'))
end
end
end
context 'when upgrading an existing block' do
before do
Fabricate(:domain_block, domain: 'example.com', severity: 'silence')
end
context 'without a confirmation' do
before do
post :create, params: { domain_block: { domain: 'example.com', severity: 'suspend', reject_media: true, reject_reports: true } }
end
it 'does not record a block' do
expect(DomainBlock.exists?(domain: 'example.com', severity: 'suspend')).to be false
end
it 'does not call DomainBlockWorker' do
expect(DomainBlockWorker).to_not have_received(:perform_async)
end
it 'renders confirm_suspension' do
expect(response).to render_template :confirm_suspension
end
end
context 'with a confirmation' do
before do
post :create, params: { :domain_block => { domain: 'example.com', severity: 'suspend', reject_media: true, reject_reports: true }, 'confirm' => '' }
end
it 'updates the record' do
expect(DomainBlock.exists?(domain: 'example.com', severity: 'suspend')).to be true
end
it 'calls DomainBlockWorker' do
expect(DomainBlockWorker).to have_received(:perform_async)
end
it 'redirects with a success message' do
expect(flash[:notice]).to eq I18n.t('admin.domain_blocks.created_msg')
expect(response).to redirect_to(admin_instances_path(limited: '1'))
end
end
end end
end end
describe 'PUT #update' do describe 'PUT #update' do
let!(:remote_account) { Fabricate(:account, domain: 'example.com') } let!(:remote_account) { Fabricate(:account, domain: 'example.com') }
let(:subject) do let(:subject) do
post :update, params: { id: domain_block.id, domain_block: { domain: 'example.com', severity: new_severity } } post :update, params: { :id => domain_block.id, :domain_block => { domain: 'example.com', severity: new_severity }, 'confirm' => '' }
end end
let(:domain_block) { Fabricate(:domain_block, domain: 'example.com', severity: original_severity) } let(:domain_block) { Fabricate(:domain_block, domain: 'example.com', severity: original_severity) }

View File

@ -0,0 +1,78 @@
# frozen_string_literal: true
require 'rails_helper'
describe 'blocking domains through the moderation interface' do
before do
sign_in Fabricate(:user, role: UserRole.find_by(name: 'Admin')), scope: :user
end
context 'when silencing a new domain' do
it 'adds a new domain block' do
visit new_admin_domain_block_path
fill_in 'domain_block_domain', with: 'example.com'
select I18n.t('admin.domain_blocks.new.severity.silence'), from: 'domain_block_severity'
click_on I18n.t('admin.domain_blocks.new.create')
expect(DomainBlock.exists?(domain: 'example.com', severity: 'silence')).to be true
end
end
context 'when suspending a new domain' do
it 'presents a confirmation screen before suspending the domain' do
visit new_admin_domain_block_path
fill_in 'domain_block_domain', with: 'example.com'
select I18n.t('admin.domain_blocks.new.severity.suspend'), from: 'domain_block_severity'
click_on I18n.t('admin.domain_blocks.new.create')
# It presents a confirmation screen
expect(page).to have_title(I18n.t('admin.domain_blocks.confirm_suspension.title', domain: 'example.com'))
# Confirming creates a block
click_on I18n.t('admin.domain_blocks.confirm_suspension.confirm')
expect(DomainBlock.exists?(domain: 'example.com', severity: 'suspend')).to be true
end
end
context 'when suspending a domain that is already silenced' do
it 'presents a confirmation screen before suspending the domain' do
domain_block = Fabricate(:domain_block, domain: 'example.com', severity: 'silence')
visit new_admin_domain_block_path
fill_in 'domain_block_domain', with: 'example.com'
select I18n.t('admin.domain_blocks.new.severity.suspend'), from: 'domain_block_severity'
click_on I18n.t('admin.domain_blocks.new.create')
# It presents a confirmation screen
expect(page).to have_title(I18n.t('admin.domain_blocks.confirm_suspension.title', domain: 'example.com'))
# Confirming updates the block
click_on I18n.t('admin.domain_blocks.confirm_suspension.confirm')
expect(domain_block.reload.severity).to eq 'silence'
end
end
context 'when editing a domain block' do
it 'presents a confirmation screen before suspending the domain' do
domain_block = Fabricate(:domain_block, domain: 'example.com', severity: 'silence')
visit edit_admin_domain_block_path(domain_block)
select I18n.t('admin.domain_blocks.new.severity.suspend'), from: 'domain_block_severity'
click_on I18n.t('generic.save_changes')
# It presents a confirmation screen
expect(page).to have_title(I18n.t('admin.domain_blocks.confirm_suspension.title', domain: 'example.com'))
# Confirming updates the block
click_on I18n.t('admin.domain_blocks.confirm_suspension.confirm')
expect(domain_block.reload.severity).to eq 'silence'
end
end
end

View File

@ -0,0 +1,40 @@
# frozen_string_literal: true
require 'rails_helper'
describe Admin::Metrics::Measure::InstanceAccountsMeasure do
subject(:measure) { described_class.new(start_at, end_at, params) }
let(:domain) { 'example.com' }
let(:start_at) { 2.days.ago }
let(:end_at) { Time.now.utc }
let(:params) { ActionController::Parameters.new(domain: domain) }
before do
Fabricate(:account, domain: domain, created_at: 1.year.ago)
Fabricate(:account, domain: domain, created_at: 1.month.ago)
Fabricate(:account, domain: domain)
Fabricate(:account, domain: "foo.#{domain}", created_at: 1.year.ago)
Fabricate(:account, domain: "foo.#{domain}")
Fabricate(:account, domain: "bar.#{domain}")
end
describe 'total' do
context 'without include_subdomains' do
it 'returns the expected number of accounts' do
expect(measure.total).to eq 3
end
end
context 'with include_subdomains' do
let(:params) { ActionController::Parameters.new(domain: domain, include_subdomains: 'true') }
it 'returns the expected number of accounts' do
expect(measure.total).to eq 6
end
end
end
end

View File

@ -0,0 +1,42 @@
# frozen_string_literal: true
require 'rails_helper'
describe Admin::Metrics::Measure::InstanceFollowersMeasure do
subject(:measure) { described_class.new(start_at, end_at, params) }
let(:domain) { 'example.com' }
let(:start_at) { 2.days.ago }
let(:end_at) { Time.now.utc }
let(:params) { ActionController::Parameters.new(domain: domain) }
before do
local_account = Fabricate(:account)
Fabricate(:account, domain: domain).follow!(local_account)
Fabricate(:account, domain: domain).follow!(local_account)
Fabricate(:account, domain: domain)
Fabricate(:account, domain: "foo.#{domain}").follow!(local_account)
Fabricate(:account, domain: "foo.#{domain}").follow!(local_account)
Fabricate(:account, domain: "bar.#{domain}")
end
describe 'total' do
context 'without include_subdomains' do
it 'returns the expected number of accounts' do
expect(measure.total).to eq 2
end
end
context 'with include_subdomains' do
let(:params) { ActionController::Parameters.new(domain: domain, include_subdomains: 'true') }
it 'returns the expected number of accounts' do
expect(measure.total).to eq 4
end
end
end
end

View File

@ -0,0 +1,42 @@
# frozen_string_literal: true
require 'rails_helper'
describe Admin::Metrics::Measure::InstanceFollowsMeasure do
subject(:measure) { described_class.new(start_at, end_at, params) }
let(:domain) { 'example.com' }
let(:start_at) { 2.days.ago }
let(:end_at) { Time.now.utc }
let(:params) { ActionController::Parameters.new(domain: domain) }
before do
local_account = Fabricate(:account)
local_account.follow!(Fabricate(:account, domain: domain))
local_account.follow!(Fabricate(:account, domain: domain))
Fabricate(:account, domain: domain)
local_account.follow!(Fabricate(:account, domain: "foo.#{domain}"))
local_account.follow!(Fabricate(:account, domain: "foo.#{domain}"))
Fabricate(:account, domain: "bar.#{domain}")
end
describe 'total' do
context 'without include_subdomains' do
it 'returns the expected number of accounts' do
expect(measure.total).to eq 2
end
end
context 'with include_subdomains' do
let(:params) { ActionController::Parameters.new(domain: domain, include_subdomains: 'true') }
it 'returns the expected number of accounts' do
expect(measure.total).to eq 4
end
end
end
end

View File

@ -0,0 +1,43 @@
# frozen_string_literal: true
require 'rails_helper'
describe Admin::Metrics::Measure::InstanceMediaAttachmentsMeasure do
subject(:measure) { described_class.new(start_at, end_at, params) }
let(:domain) { 'example.com' }
let(:start_at) { 2.days.ago }
let(:end_at) { Time.now.utc }
let(:params) { ActionController::Parameters.new(domain: domain) }
let(:remote_account) { Fabricate(:account, domain: domain) }
let(:remote_account_on_subdomain) { Fabricate(:account, domain: "foo.#{domain}") }
before do
remote_account.media_attachments.create!(file: attachment_fixture('attachment.jpg'))
remote_account_on_subdomain.media_attachments.create!(file: attachment_fixture('attachment.jpg'))
end
describe 'total' do
context 'without include_subdomains' do
it 'returns the expected number of accounts' do
expected_total = remote_account.media_attachments.sum(:file_file_size) + remote_account.media_attachments.sum(:thumbnail_file_size)
expect(measure.total).to eq expected_total
end
end
context 'with include_subdomains' do
let(:params) { ActionController::Parameters.new(domain: domain, include_subdomains: 'true') }
it 'returns the expected number of accounts' do
expected_total = [remote_account, remote_account_on_subdomain].sum do |account|
account.media_attachments.sum(:file_file_size) + account.media_attachments.sum(:thumbnail_file_size)
end
expect(measure.total).to eq expected_total
end
end
end
end

View File

@ -0,0 +1,39 @@
# frozen_string_literal: true
require 'rails_helper'
describe Admin::Metrics::Measure::InstanceReportsMeasure do
subject(:measure) { described_class.new(start_at, end_at, params) }
let(:domain) { 'example.com' }
let(:start_at) { 2.days.ago }
let(:end_at) { Time.now.utc }
let(:params) { ActionController::Parameters.new(domain: domain) }
before do
Fabricate(:report, target_account: Fabricate(:account, domain: domain))
Fabricate(:report, target_account: Fabricate(:account, domain: domain))
Fabricate(:report, target_account: Fabricate(:account, domain: "foo.#{domain}"))
Fabricate(:report, target_account: Fabricate(:account, domain: "foo.#{domain}"))
Fabricate(:report, target_account: Fabricate(:account, domain: "bar.#{domain}"))
end
describe 'total' do
context 'without include_subdomains' do
it 'returns the expected number of accounts' do
expect(measure.total).to eq 2
end
end
context 'with include_subdomains' do
let(:params) { ActionController::Parameters.new(domain: domain, include_subdomains: 'true') }
it 'returns the expected number of accounts' do
expect(measure.total).to eq 5
end
end
end
end

View File

@ -0,0 +1,39 @@
# frozen_string_literal: true
require 'rails_helper'
describe Admin::Metrics::Measure::InstanceStatusesMeasure do
subject(:measure) { described_class.new(start_at, end_at, params) }
let(:domain) { 'example.com' }
let(:start_at) { 2.days.ago }
let(:end_at) { Time.now.utc }
let(:params) { ActionController::Parameters.new(domain: domain) }
before do
Fabricate(:status, account: Fabricate(:account, domain: domain))
Fabricate(:status, account: Fabricate(:account, domain: domain))
Fabricate(:status, account: Fabricate(:account, domain: "foo.#{domain}"))
Fabricate(:status, account: Fabricate(:account, domain: "foo.#{domain}"))
Fabricate(:status, account: Fabricate(:account, domain: "bar.#{domain}"))
end
describe 'total' do
context 'without include_subdomains' do
it 'returns the expected number of accounts' do
expect(measure.total).to eq 2
end
end
context 'with include_subdomains' do
let(:params) { ActionController::Parameters.new(domain: domain, include_subdomains: 'true') }
it 'returns the expected number of accounts' do
expect(measure.total).to eq 5
end
end
end
end