From 5df8d08aaee57e688d79c2419c9bcd0ca436b952 Mon Sep 17 00:00:00 2001 From: evazion Date: Thu, 30 Jan 2020 20:59:53 -0600 Subject: [PATCH] dmails: allow Members to mark dmails as spam. * Allow Members to mark dmails as spam or not spam (previously Gold only). * Replace spam and ham endpoints with single update endpoint. --- app/controllers/dmails_controller.rb | 35 ++++++++++++++-------------- app/views/dmails/ham.js.erb | 2 -- app/views/dmails/show.html.erb | 12 ++++------ app/views/dmails/spam.js.erb | 2 -- app/views/dmails/update.js.erb | 1 + config/routes.rb | 6 +---- 6 files changed, 24 insertions(+), 34 deletions(-) delete mode 100644 app/views/dmails/ham.js.erb delete mode 100644 app/views/dmails/spam.js.erb create mode 100644 app/views/dmails/update.js.erb diff --git a/app/controllers/dmails_controller.rb b/app/controllers/dmails_controller.rb index d76e19985..b1eeede00 100644 --- a/app/controllers/dmails_controller.rb +++ b/app/controllers/dmails_controller.rb @@ -1,7 +1,6 @@ class DmailsController < ApplicationController - respond_to :html, :xml, :json - before_action :member_only, except: [:index, :show, :destroy, :mark_all_as_read] - before_action :gold_only, only: [:ham, :spam] + respond_to :html, :xml, :js, :json + before_action :member_only, except: [:index, :show, :update, :destroy, :mark_all_as_read] def new if params[:respond_to_id] @@ -9,7 +8,7 @@ class DmailsController < ApplicationController check_privilege(parent) @dmail = parent.build_response(:forward => params[:forward]) else - @dmail = Dmail.new(create_params) + @dmail = Dmail.new(dmail_params(:create)) end respond_with(@dmail) @@ -31,7 +30,16 @@ class DmailsController < ApplicationController end def create - @dmail = Dmail.create_split(create_params) + @dmail = Dmail.create_split(dmail_params(:create)) + respond_with(@dmail) + end + + def update + @dmail = Dmail.find(params[:id]) + check_privilege(@dmail) + @dmail.update(dmail_params(:update)) + flash[:notice] = "Dmail updated" + respond_with(@dmail) end @@ -50,16 +58,6 @@ class DmailsController < ApplicationController CurrentUser.user.update(has_mail: false, unread_dmail_count: 0) end - def spam - @dmail = Dmail.find(params[:id]) - @dmail.update_column(:is_spam, true) - end - - def ham - @dmail = Dmail.find(params[:id]) - @dmail.update_column(:is_spam, false) - end - private def check_privilege(dmail) @@ -68,7 +66,10 @@ class DmailsController < ApplicationController end end - def create_params - params.fetch(:dmail, {}).permit(:title, :body, :to_name, :to_id) + def dmail_params(context) + permitted_params = %i[title body to_name to_id] if context == :create + permitted_params = %i[is_spam is_read is_deleted] if context == :update + + params.fetch(:dmail, {}).permit(permitted_params) end end diff --git a/app/views/dmails/ham.js.erb b/app/views/dmails/ham.js.erb deleted file mode 100644 index 27574002b..000000000 --- a/app/views/dmails/ham.js.erb +++ /dev/null @@ -1,2 +0,0 @@ -Danbooru.notice("Message marked as not spam"); -$("#spam-links").hide(); diff --git a/app/views/dmails/show.html.erb b/app/views/dmails/show.html.erb index 28ea7361c..0bd18b085 100644 --- a/app/views/dmails/show.html.erb +++ b/app/views/dmails/show.html.erb @@ -28,14 +28,10 @@ <%= link_to "Respond", new_dmail_path(:respond_to_id => @dmail) %> | <%= link_to "Forward", new_dmail_path(:respond_to_id => @dmail, :forward => true) %> | <%= link_to "Permalink", dmail_path(@dmail, :key => @dmail.key), :title => "Use this URL to privately share with a moderator" %> - <% if CurrentUser.is_gold? %> - - <% if @dmail.is_spam? %> - | <%= link_to "Not spam", ham_dmail_path(@dmail), remote: :true, method: :post %> - <% else %> - | <%= link_to "Spam", spam_dmail_path(@dmail), remote: :true, method: :post %> - <% end %> - + <% if @dmail.is_spam? %> + | <%= link_to "Not spam", dmail_path(@dmail, format: :js), remote: :true, method: :put, "data-params": "dmail[is_spam]=false" %> + <% else %> + | <%= link_to "Spam", dmail_path(@dmail, format: :js), remote: :true, method: :put, "data-params": "dmail[is_spam]=true" %> <% end %> <% if @dmail.reportable_by?(CurrentUser.user) %> | <%= link_to "Report", new_moderation_report_path(moderation_report: { model_type: "Dmail", model_id: @dmail.id }), remote: true, title: "Report this dmail to the moderators" %> diff --git a/app/views/dmails/spam.js.erb b/app/views/dmails/spam.js.erb deleted file mode 100644 index c447171fb..000000000 --- a/app/views/dmails/spam.js.erb +++ /dev/null @@ -1,2 +0,0 @@ -Danbooru.notice("Message marked as spam"); -$("#spam-links").hide(); diff --git a/app/views/dmails/update.js.erb b/app/views/dmails/update.js.erb new file mode 100644 index 000000000..345366b9b --- /dev/null +++ b/app/views/dmails/update.js.erb @@ -0,0 +1 @@ +location.reload(); diff --git a/config/routes.rb b/config/routes.rb index 00530dca6..bfde9f824 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -110,11 +110,7 @@ Rails.application.routes.draw do put :cancel end end - resources :dmails, :only => [:new, :create, :index, :show, :destroy] do - member do - post :spam - post :ham - end + resources :dmails, :only => [:new, :create, :update, :index, :show, :destroy] do collection do post :mark_all_as_read end