]> cat aescling's git repositories - mastodon.git/commitdiff
Allow users to delete their header and avatar (#13234)
authorsternenseemann <git@lukasepple.de>
Mon, 20 Apr 2020 12:03:03 +0000 (14:03 +0200)
committerGitHub <noreply@github.com>
Mon, 20 Apr 2020 12:03:03 +0000 (14:03 +0200)
This is achieved by sending a DELETE request to
/settings/profile/pictures/{avatar,header} via a link that is part of
the upload form's hint of the respective picture.

app/controllers/settings/pictures_controller.rb [new file with mode: 0644]
app/helpers/settings_helper.rb
app/views/settings/profiles/show.html.haml
config/routes.rb

diff --git a/app/controllers/settings/pictures_controller.rb b/app/controllers/settings/pictures_controller.rb
new file mode 100644 (file)
index 0000000..7392670
--- /dev/null
@@ -0,0 +1,37 @@
+# frozen_string_literal: true
+
+module Settings
+  class PicturesController < BaseController
+    before_action :authenticate_user!
+    before_action :set_account
+    before_action :set_picture
+
+    def destroy
+      if valid_picture
+        account_params = {
+          @picture => nil,
+          (@picture + '_remote_url') => nil,
+        }
+
+        msg = UpdateAccountService.new.call(@account, account_params) ? I18n.t('generic.changes_saved_msg') : nil
+        redirect_to settings_profile_path, notice: msg, status: 303
+      else
+        bad_request
+      end
+    end
+
+    private
+
+    def set_account
+      @account = current_account
+    end
+
+    def set_picture
+      @picture = params[:id]
+    end
+
+    def valid_picture
+      @picture == 'avatar' || @picture == 'header'
+    end
+  end
+end
index 825aa974d88599f690cf3249c0d3c237e289167f..74544bad98470d4397c8c883609a5d9de8a41331 100644 (file)
@@ -105,4 +105,13 @@ module SettingsHelper
       safe_join([image_tag(account.avatar.url, width: 15, height: 15, alt: display_name(account), class: 'avatar'), content_tag(:span, account.acct, class: 'username')], ' ')
     end
   end
+
+  def picture_hint(hint, picture)
+    if picture.original_filename.nil?
+      hint
+    else
+      link = link_to t('generic.delete'), settings_profile_picture_path(picture.name.to_s), data: { method: :delete }
+      safe_join([hint, link], '<br/>'.html_safe)
+    end
+  end
 end
index c55ab7b9083d3f9fb128f8273fbe447aacbda02b..6497824c6d2aa27374b9d344a6427e506411d9cd 100644 (file)
@@ -17,9 +17,9 @@
       = render 'application/card', account: @account
 
     .fields-row__column.fields-group.fields-row__column-6
-      = f.input :header, wrapper: :with_label, input_html: { accept: AccountHeader::IMAGE_MIME_TYPES.join(',') }, hint: t('simple_form.hints.defaults.header', dimensions: '1500x500', size: number_to_human_size(AccountHeader::LIMIT))
+      = f.input :header, wrapper: :with_label, input_html: { accept: AccountHeader::IMAGE_MIME_TYPES.join(',') }, hint: picture_hint(t('simple_form.hints.defaults.header', dimensions: '1500x500', size: number_to_human_size(AccountHeader::LIMIT)), @account.header)
 
-      = f.input :avatar, wrapper: :with_label, input_html: { accept: AccountAvatar::IMAGE_MIME_TYPES.join(',') }, hint: t('simple_form.hints.defaults.avatar', dimensions: '400x400', size: number_to_human_size(AccountAvatar::LIMIT))
+      = f.input :avatar, wrapper: :with_label, input_html: { accept: AccountAvatar::IMAGE_MIME_TYPES.join(',') }, hint: picture_hint(t('simple_form.hints.defaults.avatar', dimensions: '400x400', size: number_to_human_size(AccountAvatar::LIMIT)), @account.avatar)
 
   %hr.spacer/
 
index 49ab851e2cf854fcbe0a6e487427202de4331c56..fa6639138da7910e11fcf65458fb1b4aca2ecec7 100644 (file)
@@ -100,7 +100,9 @@ Rails.application.routes.draw do
   get '/settings', to: redirect('/settings/profile')
 
   namespace :settings do
-    resource :profile, only: [:show, :update]
+    resource :profile, only: [:show, :update] do
+      resources :pictures, only: :destroy
+    end
 
     get :preferences, to: redirect('/settings/preferences/appearance')