-
Notifications
You must be signed in to change notification settings - Fork 211
Expand file tree
/
Copy pathmessages_controller.rb
More file actions
51 lines (46 loc) · 1.75 KB
/
messages_controller.rb
File metadata and controls
51 lines (46 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# 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