Skip to content

A platform for community discussion. Free, open, simple.

FIX: Only record deliberate reviewable claims in the timeline (#42375)

Régis Hanol32afc4d1f7fe
diff --git a/app/controllers/reviewable_claimed_topics_controller.rb b/app/controllers/reviewable_claimed_topics_controller.rb
index c7bec08e17a..45ef145e67b 100644
--- a/app/controllers/reviewable_claimed_topics_controller.rb
+++ b/app/controllers/reviewable_claimed_topics_controller.rb
@@ -9,12 +9,13 @@ class ReviewableClaimedTopicsController < ApplicationController
guardian.ensure_can_claim_reviewable_topic!(topic, automatic)
begin
- ReviewableClaimedTopic.create!(user_id: current_user.id, topic_id: topic.id, automatic:)
+ claim =
+ 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) }
+ claim.log_topic_history(:claimed, current_user)
notify_users(topic, current_user, automatic)
render json: success_json
@@ -26,12 +27,13 @@ class ReviewableClaimedTopicsController < ApplicationController
if topic.blank? || !guardian.can_claim_reviewable_topic?(topic, automatic)
raise Discourse::NotFound
end
- 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) }
+
+ if claim = ReviewableClaimedTopic.find_by(topic_id: topic.id)
+ claim.delete
+ claim.log_topic_history(:unclaimed, current_user)
+ notify_users(topic, current_user, claim.automatic, claimed: false)
end
- notify_users(topic, current_user, automatic, claimed: false)
render json: success_json
end
diff --git a/app/models/reviewable_claimed_topic.rb b/app/models/reviewable_claimed_topic.rb
index db61bb08bb6..d05cd365fcf 100644
--- a/app/models/reviewable_claimed_topic.rb
+++ b/app/models/reviewable_claimed_topic.rb
@@ -5,6 +5,15 @@ class ReviewableClaimedTopic < ActiveRecord::Base
belongs_to :user
validates :topic, uniqueness: true
+ def log_topic_history(type, performed_by)
+ return if automatic?
+
+ Reviewable
+ .pending
+ .where(topic_id:)
+ .find_each { |reviewable| reviewable.log_history(type, performed_by) }
+ end
+
def self.claimed_hash(topic_ids)
result = {}
if SiteSetting.reviewable_claiming == "disabled"
diff --git a/app/models/reviewable_flagged_post.rb b/app/models/reviewable_flagged_post.rb
index 9873002ca94..e6f09b37160 100644
--- a/app/models/reviewable_flagged_post.rb
+++ b/app/models/reviewable_flagged_post.rb
@@ -336,10 +336,11 @@ class ReviewableFlaggedPost < Reviewable
def unassign_topic(performed_by, post)
topic = post.topic
return unless topic && performed_by && SiteSetting.reviewable_claiming != "disabled"
- deleted_count = ReviewableClaimedTopic.where(topic_id: topic.id, automatic: false).delete_all
- if deleted_count > 0
- topic.reviewables.find_each { |reviewable| reviewable.log_history(:unclaimed, performed_by) }
- end
+ claim = ReviewableClaimedTopic.find_by(topic_id: topic.id, automatic: false)
+ return if claim.nil?
+
+ claim.delete
+ claim.log_topic_history(:unclaimed, performed_by)
user_ids = User.staff.pluck(:id)
diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml
index 693e87fbccc..26141d02517 100644
--- a/config/locales/client.en.yml
+++ b/config/locales/client.en.yml
@@ -932,10 +932,6 @@ en:
stale_help: "This item has been resolved by <b>%{username}</b>."
claim_help:
optional: "You can claim this item to prevent others from reviewing it."
- required: "You must claim items before you can review them."
- claimed_by_you: "You've claimed this item and can review it."
- claimed_by_other: "This item can only be reviewed by <b>%{username}</b>."
- automatically_claimed_by: "This item is currently being reviewed by <b>%{username}</b>."
claim:
title: "Claim this topic"
unclaim:
diff --git a/frontend/discourse/app/components/reviewable/item.gjs b/frontend/discourse/app/components/reviewable/item.gjs
index b9738ff1c95..85e9c170d13 100644
--- a/frontend/discourse/app/components/reviewable/item.gjs
+++ b/frontend/discourse/app/components/reviewable/item.gjs
@@ -31,6 +31,7 @@ import { clipboardCopy } from "discourse/lib/utilities";
import Category from "discourse/models/category";
import Composer from "discourse/models/composer";
import { PENDING } from "discourse/models/reviewable";
+import { CLAIMED, UNCLAIMED } from "discourse/models/reviewable-history";
import Topic from "discourse/models/topic";
import { eq, not } from "discourse/truth-helpers";
import DButton from "discourse/ui-kit/d-button";
@@ -130,15 +131,6 @@ export default class ReviewableItem extends Component {
}
}
- @computed("reviewable.claimed_by.automatic")
- get autoClaimed() {
- return this.reviewable?.claimed_by?.automatic;
- }
-
- set autoClaimed(value) {
- set(this, "reviewable.claimed_by.automatic", value);
- }
-
@computed(
"reviewable.type",
"reviewable.last_performing_username",
@@ -257,27 +249,6 @@ export default class ReviewableItem extends Component {
return this.siteSettings?.reviewable_claiming !== "required";
}
- @computed("siteSettings.reviewable_claiming", "reviewable.claimed_by")
- get claimHelp() {
- if (this.reviewable?.claimed_by) {
- if (this.reviewable?.claimed_by?.user.id === this.currentUser.id) {
- return i18n("review.claim_help.claimed_by_you");
- } else if (this.reviewable?.claimed_by?.automatic) {
- return i18n("review.claim_help.automatically_claimed_by", {
- username: this.reviewable?.claimed_by?.user.username,
- });
- } else {
- return i18n("review.claim_help.claimed_by_other", {
- username: this.reviewable?.claimed_by?.user.username,
- });
- }
- }
-
- return this.siteSettings?.reviewable_claiming === "optional"
- ? i18n("review.claim_help.optional")
- : i18n("review.claim_help.required");
- }
-
// Find a component to render, if one exists. For example:
// `ReviewableUser` will return `reviewable/user` or `reviewable-user`.
// The former is the new reviewable component, so will only be returned if it exists.
@@ -370,30 +341,25 @@ export default class ReviewableItem extends Component {
return;
}
- const now = new Date().toISOString();
-
const user = this.store.createRecord("user", data.user);
- if (data.claimed) {
- this.reviewable.set("claimed_by", { user, automatic: data.automatic });
- this.reviewable.set("reviewable_histories", [
- ...this.reviewable.reviewable_histories,
- {
- reviewable_history_type: 3,
- created_at: now,
- created_by: user,
- },
- ]);
- } else {
- this.reviewable.set("claimed_by", null);
- this.reviewable.set("reviewable_histories", [
- ...this.reviewable.reviewable_histories,
- {
- reviewable_history_type: 4,
- created_at: now,
- created_by: user,
- },
- ]);
+
+ this.reviewable.set(
+ "claimed_by",
+ data.claimed ? { user, automatic: data.automatic } : null
+ );
+
+ if (data.automatic || this.reviewable.status !== PENDING) {
+ return;
}
+
+ this.reviewable.set("reviewable_histories", [
+ ...this.reviewable.reviewable_histories,
+ {
+ reviewable_history_type: data.claimed ? CLAIMED : UNCLAIMED,
+ created_at: new Date().toISOString(),
+ created_by: user,
+ },
+ ]);
}
@bind
diff --git a/frontend/discourse/app/components/reviewable/timeline.gjs b/frontend/discourse/app/components/reviewable/timeline.gjs
index 94c5a45da79..255bd5ab1fa 100644
--- a/frontend/discourse/app/components/reviewable/timeline.gjs
+++ b/frontend/discourse/app/components/reviewable/timeline.gjs
@@ -10,6 +10,7 @@ import { ajax } from "discourse/lib/ajax";
import { popupAjaxError } from "discourse/lib/ajax-error";
import escape from "discourse/lib/escape";
import { sanitize } from "discourse/lib/text";
+import { CLAIMED, UNCLAIMED } from "discourse/models/reviewable-history";
import { and, eq } from "discourse/truth-helpers";
import DButton from "discourse/ui-kit/d-button";
import DDecoratedHtml from "discourse/ui-kit/d-decorated-html";
@@ -20,9 +21,6 @@ import dFormatDate from "discourse/ui-kit/helpers/d-format-date";
import dIcon from "discourse/ui-kit/helpers/d-icon";
import { i18n } from "discourse-i18n";
-const HISTORY_CLAIMED_ID = 3;
-const HISTORY_UNCLAIMED_ID = 4;
-
/**
* Timeline component for reviewable items that displays chronological events
* including flags, reviews, notes, and target post creation.
@@ -177,7 +175,7 @@ export default class ReviewableTimeline extends Component {
});
this.args.historyEvents?.forEach((history) => {
- if (history.reviewable_history_type === HISTORY_CLAIMED_ID) {
+ if (history.reviewable_history_type === CLAIMED) {
events.push({
type: "claimed",
date: history.created_at,
@@ -185,7 +183,7 @@ export default class ReviewableTimeline extends Component {
icon: "user-plus",
titleKey: "review.timeline.claimed_by",
});
- } else if (history.reviewable_history_type === HISTORY_UNCLAIMED_ID) {
+ } else if (history.reviewable_history_type === UNCLAIMED) {
events.push({
type: "unclaimed",
date: history.created_at,
diff --git a/frontend/discourse/app/helpers/reviewable-history-description.js b/frontend/discourse/app/helpers/reviewable-history-description.js
deleted file mode 100644
index b5b2c06cf9c..00000000000
--- a/frontend/discourse/app/helpers/reviewable-history-description.js
+++ /dev/null
@@ -1,16 +0,0 @@
-import { trustHTML } from "@ember/template";
-import { htmlStatus } from "discourse/helpers/reviewable-status";
-import { iconHTML } from "discourse/lib/icon-library";
-import { EDITED } from "discourse/models/reviewable-history";
-import { i18n } from "discourse-i18n";
-
-export default function reviewableHistoryDescription(rh) {
- switch (rh.reviewable_history_type) {
- case EDITED:
- return trustHTML(
- iconHTML("pencil") + " " + i18n("review.history.edited")
- );
- default:
- return trustHTML(htmlStatus(rh.status));
- }
-}
diff --git a/frontend/discourse/app/models/reviewable-history.js b/frontend/discourse/app/models/reviewable-history.js
index a99e2d1bb4d..0d734f3f3d9 100644
--- a/frontend/discourse/app/models/reviewable-history.js
+++ b/frontend/discourse/app/models/reviewable-history.js
@@ -1,13 +1,6 @@
-import { computed } from "@ember/object";
import RestModel from "discourse/models/rest";
-export const CREATED = 0;
-export const TRANSITIONED_TO = 1;
-export const EDITED = 2;
+export const CLAIMED = 3;
+export const UNCLAIMED = 4;
-export default class ReviewableHistory extends RestModel {
- @computed("reviewable_history_type")
- get created() {
- return this.reviewable_history_type === CREATED;
- }
-}
+export default class ReviewableHistory extends RestModel {}
diff --git a/frontend/discourse/tests/integration/components/reviewable/item-test.gjs b/frontend/discourse/tests/integration/components/reviewable/item-test.gjs
index e9965e45878..f90a71002d3 100644
--- a/frontend/discourse/tests/integration/components/reviewable/item-test.gjs
+++ b/frontend/discourse/tests/integration/components/reviewable/item-test.gjs
@@ -2,9 +2,42 @@ import { getOwner } from "@ember/owner";
import { render } from "@ember/test-helpers";
import { module, test } from "qunit";
import ReviewableItem from "discourse/components/reviewable/item";
+import { APPROVED, PENDING } from "discourse/models/reviewable";
+import { CLAIMED, UNCLAIMED } from "discourse/models/reviewable-history";
import { setupRenderingTest } from "discourse/tests/helpers/component-test";
import { publishToMessageBus } from "discourse/tests/helpers/qunit-helpers";
+function claimableReviewable(context, attrs = {}) {
+ return getOwner(context)
+ .lookup("service:store")
+ .createRecord("reviewable", {
+ id: 789,
+ type: "topic",
+ status: PENDING,
+ topic: { id: 123, fancyTitle: "Test Topic Title" },
+ target_url: "/t/test-topic/123",
+ reviewable_scores: [],
+ reviewable_histories: [],
+ claimed_by: null,
+ ...attrs,
+ });
+}
+
+function publishClaim({ topicId = 123, claimed, automatic = false }) {
+ return publishToMessageBus("/reviewable_claimed", {
+ topic_id: topicId,
+ user: { id: 7, username: "admin" },
+ claimed,
+ automatic,
+ });
+}
+
+function historyTypes(reviewable) {
+ return reviewable.reviewable_histories.map(
+ (history) => history.reviewable_history_type
+ );
+}
+
module("Integration | Component | Reviewable | Item", function (hooks) {
setupRenderingTest(hooks);
@@ -79,56 +112,99 @@ module("Integration | Component | Reviewable | Item", function (hooks) {
</template>
);
- await publishToMessageBus("/reviewable_claimed", {
- topic_id: 123,
- user: { id: 7, username: "admin" },
- claimed: true,
- });
+ await publishClaim({ claimed: true });
assert
.dom(".reviewable-user-info")
.exists("the item still renders and the broadcast is ignored");
});
- test("updates claimed_by when a claim is broadcast for its topic", async function (assert) {
- const claimable = getOwner(this)
- .lookup("service:store")
- .createRecord("reviewable", {
- id: 789,
- type: "topic",
- topic: { id: 123, fancyTitle: "Test Topic Title" },
- target_url: "/t/test-topic/123",
- reviewable_scores: [],
- reviewable_histories: [],
- claimed_by: null,
- });
+ test("tracks claim and release broadcasts for its topic", async function (assert) {
+ const claimable = claimableReviewable(this);
await render(
<template><ReviewableItem @reviewable={{claimable}} /></template>
);
- await publishToMessageBus("/reviewable_claimed", {
- topic_id: 123,
- user: { id: 7, username: "admin" },
- claimed: true,
- });
+ await publishClaim({ claimed: true });
assert.strictEqual(
claimable.claimed_by?.user?.username,
"admin",
"the reviewable is marked as claimed"
);
+ assert.deepEqual(
+ historyTypes(claimable),
+ [CLAIMED],
+ "the claim is recorded in the timeline"
+ );
- await publishToMessageBus("/reviewable_claimed", {
- topic_id: 999,
- user: { id: 8, username: "other-admin" },
- claimed: true,
- });
+ await publishClaim({ claimed: false });
assert.strictEqual(
- claimable.claimed_by?.user?.username,
- "admin",
+ claimable.claimed_by,
+ null,
+ "the reviewable is released"
+ );
+ assert.deepEqual(
+ historyTypes(claimable),
+ [CLAIMED, UNCLAIMED],
+ "the unclaim is recorded in the timeline"
+ );
+
+ await publishClaim({ topicId: 999, claimed: true });
+
+ assert.strictEqual(
+ claimable.claimed_by,
+ null,
"a claim for a different topic is ignored"
);
});
+
+ test("keeps automatic claims out of the timeline history", async function (assert) {
+ const claimable = claimableReviewable(this);
+
+ await render(
+ <template><ReviewableItem @reviewable={{claimable}} /></template>
+ );
+
+ await publishClaim({ claimed: true, automatic: true });
+
+ assert.true(
+ claimable.claimed_by?.automatic,
+ "the transient lock still applies"
+ );
+
+ await publishClaim({ claimed: false, automatic: true });
+
+ assert.deepEqual(
+ historyTypes(claimable),
+ [],
+ "an automatic claim is a transient lock, not a moderation event"
+ );
+ });
+
+ test("keeps claims out of the timeline history of a resolved reviewable", async function (assert) {
+ const resolved = claimableReviewable(this, { status: APPROVED });
+
+ await render(
+ <template><ReviewableItem @reviewable={{resolved}} /></template>
+ );
+
+ await publishClaim({ claimed: true });
+
+ assert.strictEqual(
+ resolved.claimed_by?.user?.username,
+ "admin",
+ "the claim is still reflected on a resolved reviewable"
+ );
+
+ await publishClaim({ claimed: false });
+
+ assert.deepEqual(
+ historyTypes(resolved),
+ [],
+ "the server only logs claims on pending reviewables"
+ );
+ });
});
diff --git a/frontend/discourse/tests/integration/components/reviewable/timeline-test.gjs b/frontend/discourse/tests/integration/components/reviewable/timeline-test.gjs
index dab53501804..66d6e41874c 100644
--- a/frontend/discourse/tests/integration/components/reviewable/timeline-test.gjs
+++ b/frontend/discourse/tests/integration/components/reviewable/timeline-test.gjs
@@ -1,6 +1,7 @@
import { render } from "@ember/test-helpers";
import { module, test } from "qunit";
import ReviewableTimeline from "discourse/components/reviewable/timeline";
+import { CLAIMED, UNCLAIMED } from "discourse/models/reviewable-history";
import { setupRenderingTest } from "discourse/tests/helpers/component-test";
module("Integration | Component | Reviewable | Timeline", function (hooks) {
@@ -52,4 +53,33 @@ module("Integration | Component | Reviewable | Timeline", function (hooks) {
"renders the cooked HTML directly"
);
});
+
+ test("renders claimed and unclaimed history events", async function (assert) {
+ const reviewable = { reviewable_scores: [] };
+ const historyEvents = [
+ {
+ reviewable_history_type: CLAIMED,
+ created_at: "2024-01-01T00:00:00.000Z",
+ created_by: { id: 1, username: "moderator" },
+ },
+ {
+ reviewable_history_type: UNCLAIMED,
+ created_at: "2024-01-02T00:00:00.000Z",
+ created_by: { id: 1, username: "moderator" },
+ },
+ ];
+
+ await render(
+ <template>
+ <ReviewableTimeline
+ @reviewable={{reviewable}}
+ @historyEvents={{historyEvents}}
+ />
+ </template>
+ );
+
+ assert.dom(".timeline-event").exists({ count: 2 });
+ assert.dom(".timeline-event__icon .d-icon-user-plus").exists();
+ assert.dom(".timeline-event__icon .d-icon-user-xmark").exists();
+ });
});
diff --git a/spec/models/reviewable_claimed_topic_spec.rb b/spec/models/reviewable_claimed_topic_spec.rb
index a0db6338a60..e23a2d9f370 100644
--- a/spec/models/reviewable_claimed_topic_spec.rb
+++ b/spec/models/reviewable_claimed_topic_spec.rb
@@ -11,6 +11,30 @@ RSpec.describe ReviewableClaimedTopic, type: :model do
expect(ct.save).to eq(false)
end
+ describe "#log_topic_history" do
+ fab!(:topic)
+ fab!(:moderator)
+ fab!(:pending_reviewable) { Fabricate(:reviewable_flagged_post, topic:) }
+ fab!(:resolved_reviewable) { Fabricate(:reviewable_flagged_post, topic:, status: :approved) }
+
+ it "logs on every pending reviewable of the topic, never on resolved ones" do
+ claim = Fabricate(:reviewable_claimed_topic, topic:, user: moderator)
+
+ claim.log_topic_history(:claimed, moderator)
+
+ expect(pending_reviewable.history.claimed.size).to eq(1)
+ expect(resolved_reviewable.history.claimed).to be_empty
+ end
+
+ it "logs nothing for an automatic claim, which is a transient lock" do
+ claim = Fabricate(:reviewable_claimed_topic, topic:, user: moderator, automatic: true)
+
+ claim.log_topic_history(:claimed, moderator)
+
+ expect(pending_reviewable.history.claimed).to be_empty
+ end
+ end
+
describe "#claimed_hash" do
it "returns the all the claimed topics when reviewable claiming is enabled" do
SiteSetting.reviewable_claiming = "optional"
@@ -26,8 +50,7 @@ RSpec.describe ReviewableClaimedTopic, type: :model do
)
result = ReviewableClaimedTopic.claimed_hash([topic1.id, topic2.id])
- expect(result[topic1.id].topic_id).to eq(topic1.id)
- expect(result[topic2.id].topic_id).to eq(topic2.id)
+ expect(result.keys).to contain_exactly(topic1.id, topic2.id)
end
it "only returns the automatic claimed topics when reviewable claiming is disabled" do
@@ -44,7 +67,7 @@ RSpec.describe ReviewableClaimedTopic, type: :model do
)
result = ReviewableClaimedTopic.claimed_hash([topic1.id, topic2.id])
- expect(result[topic2.id].topic_id).to eq(topic2.id)
+ expect(result.keys).to contain_exactly(topic2.id)
end
end
end
diff --git a/spec/models/reviewable_flagged_post_spec.rb b/spec/models/reviewable_flagged_post_spec.rb
index 1e76b2b5c88..79a46c48fb0 100644
--- a/spec/models/reviewable_flagged_post_spec.rb
+++ b/spec/models/reviewable_flagged_post_spec.rb
@@ -232,37 +232,27 @@ RSpec.describe ReviewableFlaggedPost, type: :model do
fab!(:claimed) { Fabricate(:reviewable_claimed_topic, topic: post.topic, user: moderator) }
before { SiteSetting.reviewable_claiming = "required" }
+
it "clears the claimed topic on resolve" do
reviewable.perform(moderator, :agree_and_keep)
+
expect(reviewable).to be_approved
expect(score.reload).to be_agreed
expect(post).not_to be_hidden
expect(ReviewableClaimedTopic.where(topic_id: post.topic.id).exists?).to eq(false)
- expect(
- post
- .topic
- .reviewables
- .first
- .history
- .where(reviewable_history_type: ReviewableHistory.types[:unclaimed])
- .size,
- ).to eq(1)
+ expect(reviewable.history.unclaimed.size).to eq(1)
end
- it "does not log unclaimed history when topic was not claimed" do
+ it "does not log history or publish when the topic was not claimed" do
claimed.destroy!
- reviewable.perform(moderator, :agree_and_keep)
- expect(reviewable).to be_approved
- expect(score.reload).to be_agreed
- expect(
- post
- .topic
- .reviewables
- .first
- .history
- .where(reviewable_history_type: ReviewableHistory.types[:unclaimed])
- .size,
- ).to eq(0)
+
+ messages =
+ MessageBus.track_publish("/reviewable_claimed") do
+ reviewable.perform(moderator, :agree_and_keep)
+ end
+
+ expect(reviewable.history.unclaimed).to be_empty
+ expect(messages).to be_empty
end
it "publishes reviewable_claimed message with user data when claim is removed" do
diff --git a/spec/requests/reviewable_claimed_topics_controller_spec.rb b/spec/requests/reviewable_claimed_topics_controller_spec.rb
index c21c4123514..6213f773d74 100644
--- a/spec/requests/reviewable_claimed_topics_controller_spec.rb
+++ b/spec/requests/reviewable_claimed_topics_controller_spec.rb
@@ -17,12 +17,11 @@ RSpec.describe ReviewableClaimedTopicsController do
expect(response.status).to eq(403)
end
- context "when logged in as a category group moderator who cannot see the topic" do
+ context "when logged in as a category group moderator" do
fab!(:mod_group, :group)
fab!(:cat_mod_user, :user)
fab!(:private_category) { Fabricate(:private_category, group: Fabricate(:group)) }
fab!(:private_topic) { Fabricate(:topic, category: private_category) }
- fab!(:private_reviewable) { Fabricate(:reviewable_flagged_post, topic: private_topic) }
before do
SiteSetting.enable_category_group_moderation = true
@@ -86,26 +85,11 @@ RSpec.describe ReviewableClaimedTopicsController do
).exists?,
).to eq(false)
end
- end
- context "when logged in as a category group moderator who can see the topic" do
- fab!(:mod_group, :group)
- fab!(:cat_mod_user, :user)
- fab!(:private_category) { Fabricate(:private_category, group: Fabricate(:group)) }
- fab!(:private_topic) { Fabricate(:topic, category: private_category) }
- fab!(:private_reviewable) { Fabricate(:reviewable_flagged_post, topic: private_topic) }
-
- before do
- SiteSetting.enable_category_group_moderation = true
- SiteSetting.reviewable_claiming = "optional"
+ it "allows claiming a topic the user can see" do
private_category.set_permissions(mod_group => :full)
private_category.save!
- Fabricate(:category_moderation_group, category: private_category, group: mod_group)
- mod_group.add(cat_mod_user)
- sign_in(cat_mod_user)
- end
- it "allows claiming a topic the user can see" do
post "/reviewable_claimed_topics.json",
params: {
reviewable_claimed_topic: {
@@ -123,13 +107,13 @@ RSpec.describe ReviewableClaimedTopicsController do
end
end
- context "when logged in" do
+ context "when logged in as a moderator" do
before do
SiteSetting.reviewable_claiming = "optional"
sign_in(moderator)
end
- it "works" do
+ it "claims the topic, logs it, and notifies staff" do
messages =
MessageBus.track_publish("/reviewable_claimed") do
post "/reviewable_claimed_topics.json", params: params
@@ -139,14 +123,7 @@ RSpec.describe ReviewableClaimedTopicsController do
expect(
ReviewableClaimedTopic.where(user_id: moderator.id, topic_id: topic.id).exists?,
).to eq(true)
- expect(
- topic
- .reviewables
- .first
- .history
- .where(reviewable_history_type: ReviewableHistory.types[:claimed])
- .size,
- ).to eq(1)
+ expect(reviewable.history.claimed.size).to eq(1)
expect(messages.size).to eq(1)
message = messages[0]
@@ -159,7 +136,6 @@ RSpec.describe ReviewableClaimedTopicsController do
it "publishes reviewable claimed changes to the category moderators of the topic's category" do
SiteSetting.enable_category_group_moderation = true
- SiteSetting.reviewable_claiming = "optional"
group = Fabricate(:group)
Fabricate(:category_moderation_group, category: topic.category, group:)
@@ -191,14 +167,14 @@ RSpec.describe ReviewableClaimedTopicsController do
).to eq(true)
end
- it "raises an error if user cannot claim the topic" do
+ it "returns 403 when claiming is disabled" do
SiteSetting.reviewable_claiming = "disabled"
post "/reviewable_claimed_topics.json", params: params
expect(response.status).to eq(403)
end
- it "allows claiming when automatic param is present" do
+ it "allows claiming when automatic param is present, without logging history" do
SiteSetting.reviewable_claiming = "disabled"
params[:reviewable_claimed_topic][:topic_id] = automatic_topic.id
params[:reviewable_claimed_topic][:automatic] = "true"
@@ -209,6 +185,7 @@ RSpec.describe ReviewableClaimedTopicsController do
expect(
ReviewableClaimedTopic.where(user_id: moderator.id, topic_id: automatic_topic.id).exists?,
).to eq(true)
+ expect(automatic_reviewable.history.claimed).to be_empty
end
it "raises an error if topic is already claimed" do
@@ -222,17 +199,11 @@ RSpec.describe ReviewableClaimedTopicsController do
end
it "queues a sidekiq job to refresh reviewable counts for users who can see the reviewable" do
- SiteSetting.navigation_menu = "sidebar"
SiteSetting.enable_category_group_moderation = true
- not_notified = Fabricate(:user)
-
group = Fabricate(:group)
Fabricate(:category_moderation_group, category: topic.category, group:)
- notified = Fabricate(:user)
- group.add(notified)
-
expect_enqueued_with(
job: :refresh_users_reviewable_counts,
args: {
@@ -271,7 +242,7 @@ RSpec.describe ReviewableClaimedTopicsController do
end
end
- context "when logged in as a category group moderator who cannot see the topic" do
+ context "when logged in as a category group moderator" do
fab!(:mod_group, :group)
fab!(:cat_mod_user, :user)
fab!(:private_category) { Fabricate(:private_category, group: Fabricate(:group)) }
@@ -302,26 +273,11 @@ RSpec.describe ReviewableClaimedTopicsController do
expect(response.status).to eq(404)
expect(ReviewableClaimedTopic.where(topic_id: private_topic.id).exists?).to eq(true)
end
- end
-
- context "when logged in as a category group moderator who can see the topic" do
- fab!(:mod_group, :group)
- fab!(:cat_mod_user, :user)
- fab!(:private_category) { Fabricate(:private_category, group: Fabricate(:group)) }
- fab!(:private_topic) { Fabricate(:topic, category: private_category) }
- fab!(:private_claimed) { Fabricate(:reviewable_claimed_topic, topic: private_topic) }
- before do
- SiteSetting.enable_category_group_moderation = true
- SiteSetting.reviewable_claiming = "optional"
+ it "allows unclaiming a topic the user can see" do
private_category.set_permissions(mod_group => :full)
private_category.save!
- Fabricate(:category_moderation_group, category: private_category, group: mod_group)
- mod_group.add(cat_mod_user)
- sign_in(cat_mod_user)
- end
- it "allows unclaiming a topic the user can see" do
delete "/reviewable_claimed_topics/#{private_topic.id}.json"
expect(response.status).to eq(200)
@@ -329,109 +285,112 @@ RSpec.describe ReviewableClaimedTopicsController do
end
end
- before { sign_in(moderator) }
+ context "when logged in as a moderator" do
+ before do
+ SiteSetting.reviewable_claiming = "optional"
+ sign_in(moderator)
+ end
- it "works" do
- SiteSetting.reviewable_claiming = "optional"
+ it "releases the claim, logs it, and notifies staff" do
+ messages =
+ MessageBus.track_publish("/reviewable_claimed") do
+ delete "/reviewable_claimed_topics/#{claimed.topic_id}.json"
+ expect(response.status).to eq(200)
+ end
- messages =
- MessageBus.track_publish("/reviewable_claimed") do
- delete "/reviewable_claimed_topics/#{claimed.topic_id}.json"
- expect(response.status).to eq(200)
- end
+ expect(ReviewableClaimedTopic.where(topic_id: claimed.topic_id).exists?).to eq(false)
+ expect(reviewable.history.unclaimed.size).to eq(1)
+ expect(messages.size).to eq(1)
- expect(ReviewableClaimedTopic.where(topic_id: claimed.topic_id).exists?).to eq(false)
- expect(
- topic
- .reviewables
- .first
- .history
- .where(reviewable_history_type: ReviewableHistory.types[:unclaimed])
- .size,
- ).to eq(1)
- expect(messages.size).to eq(1)
-
- message = messages[0]
-
- expect(message.data[:topic_id]).to eq(topic.id)
- expect(message.data[:user][:id]).to eq(moderator.id)
- expect(message.data[:claimed]).to be false
- expect(message.group_ids).to contain_exactly(Group::AUTO_GROUPS[:staff])
- end
+ message = messages[0]
+
+ expect(message.data[:topic_id]).to eq(topic.id)
+ expect(message.data[:user][:id]).to eq(moderator.id)
+ expect(message.data[:claimed]).to be false
+ expect(message.group_ids).to contain_exactly(Group::AUTO_GROUPS[:staff])
+ end
+
+ it "works with deleted topics" do
+ first_post = topic.first_post || Fabricate(:post, topic: topic)
+ PostDestroyer.new(Discourse.system_user, first_post, context: "Automated testing").destroy
- it "works with deleted topics" do
- SiteSetting.reviewable_claiming = "optional"
- first_post = topic.first_post || Fabricate(:post, topic: topic)
- PostDestroyer.new(Discourse.system_user, first_post, context: "Automated testing").destroy
+ delete "/reviewable_claimed_topics/#{claimed.topic_id}.json"
- delete "/reviewable_claimed_topics/#{claimed.topic_id}.json"
+ expect(response.status).to eq(200)
+ expect(
+ ReviewableClaimedTopic.where(user_id: moderator.id, topic_id: topic.id).exists?,
+ ).to eq(false)
+ end
- expect(response.status).to eq(200)
- expect(ReviewableClaimedTopic.where(user_id: moderator.id, topic_id: topic.id).exists?).to eq(
- false,
- )
- end
+ it "raises an error if topic is missing" do
+ delete "/reviewable_claimed_topics/111111111.json"
- it "raises an error if topic is missing" do
- delete "/reviewable_claimed_topics/111111111.json"
+ expect(response.status).to eq(404)
+ end
- expect(response.status).to eq(404)
- end
+ it "returns 404 when claiming is disabled" do
+ SiteSetting.reviewable_claiming = "disabled"
- it "returns 404 if user cannot claim the topic" do
- delete "/reviewable_claimed_topics/#{claimed.topic_id}.json"
+ delete "/reviewable_claimed_topics/#{claimed.topic_id}.json"
- expect(response.status).to eq(404)
- end
+ expect(response.status).to eq(404)
+ end
- it "allows unclaiming when automatic param is present" do
- SiteSetting.reviewable_claiming = "disabled"
+ it "allows unclaiming when automatic param is present, without logging history" do
+ SiteSetting.reviewable_claiming = "disabled"
- delete "/reviewable_claimed_topics/#{automatic_claimed.topic_id}.json?automatic=true"
- expect(response.status).to eq(200)
- expect(
- ReviewableClaimedTopic.where(user_id: moderator.id, topic_id: automatic_topic.id).exists?,
- ).to eq(false)
- end
+ delete "/reviewable_claimed_topics/#{automatic_claimed.topic_id}.json?automatic=true"
+ expect(response.status).to eq(200)
+ expect(
+ ReviewableClaimedTopic.where(user_id: moderator.id, topic_id: automatic_topic.id).exists?,
+ ).to eq(false)
+ expect(automatic_reviewable.history.unclaimed).to be_empty
+ end
- it "queues a sidekiq job to refresh reviewable counts for users who can see the reviewable" do
- SiteSetting.reviewable_claiming = "optional"
- SiteSetting.navigation_menu = "sidebar"
- SiteSetting.enable_category_group_moderation = true
+ it "does not log history when the claim being removed was automatic" do
+ claimed.update!(automatic: true)
- not_notified = Fabricate(:user)
+ messages =
+ MessageBus.track_publish("/reviewable_claimed") do
+ delete "/reviewable_claimed_topics/#{claimed.topic_id}.json"
+ expect(response.status).to eq(200)
+ end
- group = Fabricate(:group)
- Fabricate(:category_moderation_group, category: topic.category, group:)
+ expect(ReviewableClaimedTopic.where(topic_id: topic.id).exists?).to eq(false)
+ expect(reviewable.history.unclaimed).to be_empty
+ expect(messages.size).to eq(1)
+ expect(messages.first.data[:automatic]).to eq(true)
+ end
- notified = Fabricate(:user)
- group.add(notified)
+ it "queues a sidekiq job to refresh reviewable counts for users who can see the reviewable" do
+ SiteSetting.enable_category_group_moderation = true
- expect_enqueued_with(
- job: :refresh_users_reviewable_counts,
- args: {
- group_ids: [Group::AUTO_GROUPS[:staff], group.id],
- },
- ) do
- delete "/reviewable_claimed_topics/#{claimed.topic_id}.json"
- expect(response.status).to eq(200)
+ group = Fabricate(:group)
+ Fabricate(:category_moderation_group, category: topic.category, group:)
+
+ expect_enqueued_with(
+ job: :refresh_users_reviewable_counts,
+ args: {
+ group_ids: [Group::AUTO_GROUPS[:staff], group.id],
+ },
+ ) do
+ delete "/reviewable_claimed_topics/#{claimed.topic_id}.json"
+ expect(response.status).to eq(200)
+ end
end
- end
- it "does not log unclaimed history when topic was not claimed" do
- SiteSetting.reviewable_claiming = "optional"
- claimed.destroy!
-
- delete "/reviewable_claimed_topics/#{topic.id}.json"
- expect(response.status).to eq(200)
- expect(
- topic
- .reviewables
- .first
- .history
- .where(reviewable_history_type: ReviewableHistory.types[:unclaimed])
- .size,
- ).to eq(0)
+ it "does not log history or publish when the topic was not claimed" do
+ claimed.destroy!
+
+ messages =
+ MessageBus.track_publish("/reviewable_claimed") do
+ delete "/reviewable_claimed_topics/#{topic.id}.json"
+ expect(response.status).to eq(200)
+ end
+
+ expect(reviewable.history.unclaimed).to be_empty
+ expect(messages).to be_empty
+ end
end
end
end