diff --git a/app/controllers/dmails_controller.rb b/app/controllers/dmails_controller.rb index b1eeede00..b68b20432 100644 --- a/app/controllers/dmails_controller.rb +++ b/app/controllers/dmails_controller.rb @@ -1,6 +1,6 @@ class DmailsController < ApplicationController respond_to :html, :xml, :js, :json - before_action :member_only, except: [:index, :show, :update, :destroy, :mark_all_as_read] + before_action :member_only, except: [:index, :show, :update, :mark_all_as_read] def new if params[:respond_to_id] @@ -18,7 +18,8 @@ class DmailsController < ApplicationController if params[:folder] && params[:set_default_folder] cookies.permanent[:dmail_folder] = params[:folder] end - @dmails = Dmail.active.visible.paginated_search(params, count_pages: true) + + @dmails = Dmail.visible.paginated_search(params, defaults: { is_spam: false, is_deleted: false }, count_pages: true) respond_with(@dmails) end @@ -43,14 +44,6 @@ class DmailsController < ApplicationController respond_with(@dmail) end - def destroy - @dmail = Dmail.find(params[:id]) - check_privilege(@dmail) - @dmail.mark_as_read! - @dmail.destroy - redirect_to dmails_path, :notice => "Message destroyed" - end - def mark_all_as_read Dmail.visible.unread.each do |x| x.update_column(:is_read, true) diff --git a/app/models/application_record.rb b/app/models/application_record.rb index 777ab9011..8af541e1a 100644 --- a/app/models/application_record.rb +++ b/app/models/application_record.rb @@ -7,8 +7,10 @@ class ApplicationRecord < ActiveRecord::Base extending(PaginationExtension).paginate(*options) end - def paginated_search(params, count_pages: params[:search].present?) + def paginated_search(params, defaults: {}, count_pages: params[:search].present?) search_params = params.fetch(:search, {}).permit! + search_params = defaults.merge(search_params).with_indifferent_access + search(search_params).paginate(params[:page], limit: params[:limit], search_count: count_pages) end end @@ -93,7 +95,7 @@ class ApplicationRecord < ActiveRecord::Base end def search_boolean_attribute(attribute, params) - return all unless params[attribute] + return all unless params.key?(attribute) value = params[attribute].to_s if value.truthy? diff --git a/app/models/dmail.rb b/app/models/dmail.rb index eb8ae5085..b635af47b 100644 --- a/app/models/dmail.rb +++ b/app/models/dmail.rb @@ -136,8 +136,6 @@ class Dmail < ApplicationRecord q = q.text_attribute_matches(:title, params[:title_matches]) q = q.text_attribute_matches(:body, params[:message_matches], index_column: :message_index) - params[:is_spam] = false unless params[:is_spam].present? - q = q.read if params[:read].to_s.truthy? q = q.unread if params[:read].to_s.falsy? diff --git a/app/views/dmails/_secondary_links.html.erb b/app/views/dmails/_secondary_links.html.erb index bd73cf9c6..9a3649914 100644 --- a/app/views/dmails/_secondary_links.html.erb +++ b/app/views/dmails/_secondary_links.html.erb @@ -4,6 +4,7 @@ <%= subnav_link_to "Received", received_dmails_path(set_default_folder: true) %> <%= subnav_link_to "Sent", sent_dmails_path(set_default_folder: true) %> <%= subnav_link_to "Spam", spam_dmails_path %> + <%= subnav_link_to "Deleted", dmails_path(search: { is_deleted: true }) %>
  • |
  • <%= subnav_link_to "New", new_dmail_path %> <%= subnav_link_to "Mark all as read", {:controller => "dmails", :action => "mark_all_as_read"}, :method => :post, :remote => true %> diff --git a/app/views/dmails/index.html.erb b/app/views/dmails/index.html.erb index 00828d700..fed92afdc 100644 --- a/app/views/dmails/index.html.erb +++ b/app/views/dmails/index.html.erb @@ -27,7 +27,14 @@ <%= link_to dmail.title, dmail_path(dmail) %> <% end %> <% t.column do |dmail| %> - <%= link_to "delete", dmail_path(dmail), :method => :delete, :data => {:confirm => "Are you sure you want to delete this Dmail?"} %> + <% if dmail.is_deleted? %> + <%= link_to "Undelete", dmail_path(dmail, format: :js), remote: true, method: :put, "data-params": "dmail[is_deleted]=false" %> + <% else %> + <%= link_to "Delete", dmail_path(dmail, format: :js), remote: true, method: :put, "data-params": "dmail[is_deleted]=true", "data-confirm": "Are you sure you want to delete this dmail?" %> + <% 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" %> + <% end %> <% end %> <% end %> diff --git a/config/routes.rb b/config/routes.rb index bfde9f824..c878be5c1 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -110,7 +110,7 @@ Rails.application.routes.draw do put :cancel end end - resources :dmails, :only => [:new, :create, :update, :index, :show, :destroy] do + resources :dmails, :only => [:new, :create, :update, :index, :show] do collection do post :mark_all_as_read end diff --git a/test/functional/dmails_controller_test.rb b/test/functional/dmails_controller_test.rb index 8ce02c3b0..8c382c25d 100644 --- a/test/functional/dmails_controller_test.rb +++ b/test/functional/dmails_controller_test.rb @@ -94,18 +94,19 @@ class DmailsControllerTest < ActionDispatch::IntegrationTest end end - context "destroy action" do + context "update action" do should "allow deletion if the dmail is owned by the current user" do - assert_difference("Dmail.count", -1) do - delete_auth dmail_path(@dmail), @user - assert_redirected_to dmails_path - end + put_auth dmail_path(@dmail), @user, params: { dmail: { is_deleted: true } } + + assert_redirected_to dmail_path(@dmail) + assert_equal(true, @dmail.reload.is_deleted) end should "not allow deletion if the dmail is not owned by the current user" do - assert_difference("Dmail.count", 0) do - delete_auth dmail_path(@dmail), @unrelated_user - end + put_auth dmail_path(@dmail), @unrelated_user, params: { dmail: { is_deleted: true } } + + assert_response 403 + assert_equal(false, @dmail.reload.is_deleted) end end end