forgeplus/app/controllers/watchers_controller.rb

62 lines
1.6 KiB
Ruby
Raw Normal View History

2020-03-09 00:40:16 +08:00
class WatchersController < ApplicationController
before_action :require_login, except: %i[index]
2021-08-31 18:00:54 +08:00
before_action :require_profile_completed, only: [:follow]
# before_action :find_project_with_id
before_action :get_target
2020-03-09 00:40:16 +08:00
def index
scope = @target.watchers.includes(:user)
2020-03-09 00:40:16 +08:00
@watchers = paginate(scope)
end
def unfollow
begin
return normal_status(2, "你还没有关注哦") unless current_user.watched?(@target)
current_user.unwatch!(@target)
2020-05-18 14:58:23 +08:00
render_ok({watchers_count: @target.watchers_count, watched: false})
2020-03-09 00:40:16 +08:00
rescue Exception => e
uid_logger_error(e.message)
tip_exception(e.message)
raise ActiveRecord::Rollback
end
end
def follow
begin
return normal_status(2, "你已关注了") if current_user.watched?(@target)
current_user.watch!(@target)
2020-05-18 14:58:23 +08:00
render_ok({watchers_count: @target.watchers_count, watched: true})
2020-03-09 00:40:16 +08:00
rescue Exception => e
uid_logger_error(e.message)
tip_exception(e.message)
raise ActiveRecord::Rollback
end
end
def check_watch
is_watch = current_user.watched?(@target)
2020-03-09 00:40:16 +08:00
render_result(is_watch ? 1 : 0)
rescue Exception => e
uid_logger_error(e.message)
tip_exception(e.message)
end
private
def get_target
2020-05-18 14:58:23 +08:00
target_type = params[:target_type].to_s
case target_type
when "project"
2020-05-18 14:58:23 +08:00
@target = target_type.capitalize.constantize.find_by(id: params[:id])
else
2020-05-18 14:58:23 +08:00
@target = target_type.capitalize.constantize.find_by(login: params[:id]) #用户
end
unless @target.present?
normal_status(-1, "目标不存在")
end
end
2020-03-09 00:40:16 +08:00
end