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

This commit is contained in:
Claire
2023-06-10 14:56:47 +02:00
20 changed files with 678 additions and 53 deletions

View File

@ -31,31 +31,41 @@ module Admin
@domain_block = DomainBlock.new(resource_params)
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)
@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
@domain_block.errors.delete(:domain)
render :new
else
if existing_domain_block.present?
@domain_block = existing_domain_block
@domain_block.update(resource_params)
end
return render :new
end
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
# Allow transparently upgrading a domain block
if existing_domain_block.present?
@domain_block = existing_domain_block
@domain_block.assign_attributes(resource_params)
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
def 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?)
log_action :update, @domain_block
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
'save' if params[:save]
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

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.cohort": "Sign-up month",
"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.title": "Rate limited",
"alert.unexpected.message": "An unexpected error occurred.",

View File

@ -1309,6 +1309,15 @@ a.sparkline {
&:last-child {
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
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
def perform_previous_total_query
@ -24,13 +26,21 @@ class Admin::Metrics::Measure::InstanceAccountsMeasure < Admin::Metrics::Measure
end
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
SELECT axis.*, (
WITH new_accounts AS (
SELECT accounts.id
FROM accounts
WHERE date_trunc('day', accounts.created_at)::date = axis.period
AND accounts.domain = $3::text
AND #{account_matching_sql}
)
SELECT count(*) FROM new_accounts
) AS value
@ -53,6 +63,6 @@ class Admin::Metrics::Measure::InstanceAccountsMeasure < Admin::Metrics::Measure
end
def params
@params.permit(:domain)
@params.permit(:domain, :include_subdomains)
end
end

View File

@ -16,7 +16,9 @@ class Admin::Metrics::Measure::InstanceFollowersMeasure < Admin::Metrics::Measur
protected
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
def perform_previous_total_query
@ -24,6 +26,14 @@ class Admin::Metrics::Measure::InstanceFollowersMeasure < Admin::Metrics::Measur
end
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
SELECT axis.*, (
WITH new_followers AS (
@ -31,7 +41,7 @@ class Admin::Metrics::Measure::InstanceFollowersMeasure < Admin::Metrics::Measur
FROM follows
INNER JOIN accounts ON follows.account_id = accounts.id
WHERE date_trunc('day', follows.created_at)::date = axis.period
AND accounts.domain = $3::text
AND #{account_matching_sql}
)
SELECT count(*) FROM new_followers
) AS value
@ -54,6 +64,6 @@ class Admin::Metrics::Measure::InstanceFollowersMeasure < Admin::Metrics::Measur
end
def params
@params.permit(:domain)
@params.permit(:domain, :include_subdomains)
end
end

View File

@ -16,7 +16,9 @@ class Admin::Metrics::Measure::InstanceFollowsMeasure < Admin::Metrics::Measure:
protected
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
def perform_previous_total_query
@ -24,6 +26,14 @@ class Admin::Metrics::Measure::InstanceFollowsMeasure < Admin::Metrics::Measure:
end
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
SELECT axis.*, (
WITH new_follows AS (
@ -31,7 +41,7 @@ class Admin::Metrics::Measure::InstanceFollowsMeasure < Admin::Metrics::Measure:
FROM follows
INNER JOIN accounts ON follows.target_account_id = accounts.id
WHERE date_trunc('day', follows.created_at)::date = axis.period
AND accounts.domain = $3::text
AND #{account_matching_sql}
)
SELECT count(*) FROM new_follows
) AS value
@ -54,6 +64,6 @@ class Admin::Metrics::Measure::InstanceFollowsMeasure < Admin::Metrics::Measure:
end
def params
@params.permit(:domain)
@params.permit(:domain, :include_subdomains)
end
end

View File

@ -26,7 +26,9 @@ class Admin::Metrics::Measure::InstanceMediaAttachmentsMeasure < Admin::Metrics:
protected
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
def perform_previous_total_query
@ -34,6 +36,14 @@ class Admin::Metrics::Measure::InstanceMediaAttachmentsMeasure < Admin::Metrics:
end
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
SELECT axis.*, (
WITH new_media_attachments AS (
@ -41,7 +51,7 @@ class Admin::Metrics::Measure::InstanceMediaAttachmentsMeasure < Admin::Metrics:
FROM media_attachments
INNER JOIN accounts ON accounts.id = media_attachments.account_id
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
) AS value
@ -64,6 +74,6 @@ class Admin::Metrics::Measure::InstanceMediaAttachmentsMeasure < Admin::Metrics:
end
def params
@params.permit(:domain)
@params.permit(:domain, :include_subdomains)
end
end

View File

@ -16,7 +16,9 @@ class Admin::Metrics::Measure::InstanceReportsMeasure < Admin::Metrics::Measure:
protected
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
def perform_previous_total_query
@ -24,6 +26,14 @@ class Admin::Metrics::Measure::InstanceReportsMeasure < Admin::Metrics::Measure:
end
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
SELECT axis.*, (
WITH new_reports AS (
@ -31,7 +41,7 @@ class Admin::Metrics::Measure::InstanceReportsMeasure < Admin::Metrics::Measure:
FROM reports
INNER JOIN accounts ON accounts.id = reports.target_account_id
WHERE date_trunc('day', reports.created_at)::date = axis.period
AND accounts.domain = $3::text
AND #{account_matching_sql}
)
SELECT count(*) FROM new_reports
) AS value
@ -54,6 +64,6 @@ class Admin::Metrics::Measure::InstanceReportsMeasure < Admin::Metrics::Measure:
end
def params
@params.permit(:domain)
@params.permit(:domain, :include_subdomains)
end
end

View File

@ -16,7 +16,9 @@ class Admin::Metrics::Measure::InstanceStatusesMeasure < Admin::Metrics::Measure
protected
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
def perform_previous_total_query
@ -24,6 +26,14 @@ class Admin::Metrics::Measure::InstanceStatusesMeasure < Admin::Metrics::Measure
end
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
SELECT axis.*, (
WITH new_statuses AS (
@ -31,7 +41,7 @@ class Admin::Metrics::Measure::InstanceStatusesMeasure < Admin::Metrics::Measure
FROM statuses
INNER JOIN accounts ON accounts.id = statuses.account_id
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
)
SELECT count(*) FROM new_statuses
@ -55,6 +65,6 @@ class Admin::Metrics::Measure::InstanceStatusesMeasure < Admin::Metrics::Measure
end
def params
@params.permit(:domain)
@params.permit(:domain, :include_subdomains)
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