Create message mute resource

This commit is contained in:
Anton Khorev 2025-01-23 04:13:52 +03:00
parent 3490eb580c
commit fafed5f821
7 changed files with 108 additions and 24 deletions

View file

@ -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

View file

@ -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

View file

@ -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
##

View file

@ -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 %>
</div>
</td>

View file

@ -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"

View file

@ -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

View file

@ -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