2
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-03-03 23:54:20 +08:00
discourse/app/controllers/reviewable_claimed_topics_controller.rb
Krzysztof Kotlarek f42f5b4708
FIX: Only log unclaimed history when topic was claimed (#36949)
Previously, `log_history(:unclaimed, ...)` was being called whenever
topics were unassigned or destroyed, even when the topic was never
claimed in the first place. This resulted in history entries for
unclaimed actions on topics that had no ReviewableClaimedTopic records.

This fix uses the return value of `delete_all` to check if any rows were
actually deleted before logging the unclaimed history.
2026-01-06 14:52:55 +08:00

58 lines
1.8 KiB
Ruby

# frozen_string_literal: true
class ReviewableClaimedTopicsController < ApplicationController
requires_login
def create
topic = Topic.with_deleted.find_by(id: params[:reviewable_claimed_topic][:topic_id])
automatic = params[:reviewable_claimed_topic][:automatic] == "true"
guardian.ensure_can_claim_reviewable_topic!(topic, automatic)
begin
ReviewableClaimedTopic.create!(user_id: current_user.id, topic_id: topic.id, automatic:)
rescue ActiveRecord::RecordInvalid
return render_json_error(I18n.t("reviewables.conflict"), status: 409)
end
topic.reviewables.find_each { |reviewable| reviewable.log_history(:claimed, current_user) }
notify_users(topic, current_user, automatic)
render json: success_json
end
def destroy
topic = Topic.with_deleted.find_by(id: params[:id])
automatic = params[:automatic] == "true"
raise Discourse::NotFound if topic.blank?
guardian.ensure_can_claim_reviewable_topic!(topic, automatic)
deleted_count = ReviewableClaimedTopic.where(topic_id: topic.id).delete_all
if deleted_count > 0
topic.reviewables.find_each { |reviewable| reviewable.log_history(:unclaimed, current_user) }
end
notify_users(topic, current_user, automatic, claimed: false)
render json: success_json
end
private
def notify_users(topic, user, automatic, claimed: true)
group_ids = Set.new([Group::AUTO_GROUPS[:staff]])
if SiteSetting.enable_category_group_moderation? && topic.category
group_ids.merge(topic.category.moderating_group_ids)
end
data = {
topic_id: topic.id,
user: BasicUserSerializer.new(user, root: false).as_json,
automatic:,
claimed:,
}
MessageBus.publish("/reviewable_claimed", data, group_ids: group_ids.to_a)
Jobs.enqueue(:refresh_users_reviewable_counts, group_ids: group_ids.to_a)
end
end