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.
educoder/app/controllers/discusses_controller.rb

128 lines
5.2 KiB

6 years ago
class DiscussesController < ApplicationController
LIMIT = 10
before_action :find_container, only: [:index, :hidden]
before_action :find_discuss, except: [:create, :index, :new_message, :reward_code]
def index
page = params[:page].to_i
offset = page * LIMIT
# 总数,分页使用
@disscuss_count = Discuss.where(:dis_id => @container.id, :dis_type => @container.class.to_s, :root_id => nil).count
@discusses = Discuss.limit(LIMIT).where(:dis_id => @container.id, :dis_type => @container.class.to_s, :root_id => nil).
includes(:user, :praise_tread).offset(offset)
@current_user = current_user
end
def new_message
onclick_time = Myshixun.find(params[:myshixun_id]).try(:onclick_time)
ids = Discuss.where(user_id: User.current.id, dis_id: params[:container_id], dis_type: params[:container_type]).
where("parent_id is null").pluck(:id)
@user_discuss = Discuss.where("user_id !=? and created_at >?", User.current.id, onclick_time).where(parent_id: ids,
dis_id: params[:container_id], dis_type: params[:container_type]).first
end
def create
begin
@discuss = Discuss.create!(:dis_id => params[:container_id], :dis_type => params[:container_type], :content => params[:content].gsub("&nbsp\;", "").strip,
:user_id => current_user.id, :praise_count => 0, :position => params[:position], :challenge_id => params[:challenge_id])
rescue Exception => e
uid_logger_error("create discuss failed : #{e.message}")
raise Educoder::TipException.new("评论异常")
end
end
def reply
begin
@discuss = Discuss.create!(:content => params[:content].gsub("&nbsp\;", "").strip, :user_id => current_user.id,
:root_id => @discuss.root_id || params[:id], :parent_id => params[:id], :praise_count => 0,
:challenge_id => @discuss.challenge_id, :dis_id => @discuss.dis_id,
:dis_type => @discuss.dis_type, :position => @discuss.position)
rescue Exception => e
uid_logger_error("reply discuss failed : #{e.message}")
raise Educoder::TipException.new("回复评论异常")
end
end
# 隐藏评论
def hidden
logger.info("################{current_user.id}")
if current_user.manager_of_shixun?(@container)
logger.info("66666666#{current_user.id}")
@discuss.update_attribute(:hidden, params[:hidden] == "1")
sucess_status
else
Educoder::TipException(403, "..")
end
end
# 点/取消赞,一个用户只能点一次
# 0 取消赞;
def plus
pt = PraiseTread.where(:praise_tread_object_id => params[:id], :praise_tread_object_type => params[:container_type],
:user_id => current_user, :praise_or_tread => 1).first
# 如果当前用户已赞过,则不能重复赞
if params[:type] == 1 && pt.blank?
PraiseTread.create!(:praise_tread_object_id => params[:id], :praise_tread_object_type => params[:container_type],
:user_id => current_user.id, :praise_or_tread => 1) if pt.blank?
else
pt.destroy if pt.present? # 如果已赞过,则删掉这条赞(取消);如果没赞过,则为非法请求不处理
end
@praise_count = PraiseTread.where(:praise_tread_object_id => params[:id], :praise_tread_object_type => params[:container_type],
:praise_or_tread => 1).count
end
# 奖励金币
def reward_code
unless current_user.admin?
Educoder::TipException(403, "..")
end
ActiveRecord::Base.transaction do
container_id = params[:id]
container_type = params[:container_type]
score = params[:score]
user_id = params[:user_id]
user = User.select([:id, :login, :grade]).find(user_id)
# 金币来源记录
Grade.create!(:user_id => user_id, :container_id => container_id, :container_type => container_type, :score => score)
# 更新用户总金币数
user.update_column(:grade, (score.to_i + user.grade.to_i))
# 多种类型都可以奖励金币
case container_type
# Post为帖子回复类型这个名字取的...
when 'Memo', 'Post'
container = Memo.find(container_id)
score = score.to_i + container.reward.to_i
container.update_attribute(:reward, score)
when 'Discusses'
container = Discuss.select([:id, :reward]).find(params[:id])
score = score.to_i + container.reward.to_i
container.update_attribute(:reward, score)
end
@code = score
end
end
def destroy
begin
@discuss.destroy
sucess_status
rescue Exception => e
uid_logger_error("destroy discuss failed: #{e.message}")
Educoder::TipException("帖子删除异常")
end
end
private
def find_container
@container = Shixun.select([:id, :user_id]).find_by_identifier(params[:container_identifier])
end
def find_discuss
@discuss = Discuss.select([:id, :hidden, :reward, :dis_type, :dis_id, :position, :challenge_id, :root_id]).find(params[:id])
end
end