Files
growstuff/app/controllers/messages_controller.rb
google-labs-jules[bot] 406286d07a Implement blocking feature
This commit introduces a blocking feature that allows members to block other members.

A blocked member is prevented from:
- following the blocker
- sending private messages to the blocker
- replying to the blocker's posts
- liking the blocker's content

The implementation includes:
- A new `Block` model and a corresponding database table.
- Updates to the `Member` model to include associations for blocks.
- A new `BlocksController` to handle blocking and unblocking actions.
- New routes for the `BlocksController`.
- UI changes to add block/unblock buttons to the member profile page.
- Validations in the `Follow`, `Comment`, and `Like` models to enforce the blocking rules.
- A check in the `MessagesController` to prevent sending messages to a member who has blocked the sender.
- A callback in the `Block` model to destroy the follow relationship when a block is created.
- New feature and model specs to test the blocking functionality.
2025-09-01 22:07:41 +00:00

52 lines
1.8 KiB
Ruby

# frozen_string_literal: true
class MessagesController < ApplicationController
respond_to :html, :json
before_action :authenticate_member!
def index
redirect_to conversations_path(box: @box)
end
def show
if (@message = Message.find_by(id: params[:id])) && (@conversation = @message.conversation)
if @conversation.is_participant?(current_member)
redirect_to conversation_path(@conversation, box: @box, anchor: "message_" + @message.id.to_s)
return
end
end
redirect_to conversations_path(box: @box)
end
def new
return if params[:recipient_id].blank?
@recipient = Member.find_by(id: params[:recipient_id])
nil if @recipient.nil?
end
def create
if params[:conversation_id].present?
@conversation = Mailboxer::Conversation.find(params[:conversation_id])
# Check if any of the recipients have blocked the sender
if @conversation.recipients.any? { |recipient| recipient.already_blocking?(current_member) }
flash[:error] = "You cannot reply to this conversation because one of the recipients has blocked you."
redirect_to conversation_path(@conversation)
return
end
current_member.reply_to_conversation(@conversation, params[:body])
redirect_to conversation_path(@conversation)
else
recipient = Member.find(params[:recipient_id])
if recipient.already_blocking?(current_member)
flash[:error] = "You cannot send a message to a member who has blocked you."
redirect_back fallback_location: root_path
return
end
body = params[:body]
subject = params[:subject]
@conversation = current_member.send_message(recipient, body, subject)
redirect_to conversations_path(box: 'sentbox')
end
end
end