From 331bf9f1355b50da76196db05e678c7beca68f95 Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Sat, 2 Jul 2022 06:15:00 +0200 Subject: [PATCH 1/8] Move export actions into `Settings::ExportController` --- app/controllers/settings/export_controller.rb | 22 +++++++++++++++++++ app/controllers/user_controller.rb | 19 +--------------- config/routes.rb | 5 +++-- 3 files changed, 26 insertions(+), 20 deletions(-) create mode 100644 app/controllers/settings/export_controller.rb diff --git a/app/controllers/settings/export_controller.rb b/app/controllers/settings/export_controller.rb new file mode 100644 index 00000000..6fa05b67 --- /dev/null +++ b/app/controllers/settings/export_controller.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +class Settings::ExportController < ApplicationController + before_action :authenticate_user! + + def index + if current_user.export_processing + flash[:info] = t(".info") + end + end + + def create + if current_user.can_export? + ExportWorker.perform_async(current_user.id) + flash[:success] = t(".success") + else + flash[:error] = t(".error") + end + + redirect_to settings_export_path + end +end diff --git a/app/controllers/user_controller.rb b/app/controllers/user_controller.rb index e475c1eb..1a690087 100644 --- a/app/controllers/user_controller.rb +++ b/app/controllers/user_controller.rb @@ -1,5 +1,5 @@ class UserController < ApplicationController - before_action :authenticate_user!, only: %w[data export begin_export edit_security update_2fa destroy_2fa reset_user_recovery_codes edit_mute edit_blocks] + before_action :authenticate_user!, only: %w[data edit_security update_2fa destroy_2fa reset_user_recovery_codes edit_mute edit_blocks] def show @user = User.where('LOWER(screen_name) = ?', params[:username].downcase).includes(:profile).first! @@ -69,23 +69,6 @@ class UserController < ApplicationController def data end - def export - if current_user.export_processing - flash[:info] = t(".info") - end - end - - def begin_export - if current_user.can_export? - ExportWorker.perform_async(current_user.id) - flash[:success] = t(".success") - else - flash[:error] = t(".error") - end - - redirect_to user_export_path - end - def edit_security if current_user.otp_module_disabled? current_user.otp_secret_key = User.otp_random_secret(25) diff --git a/config/routes.rb b/config/routes.rb index 7ba4b4e3..ed09e9d9 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -73,6 +73,9 @@ Rails.application.routes.draw do get :privacy, to: redirect('/settings/privacy/edit') resource :privacy, controller: :privacy, only: %i[edit update] + + get :export, to: 'export#index' + post :export, to: 'export#create' end resolve('Theme') { [:settings_theme] } # to make link_to/form_for work nicely when passing a `Theme` object to it, see also: https://api.rubyonrails.org/v6.1.5.1/classes/ActionDispatch/Routing/Mapper/CustomUrls.html#method-i-resolve resolve('Profile') { [:settings_profile] } @@ -96,8 +99,6 @@ Rails.application.routes.draw do end match '/settings/data', to: 'user#data', via: :get, as: :user_data - match '/settings/export', to: 'user#export', via: :get, as: :user_export - match '/settings/export', to: 'user#begin_export', via: :post, as: :begin_user_export namespace :ajax do match '/ask', to: 'question#create', via: :post, as: :ask From 7fdb647deb4a6eb0b7b65be93f1610224bdf1ca8 Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Sat, 2 Jul 2022 06:15:50 +0200 Subject: [PATCH 2/8] Move and combine export settings template --- app/views/settings/{_export.haml => export/index.haml} | 5 ++++- app/views/tabs/_settings.haml | 2 +- app/views/user/export.haml | 4 ---- 3 files changed, 5 insertions(+), 6 deletions(-) rename app/views/settings/{_export.haml => export/index.haml} (82%) delete mode 100644 app/views/user/export.haml diff --git a/app/views/settings/_export.haml b/app/views/settings/export/index.haml similarity index 82% rename from app/views/settings/_export.haml rename to app/views/settings/export/index.haml index 8ad94678..76e7b6db 100644 --- a/app/views/settings/_export.haml +++ b/app/views/settings/export/index.haml @@ -4,7 +4,7 @@ = t(".body_html") - if current_user.can_export? - %form{ action: begin_user_export_path, method: :post } + %form{ action: settings_export_path, method: :post } %p.text-center %button.btn.btn-lg.btn-primary#export-btn{ type: :submit }= t(".export") = hidden_field_tag :authenticity_token, form_authenticity_token @@ -18,3 +18,6 @@ = t(".export_url.present", time: current_user.export_created_at) %a{ href: current_user.export_url } = File.basename current_user.export_url + +- provide(:title, generate_title(t(".title"))) +- parent_layout "user/settings" diff --git a/app/views/tabs/_settings.haml b/app/views/tabs/_settings.haml index 6ed1d2cf..920b8965 100644 --- a/app/views/tabs/_settings.haml +++ b/app/views/tabs/_settings.haml @@ -9,6 +9,6 @@ = list_group_item t(".blocks"), edit_user_blocks_path = list_group_item t(".theme"), settings_theme_path = list_group_item t(".data"), user_data_path - = list_group_item t(".export"), user_export_path + = list_group_item t(".export"), settings_export_path .d-none.d-sm-block= render "shared/links" diff --git a/app/views/user/export.haml b/app/views/user/export.haml deleted file mode 100644 index 3f2c9262..00000000 --- a/app/views/user/export.haml +++ /dev/null @@ -1,4 +0,0 @@ -= render "settings/export" - -- provide(:title, generate_title(t(".title"))) -- parent_layout "user/settings" From 2f8f9e1659e1416c2c156c65c318497f96c76bf6 Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Sat, 2 Jul 2022 06:16:21 +0200 Subject: [PATCH 3/8] Update locale namespaces for export settings --- config/locales/controllers.en.yml | 11 ++++++----- config/locales/views.en.yml | 28 ++++++++++++++-------------- 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/config/locales/controllers.en.yml b/config/locales/controllers.en.yml index 4ce8837f..04659dab 100644 --- a/config/locales/controllers.en.yml +++ b/config/locales/controllers.en.yml @@ -40,6 +40,12 @@ en: destroy: success: "Service removed successfully." settings: + export: + index: + info: "An export is currently in progress for this account." + create: + success: "Your account is currently being exported. This will take a little while." + error: "Exporting is currently not possible." privacy: update: success: :settings.profile.update.success @@ -60,11 +66,6 @@ en: success: "Theme saved successfully." error: "Theme saving failed. %{errors}" user: - export: - info: "An export is currently in progress for this account." - begin_export: - success: "Your account is currently being exported. This will take a little while." - error: "Exporting is currently not possible." update_2fa: error: :errors.invalid_otp destroy_2fa: diff --git a/config/locales/views.en.yml b/config/locales/views.en.yml index ce349ad2..9f3a2a30 100644 --- a/config/locales/views.en.yml +++ b/config/locales/views.en.yml @@ -131,17 +131,19 @@ en: retina: "Retina" original: "Original image" export: - heading: "Export your data" - body_html: | -

