From fafed5f82159141b724dd517facb561d305bb9b1 Mon Sep 17 00:00:00 2001 From: Anton Khorev Date: Thu, 23 Jan 2025 04:13:52 +0300 Subject: [PATCH] Create message mute resource --- app/abilities/ability.rb | 2 +- app/controllers/messages/mutes_controller.rb | 33 +++++++++ app/controllers/messages_controller.rb | 17 ----- .../messages/mailboxes/_message.html.erb | 2 +- config/locales/en.yml | 7 +- config/routes.rb | 3 +- .../messages/mutes_controller_test.rb | 68 +++++++++++++++++++ 7 files changed, 108 insertions(+), 24 deletions(-) create mode 100644 app/controllers/messages/mutes_controller.rb create mode 100644 test/controllers/messages/mutes_controller_test.rb diff --git a/app/abilities/ability.rb b/app/abilities/ability.rb index 262e92c26..3116bc5cd 100644 --- a/app/abilities/ability.rb +++ b/app/abilities/ability.rb @@ -43,7 +43,7 @@ class Ability can :update, DiaryEntry, :user => user can [:create], DiaryComment can [:show, :create, :destroy], Follow - can [:read, :create, :unmute, :destroy], Message + can [:read, :create, :destroy], Message can [:close, :reopen], Note can [:read, :update], :preference can :update, :profile diff --git a/app/controllers/messages/mutes_controller.rb b/app/controllers/messages/mutes_controller.rb new file mode 100644 index 000000000..0d0ef7d40 --- /dev/null +++ b/app/controllers/messages/mutes_controller.rb @@ -0,0 +1,33 @@ +module Messages + class MutesController < ApplicationController + layout "site" + + before_action :authorize_web + before_action :set_locale + + authorize_resource :message + + before_action :check_database_readable + before_action :check_database_writable + + # Moves message into Inbox by unsetting the muted-flag + def destroy + message = current_user.muted_messages.find(params[:message_id]) + + if message.unmute + flash[:notice] = t(".notice") + else + flash[:error] = t(".error") + end + + if current_user.muted_messages.none? + redirect_to messages_inbox_path + else + redirect_to messages_muted_inbox_path + end + rescue ActiveRecord::RecordNotFound + @title = t "messages.no_such_message.title" + render :template => "messages/no_such_message", :status => :not_found + end + end +end diff --git a/app/controllers/messages_controller.rb b/app/controllers/messages_controller.rb index 5a63dfd6e..1979c9edc 100644 --- a/app/controllers/messages_controller.rb +++ b/app/controllers/messages_controller.rb @@ -73,23 +73,6 @@ class MessagesController < ApplicationController render :action => "no_such_message", :status => :not_found end - # Moves message into Inbox by unsetting the muted-flag - def unmute - message = current_user.muted_messages.find(params[:message_id]) - - if message.unmute - flash[:notice] = t(".notice") - else - flash[:error] = t(".error") - end - - if current_user.muted_messages.none? - redirect_to messages_inbox_path - else - redirect_to messages_muted_inbox_path - end - end - private ## diff --git a/app/views/messages/mailboxes/_message.html.erb b/app/views/messages/mailboxes/_message.html.erb index 96d3fa2ca..5c9c4eb49 100644 --- a/app/views/messages/mailboxes/_message.html.erb +++ b/app/views/messages/mailboxes/_message.html.erb @@ -8,7 +8,7 @@ <%= button_to t(".read_button"), message_read_mark_path(message), :method => :post, :class => "btn btn-sm btn-primary", :form => { :data => { :turbo => true }, :hidden => message.message_read? } %> <%= button_to t(".destroy_button"), message_path(message, :referer => request.fullpath), :method => :delete, :class => "btn btn-sm btn-danger", :form => { :data => { :turbo => true }, :class => "destroy-message" } %> <% if message.muted? %> - <%= button_to t(".unmute_button"), message_unmute_path(message), :method => :patch, :class => "btn btn-sm btn-secondary", :form => { :data => { :turbo => true } } %> + <%= button_to t(".unmute_button"), message_mute_path(message), :method => :delete, :class => "btn btn-sm btn-secondary", :form => { :data => { :turbo => true } } %> <% end %> diff --git a/config/locales/en.yml b/config/locales/en.yml index d1df36f83..da3afb2ee 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1782,9 +1782,6 @@ en: destroy_button: "Delete" back: "Back" wrong_user: "You are logged in as '%{user}' but the message you have asked to read was not sent by or to that user. Please log in as the correct user in order to read it." - unmute: - notice: "Message has been moved to Inbox" - error: "The message could not be moved to the Inbox." destroy: destroyed: "Message deleted" read_marks: @@ -1792,6 +1789,10 @@ en: notice: "Message marked as read" destroy: notice: "Message marked as unread" + mutes: + destroy: + notice: "Message has been moved to Inbox" + error: "The message could not be moved to the Inbox." mailboxes: heading: my_inbox: "My Inbox" diff --git a/config/routes.rb b/config/routes.rb index 453c625ad..6e5e73855 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -322,11 +322,10 @@ OpenStreetMap::Application.routes.draw do # messages resources :messages, :path_names => { :new => "new/:display_name" }, :id => /\d+/, :only => [:new, :create, :show, :destroy] do - patch :unmute - scope :module => :messages do resource :reply, :path_names => { :new => "new" }, :only => :new resource :read_mark, :only => [:create, :destroy] + resource :mute, :only => :destroy end end namespace :messages, :path => "/messages" do diff --git a/test/controllers/messages/mutes_controller_test.rb b/test/controllers/messages/mutes_controller_test.rb new file mode 100644 index 000000000..f405c0650 --- /dev/null +++ b/test/controllers/messages/mutes_controller_test.rb @@ -0,0 +1,68 @@ +require "test_helper" + +module Messages + class MutesControllerTest < ActionDispatch::IntegrationTest + ## + # test all routes which lead to this controller + def test_routes + assert_routing( + { :path => "/messages/1/mute", :method => :delete }, + { :controller => "messages/mutes", :action => "destroy", :message_id => "1" } + ) + end + + def test_destroy_when_not_logged_in + sender_user = create(:user) + recipient_user = create(:user) + create(:user_mute, :owner => recipient_user, :subject => sender_user) + message = create(:message, :unread, :sender => sender_user, :recipient => recipient_user) + + delete message_mute_path(message) + assert_response :forbidden + end + + def test_destroy_as_other_user + sender_user = create(:user) + recipient_user = create(:user) + create(:user_mute, :owner => recipient_user, :subject => sender_user) + message = create(:message, :unread, :sender => sender_user, :recipient => recipient_user) + session_for(create(:user)) + + delete message_mute_path(message) + assert_response :not_found + assert_template "no_such_message" + end + + def test_destroy_as_sender + sender_user = create(:user) + recipient_user = create(:user) + create(:user_mute, :owner => recipient_user, :subject => sender_user) + message = create(:message, :unread, :sender => sender_user, :recipient => recipient_user) + session_for(sender_user) + + delete message_mute_path(message) + assert_response :not_found + assert_template "no_such_message" + end + + def test_destroy_as_recipient + sender_user = create(:user) + recipient_user = create(:user) + create(:user_mute, :owner => recipient_user, :subject => sender_user) + message = create(:message, :unread, :sender => sender_user, :recipient => recipient_user) + session_for(recipient_user) + + delete message_mute_path(message) + assert_redirected_to messages_inbox_path + assert_not message.reload.muted + end + + def test_destroy_on_missing_message + session_for(create(:user)) + + delete message_mute_path(99999) + assert_response :not_found + assert_template "no_such_message" + end + end +end -- 2.39.5