discourse/app/controllers/reviewable_claimed_topics_controller.rb
Krzysztof Kotlarek a4b3f74497
FEATURE: Add claim history to reviewable timeline (#36243)
This enhances the reviewable timeline by tracking and displaying when
reviewables are claimed and unclaimed by moderators.

Changes include:
- Modified reviewable_claimed_topics_controller to send user information
and claimed status for both claim and unclaim operations
- Added reviewable_histories to ReviewableSerializer to expose history
data
- Updated ReviewableItem component to track claim/unclaim events via
MessageBus and add them to the reviewable's history
- Fixed MessageBus initialization to start in Rails testing environment
to support system tests

<img width="1111" height="741" alt="Screenshot 2025-11-26 at 3 24 28 pm"
src="https://github.com/user-attachments/assets/84e0bf2a-adb3-4ed2-93cb-eda6d771f5af"
/>
2025-12-01 14:58:43 +08:00

56 lines
1.8 KiB
Ruby
Vendored

# 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)
ReviewableClaimedTopic.where(topic_id: topic.id).delete_all
topic.reviewables.find_each { |reviewable| reviewable.log_history(:unclaimed, current_user) }
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