Compare commits

...

12 Commits

Author SHA1 Message Date
Pia B
ce707cb566 change tests 2026-05-20 17:17:13 +02:00
Pia B
4338004dcc add lost model 2026-05-20 10:28:13 +02:00
Pia B
536ad6dde2 make i18n happy 2026-05-20 09:54:21 +02:00
Pia B
dfa267bb1b make i18n happy 2026-05-20 09:15:52 +02:00
Pia B
e1e8e3e48f make i18n happy 2026-05-19 17:25:29 +02:00
Pia B
529c6495ae add text resource 2026-05-19 17:20:21 +02:00
Pia B
f092934301 cleanup spec 2026-05-19 17:06:48 +02:00
Pia B
454e8f15c1 add controller and request spec 2026-05-19 17:05:53 +02:00
Pia B
9d4ad520ce add view 2026-05-19 17:05:40 +02:00
Pia B
7ed8ef71e7 add route 2026-05-19 17:05:31 +02:00
Pia B
cd610c1c05 add to controller concern and spec 2026-05-19 17:05:23 +02:00
Pia B
dfde083365 add to export summary and spec 2026-05-19 17:05:15 +02:00
10 changed files with 135 additions and 7 deletions

View File

@@ -19,15 +19,12 @@ module Settings::ExportControllerConcern
def send_export_file
respond_to do |format|
format.csv { send_data export_data, filename: export_filename }
format.csv { send_data export_data, filename: "#{controller_name}.csv" }
format.json { send_data export_data, filename: "#{controller_name}.json" }
end
end
def export_data
raise 'Override in controller'
end
def export_filename
"#{controller_name}.csv"
end
end

View File

@@ -0,0 +1,19 @@
# frozen_string_literal: true
module Settings
module Exports
class CustomFiltersController < BaseController
include Settings::ExportControllerConcern
def index
send_export_file
end
private
def export_data
@export.to_custom_filters_json
end
end
end
end

View File

@@ -55,6 +55,27 @@ class Export
end
end
def to_custom_filters_json
data_collection = []
account.custom_filters.includes(:keywords, :statuses).order(:phrase).each do |filter|
keywords_attributes = []
filter.keywords.map do |k|
keywords_attributes << { keyword: k.keyword, whole_word: k.whole_word }
end
data_collection << {
title: filter.title,
expire_at: filter.expires_at,
context: filter.context,
action: filter.action,
keywords_attributes: keywords_attributes,
statuses: filter.statuses.map { |s| s.status&.text },
}
end
JSON.generate(data_collection)
end
private
def to_csv(accounts)

View File

@@ -6,6 +6,7 @@ class ExportSummary
delegate(
:blocking,
:bookmarks,
:custom_filters,
:domain_blocks,
:owned_lists,
:media_attachments,
@@ -27,6 +28,10 @@ class ExportSummary
counts[:bookmarks].value
end
def total_custom_filters
counts[:custom_filters].value
end
def total_domain_blocks
counts[:domain_blocks].value
end
@@ -61,6 +66,7 @@ class ExportSummary
{
blocks: account_blocking.async_count,
bookmarks: account_bookmarks.async_count,
custom_filters: account_custom_filters.async_count,
domain_blocks: account_domain_blocks.async_count,
owned_lists: account_owned_lists.async_count,
muting: account_muting.async_count,

View File

@@ -40,6 +40,10 @@
%th= t('exports.bookmarks')
%td= number_with_delimiter @export_summary.total_bookmarks
%td= table_link_to 'download', t('exports.csv'), settings_exports_bookmarks_path(format: :csv)
%tr
%th= t('exports.custom_filters')
%td= number_with_delimiter @export_summary.total_custom_filters
%td= table_link_to 'download', t('exports.json'), settings_exports_custom_filters_path(format: :json)
%hr.spacer/

View File

@@ -1562,7 +1562,9 @@ en:
blocks: You block
bookmarks: Bookmarks
csv: CSV
custom_filters: Filters
domain_blocks: Domain blocks
json: JSON
lists: Lists
mutes: You mute
storage: Media storage

View File

@@ -30,6 +30,7 @@ namespace :settings do
resources :lists, only: :index
resources :domain_blocks, only: :index, controller: :blocked_domains
resources :bookmarks, only: :index
resources :custom_filters, only: :index, constraints: { format: :json }, controller: :custom_filters
end
resources :two_factor_authentication_methods, only: [:index] do

View File

@@ -15,10 +15,13 @@ RSpec.describe Settings::ExportControllerConcern do
end
end
def sign_in_user
sign_in Fabricate(:user)
end
describe 'GET #index' do
it 'returns a csv of the exported data when signed in' do
user = Fabricate(:user)
sign_in user
sign_in_user
get :index, format: :csv
expect(response).to have_http_status(200)
@@ -33,4 +36,26 @@ RSpec.describe Settings::ExportControllerConcern do
expect(response).to have_http_status(401)
end
end
context 'when with json format' do
it 'returns a json of the exported data when signed in' do
sign_in_user
get :index, format: :json
expect(response).to have_http_status(200)
expect(response.media_type).to eq 'application/json'
expect(response)
.to have_attachment('anonymous.json')
expect(response.body).to be_present
end
end
context 'when without format' do
it 'does not send exported data' do
sign_in_user
get :index
expect(response).to have_http_status(406)
end
end
end

View File

@@ -52,6 +52,14 @@ RSpec.describe ExportSummary do
end
end
describe '#total_custom_filters' do
before { Fabricate.times(2, :custom_filter, account: account) }
it 'returns the total number of lists' do
expect(subject.total_custom_filters).to eq(2)
end
end
describe '#total_followers' do
before { target_accounts.each { |target_account| target_account.follow!(account) } }

View File

@@ -0,0 +1,45 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe 'Settings / Exports / CustomFilters' do
describe 'GET /settings/exports/custom_filters' do
context 'with a signed in user who has custom_filters' do
let(:user) { Fabricate(:user) }
let(:filter) { Fabricate(:custom_filter, account: user.account, phrase: 'foo') }
let(:keyword) { Fabricate(:custom_filter_keyword, custom_filter: filter) }
let(:filter_keyword) { Fabricate(:custom_filter_keyword, keyword: 'something', custom_filter: filter, whole_word: false) }
let(:status_filter) { Fabricate(:custom_filter_status, custom_filter: filter) }
let(:create_custom_filters) { [keyword, filter_keyword, status_filter] }
let(:expected_response_body) do
[{
'title' => 'foo',
'expire_at' => nil,
'context' => ['home', 'notifications'],
'action' => 'warn',
'keywords_attributes' => [{
'keyword' => 'discourse',
'whole_word' => true,
}, {
'keyword' => 'something',
'whole_word' => false,
}],
'statuses' => ['Lorem ipsum dolor sit amet'],
}]
end
before do
sign_in user
end
it 'returns a JSON with the custom filters' do
create_custom_filters
get '/settings/exports/custom_filters.json'
expect(response).to have_http_status(200)
expect(response.content_type).to eq('application/json')
expect(response.parsed_body).to eq(expected_response_body)
end
end
end
end