The data is inside a .tar.gz archive and available in three formats: YAML, JSON, and XML. - The archive also contains a copy of your profile picture and header picture in all sizes.

-

Please note that you can only export your data once a week. Exporting your data - will take a while, so please be patient. You will receive a question once exporting - is done.

- export: "Export" - export_url: - none: "Once exporting your account is done, a download link will appear here." - present: "Here's your export from %{time}" + index: + title: "Export" + heading: "Export your data" + body_html: | +

The data is inside a .tar.gz archive and available in three formats: YAML, JSON, and XML. + The archive also contains a copy of your profile picture and header picture in all sizes.

+

Please note that you can only export your data once a week. Exporting your data + will take a while, so please be patient. You will receive a question once exporting + is done.

+ export: "Export" + export_url: + none: "Once exporting your account is done, a download link will appear here." + present: "Here's your export from %{time}" muted: heading: "Muted words" body: "Muting words (or longer phrases) will prevent questions containing those to appear in your inbox." @@ -336,9 +338,7 @@ en: edit_mute: title: "Muted Words" edit_security: - title: "Security Settings" - export: - title: "Export" + title: "Security Settings" moderation: inbox: header: From b41e5c99de38a8973027abe71005c3859ed0c500 Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Sat, 2 Jul 2022 06:19:39 +0200 Subject: [PATCH 4/8] Fix method name in Exporter user_stub call --- lib/exporter.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/exporter.rb b/lib/exporter.rb index b26ae9e5..b9c11e37 100644 --- a/lib/exporter.rb +++ b/lib/exporter.rb @@ -233,7 +233,7 @@ class Exporter def user_stub(user) uobj = {} %i[answered_count asked_count comment_smiled_count commented_count created_at - id permanently_banned screen_name smiled_count].each do |f| + id permanently_banned? screen_name smiled_count].each do |f| uobj[f] = user.send f end From c5a2d25f9d37e04d8c7c4c2b00c02c97b17fb2d0 Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Sat, 2 Jul 2022 06:26:39 +0200 Subject: [PATCH 5/8] Add test for `Settings::ExportController` index view --- .../settings/export_controller_spec.rb | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 spec/controllers/settings/export_controller_spec.rb diff --git a/spec/controllers/settings/export_controller_spec.rb b/spec/controllers/settings/export_controller_spec.rb new file mode 100644 index 00000000..cebff68f --- /dev/null +++ b/spec/controllers/settings/export_controller_spec.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +require "rails_helper" + +describe Settings::ExportController, type: :controller do + describe "#index" do + subject { get :index } + + context "user signed in" do + let(:user) { FactoryBot.create(:user) } + + before { sign_in user } + + it "renders the index template" do + subject + expect(response).to render_template(:index) + end + end + end +end From b17df72b611c5b2afe88832431f43269eb4de78d Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Sat, 2 Jul 2022 14:43:53 +0200 Subject: [PATCH 6/8] Improve export processing condition on index view --- app/controllers/settings/export_controller.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/app/controllers/settings/export_controller.rb b/app/controllers/settings/export_controller.rb index 6fa05b67..5656e614 100644 --- a/app/controllers/settings/export_controller.rb +++ b/app/controllers/settings/export_controller.rb @@ -4,9 +4,7 @@ class Settings::ExportController < ApplicationController before_action :authenticate_user! def index - if current_user.export_processing - flash[:info] = t(".info") - end + flash[:info] = t(".info") if current_user.export_processing end def create From 0ed0d346823a4bb64e0e41277910d849056dcad3 Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Sat, 2 Jul 2022 14:51:25 +0200 Subject: [PATCH 7/8] Add tests for `ExportController#create` --- .../settings/export_controller_spec.rb | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/spec/controllers/settings/export_controller_spec.rb b/spec/controllers/settings/export_controller_spec.rb index cebff68f..f0a8ce43 100644 --- a/spec/controllers/settings/export_controller_spec.rb +++ b/spec/controllers/settings/export_controller_spec.rb @@ -17,4 +17,24 @@ describe Settings::ExportController, type: :controller do end end end + + describe "#create" do + subject { post :create } + + context "user signed in" do + let(:user) { FactoryBot.create(:user) } + + before { sign_in user } + + it "enqueues an ExportWorker job" do + subject + expect(ExportWorker).to have_enqueued_sidekiq_job(user.id) + end + + it "redirects to the export page" do + subject + expect(response).to redirect_to(:settings_export) + end + end + end end From 2d77a4054831e7e34f7efc31c4019ddd8a164018 Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Sat, 2 Jul 2022 14:52:16 +0200 Subject: [PATCH 8/8] Appease the dog overlords --- spec/controllers/settings/export_controller_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/controllers/settings/export_controller_spec.rb b/spec/controllers/settings/export_controller_spec.rb index f0a8ce43..97cdae63 100644 --- a/spec/controllers/settings/export_controller_spec.rb +++ b/spec/controllers/settings/export_controller_spec.rb @@ -30,7 +30,7 @@ describe Settings::ExportController, type: :controller do subject expect(ExportWorker).to have_enqueued_sidekiq_job(user.id) end - + it "redirects to the export page" do subject expect(response).to redirect_to(:settings_export)