| 
									
										
										
										
											2019-03-17 13:36:03 +00:00
										 |  |  | # frozen_string_literal: true | 
					
						
							| 
									
										
										
										
											2019-03-15 01:32:57 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-31 12:04:25 +00:00
										 |  |  | # Redmine - project management software | 
					
						
							| 
									
										
										
										
											2024-02-26 22:55:54 +00:00
										 |  |  | # Copyright (C) 2006-  Jean-Philippe Lang | 
					
						
							| 
									
										
										
										
											2007-05-13 17:09:56 +00:00
										 |  |  | # | 
					
						
							|  |  |  | # This program is free software; you can redistribute it and/or | 
					
						
							|  |  |  | # modify it under the terms of the GNU General Public License | 
					
						
							|  |  |  | # as published by the Free Software Foundation; either version 2 | 
					
						
							|  |  |  | # of the License, or (at your option) any later version. | 
					
						
							| 
									
										
										
										
											2011-08-31 12:04:25 +00:00
										 |  |  | # | 
					
						
							| 
									
										
										
										
											2007-05-13 17:09:56 +00:00
										 |  |  | # This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  | # but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  | # GNU General Public License for more details. | 
					
						
							| 
									
										
										
										
											2011-08-31 12:04:25 +00:00
										 |  |  | # | 
					
						
							| 
									
										
										
										
											2007-05-13 17:09:56 +00:00
										 |  |  | # You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  | # along with this program; if not, write to the Free Software | 
					
						
							|  |  |  | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class MessagesController < ApplicationController | 
					
						
							| 
									
										
										
										
											2008-01-19 11:53:43 +00:00
										 |  |  |   menu_item :boards | 
					
						
							| 
									
										
										
										
											2009-10-21 17:07:18 +00:00
										 |  |  |   default_search_scope :messages | 
					
						
							| 
									
										
										
										
											2016-07-14 07:27:31 +00:00
										 |  |  |   before_action :find_board, :only => [:new, :preview] | 
					
						
							|  |  |  |   before_action :find_attachments, :only => [:preview] | 
					
						
							|  |  |  |   before_action :find_message, :except => [:new, :preview] | 
					
						
							|  |  |  |   before_action :authorize, :except => [:preview, :edit, :destroy] | 
					
						
							| 
									
										
										
										
											2007-05-13 17:09:56 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-07-31 17:17:52 +00:00
										 |  |  |   helper :boards | 
					
						
							| 
									
										
										
										
											2008-09-19 15:32:52 +00:00
										 |  |  |   helper :watchers | 
					
						
							| 
									
										
										
										
											2007-05-26 15:42:37 +00:00
										 |  |  |   helper :attachments | 
					
						
							| 
									
										
										
										
											2011-08-31 12:04:25 +00:00
										 |  |  |   include AttachmentsHelper | 
					
						
							| 
									
										
										
										
											2007-05-26 15:42:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-06 12:54:13 +00:00
										 |  |  |   REPLIES_PER_PAGE = 25 unless const_defined?(:REPLIES_PER_PAGE) | 
					
						
							| 
									
										
										
										
											2011-08-31 12:04:25 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-24 12:25:07 +00:00
										 |  |  |   # Show a topic and its replies | 
					
						
							| 
									
										
										
										
											2007-05-13 17:09:56 +00:00
										 |  |  |   def show | 
					
						
							| 
									
										
										
										
											2010-02-06 12:54:13 +00:00
										 |  |  |     page = params[:page] | 
					
						
							|  |  |  |     # Find the page of the requested reply | 
					
						
							|  |  |  |     if params[:r] && page.nil? | 
					
						
							| 
									
										
										
										
											2013-06-12 19:13:25 +00:00
										 |  |  |       offset = @topic.children.where("#{Message.table_name}.id < ?", params[:r].to_i).count | 
					
						
							| 
									
										
										
										
											2010-02-06 12:54:13 +00:00
										 |  |  |       page = 1 + offset / REPLIES_PER_PAGE | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2011-08-31 12:04:25 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-06 12:54:13 +00:00
										 |  |  |     @reply_count = @topic.children.count | 
					
						
							| 
									
										
										
										
											2013-01-08 20:18:12 +00:00
										 |  |  |     @reply_pages = Paginator.new @reply_count, REPLIES_PER_PAGE, page | 
					
						
							| 
									
										
										
										
											2012-12-02 19:09:42 +00:00
										 |  |  |     @replies =  @topic.children. | 
					
						
							|  |  |  |       includes(:author, :attachments, {:board => :project}). | 
					
						
							| 
									
										
										
										
											2014-10-22 17:37:16 +00:00
										 |  |  |       reorder("#{Message.table_name}.created_on ASC, #{Message.table_name}.id ASC"). | 
					
						
							| 
									
										
										
										
											2013-02-17 11:35:42 +00:00
										 |  |  |       limit(@reply_pages.per_page). | 
					
						
							| 
									
										
										
										
											2013-01-08 20:18:12 +00:00
										 |  |  |       offset(@reply_pages.offset). | 
					
						
							| 
									
										
										
										
											2014-10-22 17:37:16 +00:00
										 |  |  |       to_a | 
					
						
							| 
									
										
										
										
											2011-08-31 12:04:25 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-05-13 17:09:56 +00:00
										 |  |  |     @reply = Message.new(:subject => "RE: #{@message.subject}") | 
					
						
							|  |  |  |     render :action => "show", :layout => false if request.xhr? | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2011-08-31 12:04:25 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-24 12:25:07 +00:00
										 |  |  |   # Create a new topic | 
					
						
							| 
									
										
										
										
											2007-05-13 17:09:56 +00:00
										 |  |  |   def new | 
					
						
							| 
									
										
										
										
											2012-03-06 19:46:59 +00:00
										 |  |  |     @message = Message.new | 
					
						
							| 
									
										
										
										
											2007-11-20 15:40:16 +00:00
										 |  |  |     @message.author = User.current | 
					
						
							| 
									
										
										
										
											2007-11-24 12:25:07 +00:00
										 |  |  |     @message.board = @board | 
					
						
							| 
									
										
										
										
											2012-03-06 19:46:59 +00:00
										 |  |  |     @message.safe_attributes = params[:message] | 
					
						
							| 
									
										
										
										
											2012-02-23 13:02:35 +00:00
										 |  |  |     if request.post? | 
					
						
							|  |  |  |       @message.save_attachments(params[:attachments]) | 
					
						
							|  |  |  |       if @message.save | 
					
						
							| 
									
										
										
										
											2020-11-14 13:19:04 +00:00
										 |  |  |         call_hook(:controller_messages_new_after_save, {:params => params, :message => @message}) | 
					
						
							| 
									
										
										
										
											2012-02-23 13:02:35 +00:00
										 |  |  |         render_attachment_warning_if_needed(@message) | 
					
						
							| 
									
										
										
										
											2018-12-15 21:40:09 +00:00
										 |  |  |         flash[:notice] = l(:notice_successful_create) | 
					
						
							| 
									
										
										
										
											2012-07-15 19:14:23 +00:00
										 |  |  |         redirect_to board_message_path(@board, @message) | 
					
						
							| 
									
										
										
										
											2012-02-23 13:02:35 +00:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2007-05-13 17:09:56 +00:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-24 12:25:07 +00:00
										 |  |  |   # Reply to a topic | 
					
						
							| 
									
										
										
										
											2007-05-13 17:09:56 +00:00
										 |  |  |   def reply | 
					
						
							| 
									
										
										
										
											2012-03-06 19:46:59 +00:00
										 |  |  |     @reply = Message.new | 
					
						
							| 
									
										
										
										
											2007-11-20 15:40:16 +00:00
										 |  |  |     @reply.author = User.current | 
					
						
							| 
									
										
										
										
											2007-05-13 17:09:56 +00:00
										 |  |  |     @reply.board = @board | 
					
						
							| 
									
										
										
										
											2012-03-06 19:46:59 +00:00
										 |  |  |     @reply.safe_attributes = params[:reply] | 
					
						
							| 
									
										
										
										
											2020-02-11 02:34:25 +00:00
										 |  |  |     @reply.save_attachments(params[:attachments]) | 
					
						
							| 
									
										
										
										
											2007-11-24 12:25:07 +00:00
										 |  |  |     @topic.children << @reply | 
					
						
							| 
									
										
										
										
											2024-08-25 08:44:33 +00:00
										 |  |  |     unless @reply.new_record? | 
					
						
							| 
									
										
										
										
											2020-11-14 13:19:04 +00:00
										 |  |  |       call_hook(:controller_messages_reply_after_save, {:params => params, :message => @reply}) | 
					
						
							| 
									
										
										
										
											2010-03-03 17:05:00 +00:00
										 |  |  |       render_attachment_warning_if_needed(@reply) | 
					
						
							| 
									
										
										
										
											2007-11-22 19:19:47 +00:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2018-12-15 21:40:09 +00:00
										 |  |  |     flash[:notice] = l(:notice_successful_update) | 
					
						
							| 
									
										
										
										
											2012-07-15 19:14:23 +00:00
										 |  |  |     redirect_to board_message_path(@board, @topic, :r => @reply) | 
					
						
							| 
									
										
										
										
											2007-11-24 12:25:07 +00:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   # Edit a message | 
					
						
							|  |  |  |   def edit | 
					
						
							| 
									
										
										
										
											2009-12-18 14:22:18 +00:00
										 |  |  |     (render_403; return false) unless @message.editable_by?(User.current) | 
					
						
							| 
									
										
										
										
											2012-03-06 19:46:59 +00:00
										 |  |  |     @message.safe_attributes = params[:message] | 
					
						
							| 
									
										
										
										
											2020-02-11 02:34:25 +00:00
										 |  |  |     if request.post? | 
					
						
							|  |  |  |       @message.save_attachments(params[:attachments]) | 
					
						
							|  |  |  |       if @message.save | 
					
						
							|  |  |  |         render_attachment_warning_if_needed(@message) | 
					
						
							|  |  |  |         flash[:notice] = l(:notice_successful_update) | 
					
						
							|  |  |  |         @message.reload | 
					
						
							|  |  |  |         redirect_to board_message_path(@message.board, @message.root, :r => (@message.parent_id && @message.id)) | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2007-11-24 12:25:07 +00:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2011-08-31 12:04:25 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-24 12:25:07 +00:00
										 |  |  |   # Delete a messages | 
					
						
							|  |  |  |   def destroy | 
					
						
							| 
									
										
										
										
											2009-12-18 14:22:18 +00:00
										 |  |  |     (render_403; return false) unless @message.destroyable_by?(User.current) | 
					
						
							| 
									
										
										
										
											2012-04-25 17:17:49 +00:00
										 |  |  |     r = @message.to_param | 
					
						
							| 
									
										
										
										
											2007-11-24 12:25:07 +00:00
										 |  |  |     @message.destroy | 
					
						
							| 
									
										
										
										
											2018-12-15 21:40:09 +00:00
										 |  |  |     flash[:notice] = l(:notice_successful_delete) | 
					
						
							| 
									
										
										
										
											2012-07-15 19:14:23 +00:00
										 |  |  |     if @message.parent | 
					
						
							|  |  |  |       redirect_to board_message_path(@board, @message.parent, :r => r) | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       redirect_to project_board_path(@project, @board) | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2007-05-13 17:09:56 +00:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2011-08-31 12:04:25 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-28 18:56:47 +00:00
										 |  |  |   def quote | 
					
						
							| 
									
										
										
										
											2012-07-19 18:23:45 +00:00
										 |  |  |     @subject = @message.subject | 
					
						
							|  |  |  |     @subject = "RE: #{@subject}" unless @subject.starts_with?('RE:') | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-02 13:29:54 +00:00
										 |  |  |     if @message.root == @message | 
					
						
							|  |  |  |       @content = +"#{ll(Setting.default_language, :text_user_wrote, @message.author)}\n> " | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       @content = +"#{ll(Setting.default_language, :text_user_wrote_in, {:value => @message.author, :link => "message##{@message.id}"})}\n> " | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2014-03-17 07:49:14 +00:00
										 |  |  |     @content << @message.content.to_s.strip.gsub(%r{<pre>(.*?)</pre>}m, '[...]').gsub(/(\r?\n|\r\n?)/, "\n> ") + "\n\n" | 
					
						
							| 
									
										
										
										
											2024-01-03 01:19:25 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     respond_to do |format| | 
					
						
							|  |  |  |       format.html { render_404 } | 
					
						
							|  |  |  |       format.js | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2008-08-28 18:56:47 +00:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2011-08-31 12:04:25 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-07 17:49:44 +00:00
										 |  |  |   def preview | 
					
						
							|  |  |  |     message = @board.messages.find_by_id(params[:id]) | 
					
						
							| 
									
										
										
										
											2018-09-26 07:27:30 +00:00
										 |  |  |     @text = params[:text] ? params[:text] : nil | 
					
						
							| 
									
										
										
										
											2012-07-31 17:22:17 +00:00
										 |  |  |     @previewed = message | 
					
						
							| 
									
										
										
										
											2008-03-07 17:49:44 +00:00
										 |  |  |     render :partial => 'common/preview' | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2011-08-31 12:04:25 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-22 13:44:04 +00:00
										 |  |  |   private | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-24 12:25:07 +00:00
										 |  |  |   def find_message | 
					
						
							| 
									
										
										
										
											2013-02-20 20:59:52 +00:00
										 |  |  |     return unless find_board | 
					
						
							| 
									
										
										
										
											2020-11-05 13:42:23 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-08 06:11:46 +00:00
										 |  |  |     @message = @board.messages.includes(:parent).find(params[:id]) | 
					
						
							| 
									
										
										
										
											2007-11-24 12:25:07 +00:00
										 |  |  |     @topic = @message.root | 
					
						
							|  |  |  |   rescue ActiveRecord::RecordNotFound | 
					
						
							|  |  |  |     render_404 | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2011-08-31 12:04:25 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-24 12:25:07 +00:00
										 |  |  |   def find_board | 
					
						
							| 
									
										
										
										
											2014-01-08 06:11:46 +00:00
										 |  |  |     @board = Board.includes(:project).find(params[:board_id]) | 
					
						
							| 
									
										
										
										
											2007-05-13 17:09:56 +00:00
										 |  |  |     @project = @board.project | 
					
						
							|  |  |  |   rescue ActiveRecord::RecordNotFound | 
					
						
							|  |  |  |     render_404 | 
					
						
							| 
									
										
										
										
											2013-02-20 20:59:52 +00:00
										 |  |  |     nil | 
					
						
							| 
									
										
										
										
											2007-05-13 17:09:56 +00:00
										 |  |  |   end | 
					
						
							|  |  |  | end |