You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

messages_controller.rb 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2017 Jean-Philippe Lang
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # of the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. class MessagesController < ApplicationController
  18. menu_item :boards
  19. default_search_scope :messages
  20. before_action :find_board, :only => [:new, :preview]
  21. before_action :find_attachments, :only => [:preview]
  22. before_action :find_message, :except => [:new, :preview]
  23. before_action :authorize, :except => [:preview, :edit, :destroy]
  24. helper :boards
  25. helper :watchers
  26. helper :attachments
  27. include AttachmentsHelper
  28. REPLIES_PER_PAGE = 25 unless const_defined?(:REPLIES_PER_PAGE)
  29. # Show a topic and its replies
  30. def show
  31. page = params[:page]
  32. # Find the page of the requested reply
  33. if params[:r] && page.nil?
  34. offset = @topic.children.where("#{Message.table_name}.id < ?", params[:r].to_i).count
  35. page = 1 + offset / REPLIES_PER_PAGE
  36. end
  37. @reply_count = @topic.children.count
  38. @reply_pages = Paginator.new @reply_count, REPLIES_PER_PAGE, page
  39. @replies = @topic.children.
  40. includes(:author, :attachments, {:board => :project}).
  41. reorder("#{Message.table_name}.created_on ASC, #{Message.table_name}.id ASC").
  42. limit(@reply_pages.per_page).
  43. offset(@reply_pages.offset).
  44. to_a
  45. @reply = Message.new(:subject => "RE: #{@message.subject}")
  46. render :action => "show", :layout => false if request.xhr?
  47. end
  48. # Create a new topic
  49. def new
  50. @message = Message.new
  51. @message.author = User.current
  52. @message.board = @board
  53. @message.safe_attributes = params[:message]
  54. if request.post?
  55. @message.save_attachments(params[:attachments])
  56. if @message.save
  57. call_hook(:controller_messages_new_after_save, { :params => params, :message => @message})
  58. render_attachment_warning_if_needed(@message)
  59. flash[:notice] = l(:notice_successful_create)
  60. redirect_to board_message_path(@board, @message)
  61. end
  62. end
  63. end
  64. # Reply to a topic
  65. def reply
  66. @reply = Message.new
  67. @reply.author = User.current
  68. @reply.board = @board
  69. @reply.safe_attributes = params[:reply]
  70. @topic.children << @reply
  71. if !@reply.new_record?
  72. call_hook(:controller_messages_reply_after_save, { :params => params, :message => @reply})
  73. attachments = Attachment.attach_files(@reply, params[:attachments])
  74. render_attachment_warning_if_needed(@reply)
  75. end
  76. flash[:notice] = l(:notice_successful_update)
  77. redirect_to board_message_path(@board, @topic, :r => @reply)
  78. end
  79. # Edit a message
  80. def edit
  81. (render_403; return false) unless @message.editable_by?(User.current)
  82. @message.safe_attributes = params[:message]
  83. if request.post? && @message.save
  84. attachments = Attachment.attach_files(@message, params[:attachments])
  85. render_attachment_warning_if_needed(@message)
  86. flash[:notice] = l(:notice_successful_update)
  87. @message.reload
  88. redirect_to board_message_path(@message.board, @message.root, :r => (@message.parent_id && @message.id))
  89. end
  90. end
  91. # Delete a messages
  92. def destroy
  93. (render_403; return false) unless @message.destroyable_by?(User.current)
  94. r = @message.to_param
  95. @message.destroy
  96. flash[:notice] = l(:notice_successful_delete)
  97. if @message.parent
  98. redirect_to board_message_path(@board, @message.parent, :r => r)
  99. else
  100. redirect_to project_board_path(@project, @board)
  101. end
  102. end
  103. def quote
  104. @subject = @message.subject
  105. @subject = "RE: #{@subject}" unless @subject.starts_with?('RE:')
  106. @content = "#{ll(Setting.default_language, :text_user_wrote, @message.author)}\n> "
  107. @content << @message.content.to_s.strip.gsub(%r{<pre>(.*?)</pre>}m, '[...]').gsub(/(\r?\n|\r\n?)/, "\n> ") + "\n\n"
  108. end
  109. def preview
  110. message = @board.messages.find_by_id(params[:id])
  111. @text = params[:text] ? params[:text] : nil
  112. @previewed = message
  113. render :partial => 'common/preview'
  114. end
  115. private
  116. def find_message
  117. return unless find_board
  118. @message = @board.messages.includes(:parent).find(params[:id])
  119. @topic = @message.root
  120. rescue ActiveRecord::RecordNotFound
  121. render_404
  122. end
  123. def find_board
  124. @board = Board.includes(:project).find(params[:board_id])
  125. @project = @board.project
  126. rescue ActiveRecord::RecordNotFound
  127. render_404
  128. nil
  129. end
  130. end