class MessagesController < ApplicationController include MessagesHelper SORT_TYPE = %w[time hot] before_action :require_login, only: %i[create update sticky_top bulk_delete create destroy bulk_send bulk_move bulk_public] before_action :find_board, only: [:create, :index, :bulk_delete, :bulk_move, :bulk_send, :bulk_public] before_action :find_message, only: [:update, :destroy, :sticky_top, :reply_list, :destroy, :reply] before_action :validate_delete_params, only: %i[bulk_delete bulk_public] before_action :message_validate_create_params, only: :create before_action :validate_update_params, only: :update before_action :validate_sort_type, only: :index before_action :validate_send_message_to_course_params, only: :bulk_send before_action :validate_move_params, only: :bulk_move def index @page = params[:page] || 1 @page_size = params[:page_size] || 15 sort = params[:sort] || 0 sort_type = params[:sort_type] || 'time' sort = sort.to_i sort_type = sort_type.strip @messages = @board.messages.root_nodes.by_keywords(params[:search]).includes(:praise_treads, :author, :children) @messages = @messages.ordered(sort: sort, sort_type: sort_type) @messages = sort_by_all_replies(sort, sort_type, @messages) @messages = sort_by_sticky(@messages) @messages = Kaminari.paginate_array(@messages).page(@page).per(@page_size) end def reply_list @page = params[:page] || 1 @page_size = params[:page_size] || 10 @current_user = current_user || nil @messages = @message.children.preload_messages @messages = @messages.ordered(sort: 1) unless @message.parent_id.nil? @messages = @messages.page(@page).per(@page_size) end def reply return normal_status(2, "回复内容不能为空") if params[:content].blank? begin @reply = Message.create!(board: @message.board, author: current_user, parent: @message, message_detail_attributes: { content: params[:content] } ) rescue Exception => e uid_logger_error(e.message) tip_exception(e.message) raise ActiveRecord::Rollback end end def sticky_top return normal_status(403, "您没有权限进行该操作") unless current_user.teacher_of_course?(@message.board.course) ActiveRecord::Base.transaction do begin @message.update_attributes(:sticky => @message.sticky == 1 ? 0 : 1) rescue Exception => e uid_logger_error(e.message) tip_exception(e.message) raise ActiveRecord::Rollback end end end def bulk_delete ActiveRecord::Base.transaction do begin @messages = @board.messages.by_ids(params[:ids]) @messages.destroy_all rescue Exception => e tip_exception(e.message) raise ActiveRecord::Rollback end end end def new @message = Message.new end def show @message = Message.includes(:attachments, :message_detail, :children, :author => :user_extension, :board => [{course: :board_course_modules}]).find_by_id params[:id] return normal_status(-2, "ID为#{params[:id]}的帖子不存在") if @message.nil? @attachment_size = @message.attachments.size @message.update_visits @current_user = current_user end def update return normal_status(403, "您没有权限进行该操作") if current_user != @message.author && !current_user.teacher_of_course?(@message.board.course) begin @message.update_attributes(message_params) Attachment.associate_container(params[:attachment_ids], @message.id, @message.class.name) @message.update_content(params[:content]) rescue Exception => e uid_logger_error(e.message) tip_exception("修改失败") raise ActiveRecord::Rollback end end def create return normal_status(403, "您没有权限进行该操作") unless current_user.admin? || current_user.member_of_course?(@board.course) begin @message = Message.new(message_params) @message.author = current_user @message.board_id = params[:select_board_id] @message.message_detail_attributes = {content: params[:content]} @message.save! Attachment.associate_container(params[:attachment_ids], @message.id, @message.class.name) rescue Exception => e uid_logger_error(e.message) tip_exception(e.message) raise ActiveRecord::Rollback end end def destroy begin return normal_status(403, "您没有权限进行该操作") unless @message.author == current_user || current_user.teacher_of_course?(@message.board.course) @message.destroy! rescue Exception => e uid_logger_error(e.message) tip_exception(e.message) raise ActiveRecord::Rollback end end def bulk_send return normal_status(403) unless current_user.teacher_or_admin?(@board.course) ids = params[:ids] course_ids = params[:to_course_ids] begin ids.each do |id| @message = Message.find_by_id id if @message.try(:parent_id).nil? # TODO 暂时只支持目录下的跟节点发送 course_ids.each do |course_id| course = Course.find course_id new_message = Message.create!(board: course.course_board, subject: @message.subject, author: current_user, message_detail_attributes: { content: @message.try(:message_detail).try(:content) } ) @message.copy_attachments_to_new_message(new_message, current_user) end end end rescue Exception => e uid_logger_error(e.message) tip_exception(e.message) raise ActiveRecord::Rollback end end def bulk_move # 课堂的目录之间移动,有子栏目的才显示此项 return normal_status(403) unless current_user.teacher_of_course?(@board.course) Message.bulk_move_to_other_board(params[:ids], params[:to_board_id]) end def bulk_public @messages = @board.messages.root_nodes.by_ids Array(params[:ids]) @messages.update_all(is_public: true) end private def validate_sort_type normal_status(2, "参数sort_tyope暂时只支持 'time', 'hot'两种") if params.has_key?(:sort_type) && !SORT_TYPE.include?(params[:sort_type].strip) end def find_message begin @message = Message.find params[:id] rescue Exception => e uid_logger_error(e.message) tip_exception(e.message) end end def message_params params.require(:message).permit(:subject, :sticky) end end