|
|
|
@ -17,17 +17,25 @@ class MessagesController < ApplicationController
|
|
|
|
|
@page = params[:page] || 1
|
|
|
|
|
@page_size = params[:page_size] || 15
|
|
|
|
|
|
|
|
|
|
sort = params[:sort] || 0
|
|
|
|
|
sort = params[:sort].to_i == 1 ? 'asc' : 'desc'
|
|
|
|
|
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 = @board.messages.root_nodes.by_keywords(params[:search])
|
|
|
|
|
|
|
|
|
|
@messages = sort_by_sticky(@messages)
|
|
|
|
|
@messages = Kaminari.paginate_array(@messages).page(@page).per(@page_size)
|
|
|
|
|
messages = messages.order('(sticky = 1) DESC') # 置顶
|
|
|
|
|
|
|
|
|
|
messages =
|
|
|
|
|
case sort_type
|
|
|
|
|
when 'time' then messages.reorder("created_on #{sort}")
|
|
|
|
|
when 'hot' then messages.reorder("descendants_count #{sort}")
|
|
|
|
|
else messages.reorder("created_on #{sort}")
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
messages = messages.includes(:author)
|
|
|
|
|
@messages = Kaminari.paginate_array(messages).page(@page).per(@page_size)
|
|
|
|
|
|
|
|
|
|
ids = @messages.map(&:id)
|
|
|
|
|
@praises_count_map = Message.where(root_id: ids).group(:root_id).sum(:praises_count)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def reply_list
|
|
|
|
@ -43,33 +51,16 @@ class MessagesController < ApplicationController
|
|
|
|
|
|
|
|
|
|
def reply
|
|
|
|
|
return normal_status(2, "回复内容不能为空") if params[:content].blank?
|
|
|
|
|
begin
|
|
|
|
|
@reply = Message.create!(board: @message.board,
|
|
|
|
|
author: current_user,
|
|
|
|
|
parent: @message,
|
|
|
|
|
@reply = Message.create!(board: @message.board, root: @message.root, 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
|
|
|
|
|