2014-03-23 00:22:25 +00:00
|
|
|
class MessagesController < ApplicationController
|
2020-03-16 23:57:47 +00:00
|
|
|
before_action :get_message, only: [:show, :edit, :update, :destroy]
|
2014-03-23 00:22:25 +00:00
|
|
|
|
|
|
|
def index
|
|
|
|
raise AccessError unless cuser
|
|
|
|
end
|
|
|
|
|
|
|
|
def show
|
|
|
|
raise AccessError unless @message.can_show? cuser
|
2020-03-15 18:31:00 +00:00
|
|
|
@message.mark_as_read! for: cuser
|
2014-03-23 00:22:25 +00:00
|
|
|
@messages = @message.thread
|
|
|
|
end
|
|
|
|
|
|
|
|
def new
|
|
|
|
@message = Message.new
|
|
|
|
raise AccessError unless @message.can_create? cuser
|
|
|
|
|
|
|
|
case params[:id]
|
|
|
|
when "User"
|
|
|
|
@message.recipient = User.find(params[:id2])
|
|
|
|
when "Team"
|
|
|
|
@message.recipient = Team.find(params[:id2])
|
|
|
|
when "Group"
|
|
|
|
@message.recipient = Group.find(params[:id2])
|
|
|
|
else
|
|
|
|
raise Error, "Illegible recipient"
|
|
|
|
end
|
|
|
|
@message.title = params[:title]
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2020-03-18 03:38:17 +00:00
|
|
|
@message = Message.new(Message.params(params, cuser))
|
2014-03-23 00:22:25 +00:00
|
|
|
@message.sender = @message.sender_raw == "" ? cuser : cuser.active_teams.find(@message.sender_raw)
|
|
|
|
raise AccessError unless @message.can_create? cuser
|
|
|
|
|
|
|
|
if @message.save
|
|
|
|
flash[:notice] = t(:message_create)
|
|
|
|
redirect_to(@message)
|
|
|
|
else
|
2014-03-31 21:33:16 +00:00
|
|
|
render :new
|
2014-03-23 00:22:25 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def get_message
|
|
|
|
@message = Message.find(params[:id])
|
|
|
|
end
|
|
|
|
end
|