2009-01-06 11:49:19 +01:00
|
|
|
class MessagesController < ApplicationController
|
2019-10-28 21:11:35 +01:00
|
|
|
before_action -> { require_plugin_enabled FoodsoftMessages }
|
2014-04-05 18:47:16 +02:00
|
|
|
|
2009-01-06 11:49:19 +01:00
|
|
|
# Renders the "inbox" action.
|
|
|
|
def index
|
2018-12-20 16:59:37 +01:00
|
|
|
@messages = Message.readable_for(current_user).page(params[:page]).per(@per_page).order('created_at DESC').includes(:sender)
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
2011-05-18 14:47:17 +02:00
|
|
|
|
2009-01-06 11:49:19 +01:00
|
|
|
# Creates a new message object.
|
|
|
|
def new
|
2011-05-18 14:47:17 +02:00
|
|
|
@message = Message.new(params[:message])
|
2015-02-26 00:11:39 +01:00
|
|
|
|
|
|
|
if @message.reply_to
|
|
|
|
original_message = Message.find(@message.reply_to)
|
2016-02-18 11:00:03 +01:00
|
|
|
if original_message.reply_to
|
|
|
|
@message.reply_to = original_message.reply_to
|
|
|
|
end
|
2015-02-26 00:11:39 +01:00
|
|
|
if original_message.is_readable_for?(current_user)
|
2019-10-29 01:19:05 +01:00
|
|
|
@message.add_recipients [original_message.sender_id]
|
2015-02-26 00:11:39 +01:00
|
|
|
@message.group_id = original_message.group_id
|
2015-03-03 13:28:54 +01:00
|
|
|
@message.private = original_message.private
|
2015-02-26 00:11:39 +01:00
|
|
|
@message.subject = I18n.t('messages.model.reply_subject', :subject => original_message.subject)
|
|
|
|
@message.body = I18n.t('messages.model.reply_header', :user => original_message.sender.display, :when => I18n.l(original_message.created_at, :format => :short)) + "\n"
|
2021-03-01 15:27:26 +01:00
|
|
|
original_message.body.each_line { |l| @message.body += I18n.t('messages.model.reply_indent', :line => l) }
|
2015-02-26 00:11:39 +01:00
|
|
|
else
|
2016-03-07 22:17:18 +01:00
|
|
|
redirect_to new_message_url, alert: I18n.t('messages.new.error_private')
|
2015-02-26 00:11:39 +01:00
|
|
|
end
|
2013-03-24 01:36:50 +01:00
|
|
|
end
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
2011-05-18 14:47:17 +02:00
|
|
|
|
2009-01-06 11:49:19 +01:00
|
|
|
# Creates a new message.
|
|
|
|
def create
|
2009-01-15 18:26:37 +01:00
|
|
|
@message = @current_user.send_messages.new(params[:message])
|
|
|
|
if @message.save
|
2020-07-31 14:09:45 +02:00
|
|
|
DeliverMessageJob.perform_later(@message)
|
2013-02-09 10:13:56 +01:00
|
|
|
redirect_to messages_url, :notice => I18n.t('messages.create.notice')
|
2009-01-06 11:49:19 +01:00
|
|
|
else
|
|
|
|
render :action => 'new'
|
|
|
|
end
|
|
|
|
end
|
2011-05-18 14:47:17 +02:00
|
|
|
|
2009-01-06 11:49:19 +01:00
|
|
|
# Shows a single message.
|
|
|
|
def show
|
2009-01-15 18:26:37 +01:00
|
|
|
@message = Message.find(params[:id])
|
2013-03-24 01:36:50 +01:00
|
|
|
unless @message.is_readable_for?(current_user)
|
2022-05-27 21:48:55 +02:00
|
|
|
redirect_to messages_url, alert: I18n.t('messages.new.error_private')
|
2013-03-24 01:36:50 +01:00
|
|
|
end
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
2016-03-04 17:24:28 +01:00
|
|
|
|
2019-02-11 13:22:54 +01:00
|
|
|
def toggle_private
|
|
|
|
message = Message.find(params[:id])
|
|
|
|
if message.can_toggle_private?(current_user)
|
|
|
|
message.update_attribute :private, !message.private
|
|
|
|
redirect_to message
|
|
|
|
else
|
|
|
|
redirect_to message, alert: I18n.t('messages.toggle_private.not_allowed')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-04 17:24:28 +01:00
|
|
|
def thread
|
|
|
|
@messages = Message.thread(params[:id]).order(:created_at)
|
|
|
|
end
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|