diff --git a/docs/api/api/api.txt b/docs/api/api/api.txt index 94b4267ee6a..cfd3bb2a926 100644 --- a/docs/api/api/api.txt +++ b/docs/api/api/api.txt @@ -1014,9 +1014,9 @@ GET /comments/package/:project/:pname XmlResult: directory -POST /comments/request/:id -POST /comments/project/:name -POST /comments/package/:project/:pname +POST /comments/request/:id?parent_id= +POST /comments/project/:name?parent_id= +POST /comments/package/:project/:pname?parent_id= Create a comment for the object diff --git a/src/api/app/controllers/comments_controller.rb b/src/api/app/controllers/comments_controller.rb index b9af9ad897f..8a58d6d37de 100644 --- a/src/api/app/controllers/comments_controller.rb +++ b/src/api/app/controllers/comments_controller.rb @@ -7,7 +7,7 @@ def show_comments end def create - @obj.comments.create!(body: request.raw_post, user: User.current) + @obj.comments.create!(body: request.raw_post, user: User.current, parent_id: params[:parent_id]) render_ok end diff --git a/src/api/app/models/comment.rb b/src/api/app/models/comment.rb index b0f8f7c0600..bab09ee00be 100644 --- a/src/api/app/models/comment.rb +++ b/src/api/app/models/comment.rb @@ -7,6 +7,8 @@ class Comment < ApplicationRecord validates :body, :commentable, :user, presence: true + validate :validate_parent_id + after_create :create_notification after_destroy :delete_parent_if_unused @@ -88,4 +90,10 @@ def destroy def delete_parent_if_unused parent.destroy if parent && parent.user == User.find_nobody! && parent.children.length.zero? end + + def validate_parent_id + return unless parent_id + return if commentable.comments.where(id: parent_id).present? + errors.add(:parent, "belongs to different object") + end end diff --git a/src/api/spec/models/comment_spec.rb b/src/api/spec/models/comment_spec.rb index 5d3aa26f681..6c989850610 100644 --- a/src/api/spec/models/comment_spec.rb +++ b/src/api/spec/models/comment_spec.rb @@ -2,9 +2,9 @@ RSpec.describe Comment do let(:comment_package) { create(:comment_package) } - let(:comment_package_with_parent) { create(:comment_package, parent: comment_package) } - let(:comment_package_with_parent_2) { create(:comment_package, parent: comment_package) } - let(:comment_package_with_grandparent) { create(:comment_package, parent: comment_package_with_parent) } + let(:comment_package_with_parent) { create(:comment_package, parent: comment_package, commentable: comment_package.commentable) } + let(:comment_package_with_parent_2) { create(:comment_package, parent: comment_package, commentable: comment_package.commentable) } + let(:comment_package_with_grandparent) { create(:comment_package, parent: comment_package_with_parent, commentable: comment_package.commentable) } describe "has a valid Factory" do it { expect(comment_package).to be_valid } @@ -21,6 +21,10 @@ it { is_expected.to validate_presence_of(:body) } it { is_expected.to validate_presence_of(:commentable) } it { is_expected.to validate_presence_of(:user) } + it { + expect { create(:comment_package, parent: comment_package) }.to raise_error( + ActiveRecord::RecordInvalid, "Validation failed: Parent belongs to different object") + } end describe "to_xml" do