实训评论的回复

dev_trainings
daiao 6 years ago
parent 0901c8baa0
commit 4072011dca

@ -69,8 +69,20 @@ class ShixunsService
dis = Shixun.select([:id, :user_id]).find(dis_id)
dis_type = params[:container_type] # 如:"Shixun"
# 总数,分页使用
disscuss_count = Discuss.where(:dis_id => dis_id, :dis_type => dis_type, :root_id => nil).count
discusses = Discuss.limit(LIMIT).where(:dis_id => dis_id, :dis_type => dis_type, :root_id => nil).includes(:user, :praise_tread).offset(offset)
if current_user.admin?
disscuss_count = Discuss.where(:dis_id => dis_id, :dis_type => dis_type, :root_id => nil).count
discusses = Discuss.limit(LIMIT).where(:dis_id => dis_id, :dis_type => dis_type,
:root_id => nil).includes(:user, :praise_tread).offset(offset)
else
disscusses = Discuss.where("dis_id = :dis_id and dis_type = :dis_type and root_id = :root_id and
(hidden = :hidden or user_id = :user_id)",
{dis_id: dis_id, dis_type: dis_type, root_id: nil, hidden: false,
user_id: current_user.id})
disscuss_count = disscusses.count
discusses = disscusses.limit(LIMIT).includes(:user, :praise_tread).offset(offset)
end
base_data discusses, dis, current_user
return {:children_list => @children_list, :disscuss_count => disscuss_count}
@ -119,9 +131,6 @@ class ShixunsService
@children_list = []
# 目前只取十个不多N+1问题不大
# 需要彻底解决则需要改数据路结构比如Nested算法可以解决
unless current_user.admin?
discusses = discusses.where(:hidden => false)
end
if discusses.present?
discusses.each do |d|
# 总点赞数
@ -145,7 +154,13 @@ class ShixunsService
:user_praise => user_praise, :admin => current_user.admin?}
# 现在没有二级回复所以查询的时候直接从root_id取
children = Discuss.where(:root_id => d.id).includes(:user).reorder("created_at asc")
children =
if current_user.admin?
Discuss.where(root_id: d.id).includes(:user).reorder("created_at asc")
else
Discuss.where("root_id = :root_id and (hidden = :hidden or user_id = :user_id",
{root_id: d.id, hidden: false, user_id: current_user.id}).includes(:user).reorder("created_at asc")
end
@children_list << parents.merge({:children => (children.map{|child| [:content => child.content, :time => time_from_now(child.created_at), :position => child.position , :reward => child.reward,:hidden => child.hidden,
:image_url => url_to_avatar(child.user), :username => child.username, :user_id => child.user_id, :user_login => child.user.try(:login),

Loading…
Cancel
Save