A platform for community discussion. Free, open, simple.
DEV: Add a ReviewableNote model, and tie it into the Reviewable model. (#33047)
Gary Pendergast599404de3142
diff --git a/app/controllers/reviewable_notes_controller.rb b/app/controllers/reviewable_notes_controller.rbnew file mode 100644index 00000000000..5d397e7dacf--- /dev/null+++ b/app/controllers/reviewable_notes_controller.rb@@ -0,0 +1,38 @@+# frozen_string_literal: true++class ReviewableNotesController < ApplicationController+ before_action :find_reviewable++ def create+ note = @reviewable.reviewable_notes.build(note_params)+ note.user = current_user++ if note.save+ # Reload to ensure associations are loaded+ note.reload+ render json: ReviewableNoteSerializer.new(note, scope: guardian, root: false)+ else+ render json: { errors: note.errors.full_messages }, status: 422+ end+ end++ def destroy+ note = @reviewable.reviewable_notes.find(params[:id])++ # Only allow the author or admin to delete notes+ raise Discourse::InvalidAccess unless note.user == current_user || current_user.admin?++ note.destroy!+ render json: success_json+ end++ private++ def find_reviewable+ @reviewable = Reviewable.find(params[:reviewable_id])+ end++ def note_params+ params.require(:reviewable_note).permit(:content)+ end+enddiff --git a/app/models/reviewable.rb b/app/models/reviewable.rbindex 4139e75041f..70a41076b73 100644--- a/app/models/reviewable.rb+++ b/app/models/reviewable.rb@@ -33,6 +33,7 @@ class Reviewable < ActiveRecord::Basehas_many :reviewable_histories, dependent: :destroyhas_many :reviewable_scores, -> { order(created_at: :desc) }, dependent: :destroy+ has_many :reviewable_notes, -> { order(created_at: :asc) }, dependent: :destroyenum :status, { pending: 0, approved: 1, rejected: 2, ignored: 3, deleted: 4 }@@ -428,13 +429,16 @@ class Reviewable < ActiveRecord::Baseif preloadresult =- result.includes(- { created_by: :user_stat },- :topic,- :target,- :target_created_by,- :reviewable_histories,- ).includes(reviewable_scores: { user: :user_stat, meta_topic: :posts })+ result+ .includes(+ { created_by: :user_stat },+ :topic,+ :target,+ :target_created_by,+ :reviewable_histories,+ )+ .includes(reviewable_scores: { user: :user_stat, meta_topic: :posts })+ .includes(reviewable_notes: { user: :user_stat })endreturn result if user.admin?diff --git a/app/models/reviewable_note.rb b/app/models/reviewable_note.rbnew file mode 100644index 00000000000..320a2068094--- /dev/null+++ b/app/models/reviewable_note.rb@@ -0,0 +1,37 @@+# frozen_string_literal: true++class ReviewableNote < ActiveRecord::Base+ MAX_CONTENT_LENGTH = 2000++ belongs_to :reviewable+ belongs_to :user++ validates :content, presence: true, length: { minimum: 1, maximum: MAX_CONTENT_LENGTH }+ validates :reviewable_id, presence: true+ validates :user_id, presence: true++ scope :ordered, -> { order(:created_at) }+end++# == Schema Information+#+# Table name: reviewable_notes+#+# id :bigint not null, primary key+# reviewable_id :bigint not null+# user_id :bigint not null+# content :text not null+# created_at :datetime not null+# updated_at :datetime not null+#+# Indexes+#+# index_reviewable_notes_on_reviewable_id (reviewable_id)+# index_reviewable_notes_on_reviewable_id_and_created_at (reviewable_id,created_at)+# index_reviewable_notes_on_user_id (user_id)+#+# Foreign Keys+#+# fk_rails_... (reviewable_id => reviewables.id)+# fk_rails_... (user_id => users.id)+#diff --git a/app/serializers/reviewable_note_serializer.rb b/app/serializers/reviewable_note_serializer.rbnew file mode 100644index 00000000000..54a5858b1a3--- /dev/null+++ b/app/serializers/reviewable_note_serializer.rb@@ -0,0 +1,7 @@+# frozen_string_literal: true++class ReviewableNoteSerializer < ApplicationSerializer+ attributes :id, :content, :created_at, :updated_at++ has_one :user, serializer: BasicUserSerializer, embed: :objects+enddiff --git a/app/serializers/reviewable_serializer.rb b/app/serializers/reviewable_serializer.rbindex 1774dea4871..bac1fc60970 100644--- a/app/serializers/reviewable_serializer.rb+++ b/app/serializers/reviewable_serializer.rb@@ -28,6 +28,7 @@ class ReviewableSerializer < ApplicationSerializerhas_many :editable_fields, serializer: ReviewableEditableFieldSerializer, embed: :objectshas_many :reviewable_scores, serializer: ReviewableScoreSerializerhas_many :bundled_actions, serializer: ReviewableBundledActionSerializer+ has_many :reviewable_notes, serializer: ReviewableNoteSerializerhas_one :claimed_by, serializer: ReviewableClaimedTopicSerializer# Used to keep track of our payload attributesdiff --git a/config/routes.rb b/config/routes.rbindex 577dde79e37..7b0d18f816a 100644--- a/config/routes.rb+++ b/config/routes.rb@@ -526,6 +526,13 @@ Discourse::Application.routes.draw doresources :reviewable_claimed_topics, only: %i[create destroy]+ resources :reviewables, only: [] do+ resources :reviewable_notes,+ only: %i[create destroy],+ path: "notes",+ constraints: StaffConstraint.new+ end+get "session/sso" => "session#sso"get "session/sso_login" => "session#sso_login"get "session/sso_provider" => "session#sso_provider"diff --git a/db/migrate/20250603051201_create_reviewable_notes.rb b/db/migrate/20250603051201_create_reviewable_notes.rbnew file mode 100644index 00000000000..88847d699e9--- /dev/null+++ b/db/migrate/20250603051201_create_reviewable_notes.rb@@ -0,0 +1,14 @@+# frozen_string_literal: true++class CreateReviewableNotes < ActiveRecord::Migration[7.0]+ def change+ create_table :reviewable_notes do |t|+ t.references :reviewable, null: false, foreign_key: true+ t.references :user, null: false, foreign_key: true+ t.text :content, null: false+ t.timestamps+ end++ add_index :reviewable_notes, %i[reviewable_id created_at]+ end+enddiff --git a/spec/fabricators/reviewable_note_fabricator.rb b/spec/fabricators/reviewable_note_fabricator.rbnew file mode 100644index 00000000000..f4a7b69b9a6--- /dev/null+++ b/spec/fabricators/reviewable_note_fabricator.rb@@ -0,0 +1,7 @@+# frozen_string_literal: true++Fabricator(:reviewable_note) do+ content { "This is a sample reviewable note for testing purposes." }+ user { Fabricate(:admin) }+ reviewable { Fabricate(:reviewable_flagged_post) }+enddiff --git a/spec/models/reviewable_note_spec.rb b/spec/models/reviewable_note_spec.rbnew file mode 100644index 00000000000..097a750b922--- /dev/null+++ b/spec/models/reviewable_note_spec.rb@@ -0,0 +1,84 @@+# frozen_string_literal: true++RSpec.describe ReviewableNote do+ fab!(:admin)+ fab!(:moderator)+ fab!(:user)+ fab!(:reviewable) { Fabricate(:reviewable_flagged_post) }++ describe "associations" do+ it { is_expected.to belong_to(:reviewable) }+ it { is_expected.to belong_to(:user) }+ end++ describe "validations" do+ subject { build(:reviewable_note, reviewable: reviewable, user: admin) }++ it { is_expected.to validate_presence_of(:content) }+ it do+ is_expected.to validate_length_of(:content).is_at_least(1).is_at_most(+ ReviewableNote::MAX_CONTENT_LENGTH,+ )+ end+ it { is_expected.to validate_presence_of(:reviewable_id) }+ it { is_expected.to validate_presence_of(:user_id) }++ context "when content is blank" do+ it "is invalid with empty content" do+ note = build(:reviewable_note, content: "", reviewable: reviewable, user: admin)+ expect(note).not_to be_valid+ expect(note.errors[:content]).to include("can't be blank")+ end++ it "is invalid with whitespace-only content" do+ note = build(:reviewable_note, content: " ", reviewable: reviewable, user: admin)+ expect(note).not_to be_valid+ expect(note.errors[:content]).to include("can't be blank")+ end+ end++ context "when content is too long" do+ it "is invalid with content over #{ReviewableNote::MAX_CONTENT_LENGTH} characters" do+ long_content = "a" * (ReviewableNote::MAX_CONTENT_LENGTH + 1)+ note = build(:reviewable_note, content: long_content, reviewable: reviewable, user: admin)+ expect(note).not_to be_valid+ expect(note.errors[:content]).to include(+ "is too long (maximum is #{ReviewableNote::MAX_CONTENT_LENGTH} characters)",+ )+ end++ it "is valid with content at exactly #{ReviewableNote::MAX_CONTENT_LENGTH} characters" do+ max_content = "a" * ReviewableNote::MAX_CONTENT_LENGTH+ note = build(:reviewable_note, content: max_content, reviewable: reviewable, user: admin)+ expect(note).to be_valid+ end+ end+ end++ describe "scopes" do+ fab!(:old_note) do+ Fabricate(:reviewable_note, reviewable: reviewable, user: admin, created_at: 2.days.ago)+ end+ fab!(:new_note) do+ Fabricate(:reviewable_note, reviewable: reviewable, user: moderator, created_at: 1.day.ago)+ end++ describe ".ordered" do+ it "returns notes ordered by creation date ascending" do+ notes = ReviewableNote.ordered+ expect(notes.first).to eq(old_note)+ expect(notes.last).to eq(new_note)+ end+ end+ end++ describe "factory" do+ it "creates a valid reviewable note" do+ note = Fabricate(:reviewable_note, reviewable: reviewable, user: admin)+ expect(note).to be_valid+ expect(note.content).to be_present+ expect(note.reviewable).to eq(reviewable)+ expect(note.user).to eq(admin)+ end+ end+enddiff --git a/spec/models/reviewable_spec.rb b/spec/models/reviewable_spec.rbindex 32c38ddb90b..5df81437203 100644--- a/spec/models/reviewable_spec.rb+++ b/spec/models/reviewable_spec.rb@@ -13,6 +13,7 @@ RSpec.describe Reviewable, type: :model doit { is_expected.to have_many(:reviewable_scores).dependent(:destroy) }it { is_expected.to have_many(:reviewable_histories).dependent(:destroy) }+ it { is_expected.to have_many(:reviewable_notes).dependent(:destroy) }it "can create a reviewable object" doexpect(reviewable).to be_presentdiff --git a/spec/requests/reviewable_notes_controller_spec.rb b/spec/requests/reviewable_notes_controller_spec.rbnew file mode 100644index 00000000000..3e9a5b1e2f1--- /dev/null+++ b/spec/requests/reviewable_notes_controller_spec.rb@@ -0,0 +1,265 @@+# frozen_string_literal: true++RSpec.describe ReviewableNotesController do+ fab!(:admin)+ fab!(:moderator)+ fab!(:user)+ fab!(:reviewable) { Fabricate(:reviewable_flagged_post) }++ describe "#create" do+ context "when user is staff" do+ before { sign_in(admin) }++ it "creates a new reviewable note successfully" do+ post "/reviewables/#{reviewable.id}/notes.json",+ params: {+ reviewable_note: {+ content: "This is a test note",+ },+ }++ expect(response.status).to eq(200)++ json = response.parsed_body+ expect(json["content"]).to eq("This is a test note")+ expect(json["user"]["id"]).to eq(admin.id)+ expect(json["user"]["username"]).to eq(admin.username)++ # Verify note was actually created in database+ note = ReviewableNote.last+ expect(note.content).to eq("This is a test note")+ expect(note.user).to eq(admin)+ expect(note.reviewable).to eq(reviewable)+ end++ it "creates a new reviewable note successfully as a moderator" do+ sign_in(moderator)++ post "/reviewables/#{reviewable.id}/notes.json",+ params: {+ reviewable_note: {+ content: "Moderator note",+ },+ }++ expect(response.status).to eq(200)++ json = response.parsed_body+ expect(json["user"]["id"]).to eq(moderator.id)+ end++ it "returns validation errors for invalid content" do+ post "/reviewables/#{reviewable.id}/notes.json",+ params: {+ reviewable_note: {+ content: "",+ },+ }++ expect(response.status).to eq(422)++ json = response.parsed_body+ expect(json["errors"]).to include("Content can't be blank")+ end++ it "returns validation errors for content that's too long" do+ long_content = "a" * (ReviewableNote::MAX_CONTENT_LENGTH + 1)++ post "/reviewables/#{reviewable.id}/notes.json",+ params: {+ reviewable_note: {+ content: long_content,+ },+ }++ expect(response.status).to eq(422)++ json = response.parsed_body+ expect(json["errors"]).to include(+ "Content is too long (maximum is #{ReviewableNote::MAX_CONTENT_LENGTH} characters)",+ )+ end++ it "trims whitespace from content" do+ post "/reviewables/#{reviewable.id}/notes.json",+ params: {+ reviewable_note: {+ content: " ",+ },+ }++ expect(response.status).to eq(422)++ json = response.parsed_body+ expect(json["errors"]).to include("Content can't be blank")+ end++ it "handles missing reviewable" do+ post "/reviewables/999999/notes.json", params: { reviewable_note: { content: "Test note" } }++ expect(response.status).to eq(404)+ end++ it "handles malformed parameters" do+ post "/reviewables/#{reviewable.id}/notes.json",+ params: {+ wrong_param: {+ content: "Test note",+ },+ }++ expect(response.status).to eq(400)+ end++ context "with HTML content" do+ it "preserves HTML content as-is" do+ html_content = "<p>This is <strong>bold</strong> text</p>"++ post "/reviewables/#{reviewable.id}/notes.json",+ params: {+ reviewable_note: {+ content: html_content,+ },+ }++ expect(response.status).to eq(200)++ json = response.parsed_body+ expect(json["content"]).to eq(html_content)+ end+ end++ context "with Unicode content" do+ it "handles Unicode characters correctly" do+ unicode_content = "Test with emojis 🎉 and accents café"++ post "/reviewables/#{reviewable.id}/notes.json",+ params: {+ reviewable_note: {+ content: unicode_content,+ },+ }++ expect(response.status).to eq(200)++ json = response.parsed_body+ expect(json["content"]).to eq(unicode_content)+ end+ end+ end++ context "when user is not staff" do+ before { sign_in(user) }++ it "returns 404" do+ post "/reviewables/#{reviewable.id}/notes.json",+ params: {+ reviewable_note: {+ content: "This should fail",+ },+ }++ expect(response.status).to eq(404)+ end+ end++ context "when user is not logged in" do+ it "returns 404" do+ post "/reviewables/#{reviewable.id}/notes.json",+ params: {+ reviewable_note: {+ content: "This should fail",+ },+ }++ expect(response.status).to eq(404)+ end+ end+ end++ describe "#destroy" do+ fab!(:note) { Fabricate(:reviewable_note, reviewable: reviewable, user: admin) }++ context "when user is the note author" do+ before { sign_in(admin) }++ it "deletes the note successfully" do+ expect { delete "/reviewables/#{reviewable.id}/notes/#{note.id}.json" }.to change {+ ReviewableNote.where(user: admin).count+ }.by(-1)++ expect(response.status).to eq(200)++ json = response.parsed_body+ expect(json["success"]).to eq("OK")+ end++ it "returns 404 for non-existent note" do+ delete "/reviewables/#{reviewable.id}/notes/999999.json"+ expect(response.status).to eq(404)+ end+ end++ context "when user is admin but not the note author" do+ fab!(:moderator_note) { Fabricate(:reviewable_note, reviewable: reviewable, user: moderator) }++ before { sign_in(admin) }++ it "allows admin to delete any note" do+ expect {+ delete "/reviewables/#{reviewable.id}/notes/#{moderator_note.id}.json"+ }.to change { ReviewableNote.count }.by(-1)++ expect(response.status).to eq(200)+ end+ end++ context "when user is moderator but not admin and not note author" do+ fab!(:admin_note) { Fabricate(:reviewable_note, reviewable: reviewable, user: admin) }++ before { sign_in(moderator) }++ it "returns 403 forbidden" do+ delete "/reviewables/#{reviewable.id}/notes/#{admin_note.id}.json"+ expect(response.status).to eq(403)+ end+ end++ context "when user is not staff" do+ before { sign_in(user) }++ it "returns 404" do+ delete "/reviewables/#{reviewable.id}/notes/#{note.id}.json"+ expect(response.status).to eq(404)+ end+ end++ context "when user is not logged in" do+ it "returns 404" do+ delete "/reviewables/#{reviewable.id}/notes/#{note.id}.json"+ expect(response.status).to eq(404)+ end+ end++ context "when reviewable doesn't exist" do+ before { sign_in(admin) }++ it "returns 404" do+ delete "/reviewables/999999/notes/#{note.id}.json"+ expect(response.status).to eq(404)+ end+ end++ context "when note belongs to different reviewable" do+ fab!(:other_reviewable) { Fabricate(:reviewable_flagged_post) }+ fab!(:other_note) { Fabricate(:reviewable_note, reviewable: other_reviewable, user: admin) }++ before { sign_in(admin) }++ it "returns 404" do+ delete "/reviewables/#{reviewable.id}/notes/#{other_note.id}.json"+ expect(response.status).to eq(404)+ end+ end+ end+enddiff --git a/spec/requests/reviewables_controller_spec.rb b/spec/requests/reviewables_controller_spec.rbindex 23bfe064413..b23ba0e64fc 100644--- a/spec/requests/reviewables_controller_spec.rb+++ b/spec/requests/reviewables_controller_spec.rb@@ -383,6 +383,51 @@ RSpec.describe ReviewablesController doexpect(json["reviewables"]).to be_presentexpect(json["reviewables"].size).to eq(1)end++ context "with reviewable notes" do+ fab!(:moderator)+ fab!(:reviewable)++ it "doesn't cause N+1 queries when notes are added" do+ # post sign-in warmup+ get "/review.json"++ post "/reviewables/#{reviewable.id}/notes.json",+ params: {+ reviewable_note: {+ content: "This is a test note",+ },+ }++ initial_sql_queries =+ track_sql_queries do+ get "/review.json"+ expect(response.status).to eq(200)+ end++ sign_in(moderator)++ post "/reviewables/#{reviewable.id}/notes.json",+ params: {+ reviewable_note: {+ content: "This is another test note",+ },+ }++ sign_in(admin)++ # second post sign-in warmup+ get "/review.json"++ new_sql_queries =+ track_sql_queries do+ get "/review.json"+ expect(response.status).to eq(200)+ end++ expect(new_sql_queries.count).to eq(initial_sql_queries.count)+ end+ endenddescribe "#user_menu_list" dodiff --git a/spec/serializers/reviewable_note_serializer_spec.rb b/spec/serializers/reviewable_note_serializer_spec.rbnew file mode 100644index 00000000000..fc416f1e854--- /dev/null+++ b/spec/serializers/reviewable_note_serializer_spec.rb@@ -0,0 +1,27 @@+# frozen_string_literal: true+RSpec.describe ReviewableNoteSerializer do+ fab!(:admin)+ fab!(:moderator)+ fab!(:user)+ fab!(:reviewable) { Fabricate(:reviewable_flagged_post) }+ fab!(:note) do+ Fabricate(:reviewable_note, reviewable: reviewable, user: admin, content: "Test note content")+ end+ def serialized_note(note, current_user = admin)+ ReviewableNoteSerializer.new(note, scope: Guardian.new(current_user), root: false).as_json+ end+ describe "serialization" do+ let(:json) { serialized_note(note) }+ it "includes basic attributes" do+ expect(json[:id]).to eq(note.id)+ expect(json[:content]).to eq("Test note content")+ expect(json[:created_at]).to be_present+ expect(json[:updated_at]).to be_present+ end+ it "includes user information" do+ expect(json[:user]).to be_present+ expect(json[:user][:id]).to eq(admin.id)+ expect(json[:user][:username]).to eq(admin.username)+ end+ end+end