2019-05-13 09:55:44 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-05-08 22:20:51 +08:00
|
|
|
class ReviewableClaimedTopicsController < ApplicationController
|
|
|
|
requires_login
|
|
|
|
|
|
|
|
def create
|
2020-02-25 21:49:23 +08:00
|
|
|
topic = Topic.with_deleted.find_by(id: params[:reviewable_claimed_topic][:topic_id])
|
2019-05-08 22:20:51 +08:00
|
|
|
guardian.ensure_can_claim_reviewable_topic!(topic)
|
2020-02-22 01:11:50 +08:00
|
|
|
|
|
|
|
begin
|
|
|
|
ReviewableClaimedTopic.create!(user_id: current_user.id, topic_id: topic.id)
|
|
|
|
rescue ActiveRecord::RecordInvalid
|
|
|
|
return render_json_error(I18n.t("reviewables.conflict"), status: 409)
|
|
|
|
end
|
|
|
|
|
2020-02-11 07:40:01 +08:00
|
|
|
topic.reviewables.find_each { |reviewable| reviewable.log_history(:claimed, current_user) }
|
2020-02-22 01:11:50 +08:00
|
|
|
|
|
|
|
notify_users(topic, current_user)
|
2019-05-08 22:20:51 +08:00
|
|
|
render json: success_json
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
2020-02-25 21:49:23 +08:00
|
|
|
topic = Topic.with_deleted.find_by(id: params[:id])
|
2019-05-08 22:20:51 +08:00
|
|
|
raise Discourse::NotFound if topic.blank?
|
|
|
|
|
|
|
|
guardian.ensure_can_claim_reviewable_topic!(topic)
|
|
|
|
ReviewableClaimedTopic.where(topic_id: topic.id).delete_all
|
2020-02-11 07:40:01 +08:00
|
|
|
topic.reviewables.find_each { |reviewable| reviewable.log_history(:unclaimed, current_user) }
|
2019-05-08 22:20:51 +08:00
|
|
|
|
2020-02-22 01:11:50 +08:00
|
|
|
notify_users(topic, nil)
|
2019-05-08 22:20:51 +08:00
|
|
|
render json: success_json
|
|
|
|
end
|
2020-02-22 01:11:50 +08:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def notify_users(topic, claimed_by)
|
2022-11-25 08:07:29 +08:00
|
|
|
group_ids = Set.new([Group::AUTO_GROUPS[:staff]])
|
2020-02-22 01:11:50 +08:00
|
|
|
|
2020-07-15 00:36:19 +08:00
|
|
|
if SiteSetting.enable_category_group_moderation? &&
|
|
|
|
group_id = topic.category&.reviewable_by_group_id.presence
|
2022-11-25 08:07:29 +08:00
|
|
|
group_ids.add(group_id)
|
2020-02-22 01:11:50 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
if claimed_by.present?
|
|
|
|
data = { topic_id: topic.id, user: BasicUserSerializer.new(claimed_by, root: false).as_json }
|
|
|
|
else
|
|
|
|
data = { topic_id: topic.id }
|
|
|
|
end
|
|
|
|
|
2022-11-25 08:07:29 +08:00
|
|
|
MessageBus.publish("/reviewable_claimed", data, group_ids: group_ids.to_a)
|
2022-12-08 09:44:29 +08:00
|
|
|
|
2023-10-09 07:24:10 +08:00
|
|
|
Jobs.enqueue(:refresh_users_reviewable_counts, group_ids: group_ids.to_a)
|
2020-02-22 01:11:50 +08:00
|
|
|
end
|
2019-05-08 22:20:51 +08:00
|
|
|
end
|