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 5.1KB

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