From 7f0a865b05628fa82ac692ec9a21c418e30dac14 Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Wed, 12 Apr 2017 12:20:44 -0400 Subject: [PATCH] Allow import/export of mutes list (#1541) * Allow export of mutes list * Allow importing of mutes list * Refactor to use Settings::Exports::BaseController and DRY up exports code --- .../settings/exports/base_controller.rb | 23 +++++++++++++++++++ .../exports/blocked_accounts_controller.rb | 12 ++++------ .../exports/following_accounts_controller.rb | 12 ++++------ .../exports/muted_accounts_controller.rb | 13 +++++++++++ .../settings/exports_controller.rb | 1 + app/models/import.rb | 2 +- app/views/settings/exports/show.html.haml | 4 ++++ app/workers/import_worker.rb | 14 +++++++++++ config/locales/en.yml | 2 ++ config/routes.rb | 1 + .../blocked_accounts_controller_spec.rb | 2 +- .../following_accounts_controller_spec.rb | 2 +- .../exports/muted_accounts_controller_spec.rb | 17 ++++++++++++++ 13 files changed, 86 insertions(+), 19 deletions(-) create mode 100644 app/controllers/settings/exports/base_controller.rb create mode 100644 app/controllers/settings/exports/muted_accounts_controller.rb create mode 100644 spec/controllers/settings/exports/muted_accounts_controller_spec.rb diff --git a/app/controllers/settings/exports/base_controller.rb b/app/controllers/settings/exports/base_controller.rb new file mode 100644 index 000000000..0b790959f --- /dev/null +++ b/app/controllers/settings/exports/base_controller.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +module Settings + module Exports + class BaseController < ApplicationController + before_action :authenticate_user! + + def index + export_data = Export.new(export_accounts).to_csv + + respond_to do |format| + format.csv { send_data export_data, filename: export_filename } + end + end + + private + + def export_filename + "#{controller_name}.csv" + end + end + end +end diff --git a/app/controllers/settings/exports/blocked_accounts_controller.rb b/app/controllers/settings/exports/blocked_accounts_controller.rb index 0bf8848b4..9c4bcaa53 100644 --- a/app/controllers/settings/exports/blocked_accounts_controller.rb +++ b/app/controllers/settings/exports/blocked_accounts_controller.rb @@ -2,15 +2,11 @@ module Settings module Exports - class BlockedAccountsController < ApplicationController - before_action :authenticate_user! + class BlockedAccountsController < BaseController + private - def index - export_data = Export.new(current_account.blocking).to_csv - - respond_to do |format| - format.csv { send_data export_data, filename: 'blocking.csv' } - end + def export_accounts + current_account.blocking end end end diff --git a/app/controllers/settings/exports/following_accounts_controller.rb b/app/controllers/settings/exports/following_accounts_controller.rb index a7f4344ca..8d06bcc95 100644 --- a/app/controllers/settings/exports/following_accounts_controller.rb +++ b/app/controllers/settings/exports/following_accounts_controller.rb @@ -2,15 +2,11 @@ module Settings module Exports - class FollowingAccountsController < ApplicationController - before_action :authenticate_user! + class FollowingAccountsController < BaseController + private - def index - export_data = Export.new(current_account.following).to_csv - - respond_to do |format| - format.csv { send_data export_data, filename: 'following.csv' } - end + def export_accounts + current_account.following end end end diff --git a/app/controllers/settings/exports/muted_accounts_controller.rb b/app/controllers/settings/exports/muted_accounts_controller.rb new file mode 100644 index 000000000..a77a9af6d --- /dev/null +++ b/app/controllers/settings/exports/muted_accounts_controller.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +module Settings + module Exports + class MutedAccountsController < BaseController + private + + def export_accounts + current_account.muting + end + end + end +end diff --git a/app/controllers/settings/exports_controller.rb b/app/controllers/settings/exports_controller.rb index e060f03d3..77dea3231 100644 --- a/app/controllers/settings/exports_controller.rb +++ b/app/controllers/settings/exports_controller.rb @@ -9,5 +9,6 @@ class Settings::ExportsController < ApplicationController @total_storage = current_account.media_attachments.sum(:file_file_size) @total_follows = current_account.following.count @total_blocks = current_account.blocking.count + @total_mutes = current_account.muting.count end end diff --git a/app/models/import.rb b/app/models/import.rb index 5384986d8..3013bc50e 100644 --- a/app/models/import.rb +++ b/app/models/import.rb @@ -3,7 +3,7 @@ class Import < ApplicationRecord self.inheritance_column = false - enum type: [:following, :blocking] + enum type: [:following, :blocking, :muting] belongs_to :account diff --git a/app/views/settings/exports/show.html.haml b/app/views/settings/exports/show.html.haml index 432a61b4a..51be40fb6 100644 --- a/app/views/settings/exports/show.html.haml +++ b/app/views/settings/exports/show.html.haml @@ -15,3 +15,7 @@ %th= t('exports.blocks') %td= @total_blocks %td= table_link_to 'download', t('exports.csv'), settings_exports_blocks_path(format: :csv) + %tr + %th= t('exports.mutes') + %td= @total_mutes + %td= table_link_to 'download', t('exports.csv'), settings_exports_mutes_path(format: :csv) diff --git a/app/workers/import_worker.rb b/app/workers/import_worker.rb index 60529c0e1..bb21468e7 100644 --- a/app/workers/import_worker.rb +++ b/app/workers/import_worker.rb @@ -16,6 +16,8 @@ class ImportWorker process_blocks when 'following' process_follows + when 'muting' + process_mutes end @import.destroy @@ -35,6 +37,18 @@ class ImportWorker CSV.new(import_contents).reject(&:blank?) end + def process_mutes + import_rows.each do |row| + begin + target_account = FollowRemoteAccountService.new.call(row.first) + next if target_account.nil? + MuteService.new.call(from_account, target_account) + rescue Goldfinger::Error, HTTP::Error, OpenSSL::SSL::SSLError + next + end + end + end + def process_blocks import_rows.each do |row| begin diff --git a/config/locales/en.yml b/config/locales/en.yml index 802db148c..6e32d2c38 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -76,6 +76,7 @@ en: x_seconds: "%{count}s" exports: blocks: You block + mutes: You mute csv: CSV follows: You follow storage: Media storage @@ -92,6 +93,7 @@ en: types: blocking: Blocking list following: Following list + muting: Muting list upload: Upload landing_strip_html: %{name} is a user on %{domain}. You can follow them or interact with them if you have an account anywhere in the fediverse. If you don't, you can sign up here. media_attachments: diff --git a/config/routes.rb b/config/routes.rb index 99ce1754e..78bf7870c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -57,6 +57,7 @@ Rails.application.routes.draw do namespace :exports, constraints: { format: :csv } do resources :follows, only: :index, controller: :following_accounts resources :blocks, only: :index, controller: :blocked_accounts + resources :mutes, only: :index, controller: :muted_accounts end resource :two_factor_auth, only: [:show, :new, :create] do diff --git a/spec/controllers/settings/exports/blocked_accounts_controller_spec.rb b/spec/controllers/settings/exports/blocked_accounts_controller_spec.rb index 574d4d875..c815bdfec 100644 --- a/spec/controllers/settings/exports/blocked_accounts_controller_spec.rb +++ b/spec/controllers/settings/exports/blocked_accounts_controller_spec.rb @@ -11,7 +11,7 @@ describe Settings::Exports::BlockedAccountsController do expect(response).to have_http_status(:success) expect(response.content_type).to eq 'text/csv' - expect(response.headers['Content-Disposition']).to eq 'attachment; filename="blocking.csv"' + expect(response.headers['Content-Disposition']).to eq 'attachment; filename="blocked_accounts.csv"' end end end diff --git a/spec/controllers/settings/exports/following_accounts_controller_spec.rb b/spec/controllers/settings/exports/following_accounts_controller_spec.rb index bf7680523..a7029709c 100644 --- a/spec/controllers/settings/exports/following_accounts_controller_spec.rb +++ b/spec/controllers/settings/exports/following_accounts_controller_spec.rb @@ -11,7 +11,7 @@ describe Settings::Exports::FollowingAccountsController do expect(response).to have_http_status(:success) expect(response.content_type).to eq 'text/csv' - expect(response.headers['Content-Disposition']).to eq 'attachment; filename="following.csv"' + expect(response.headers['Content-Disposition']).to eq 'attachment; filename="following_accounts.csv"' end end end diff --git a/spec/controllers/settings/exports/muted_accounts_controller_spec.rb b/spec/controllers/settings/exports/muted_accounts_controller_spec.rb new file mode 100644 index 000000000..bb52b6fcf --- /dev/null +++ b/spec/controllers/settings/exports/muted_accounts_controller_spec.rb @@ -0,0 +1,17 @@ +require 'rails_helper' + +describe Settings::Exports::MutedAccountsController do + before do + sign_in Fabricate(:user), scope: :user + end + + describe 'GET #index' do + it 'returns a csv of the muting accounts' do + get :index, format: :csv + + expect(response).to have_http_status(:success) + expect(response.content_type).to eq 'text/csv' + expect(response.headers['Content-Disposition']).to eq 'attachment; filename="muted_accounts.csv"' + end + end +end