From 0925a26506ae58c49af72258316ec45d0824915c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20V=C3=B6gele?= Date: Wed, 18 Nov 2015 01:45:32 +0100 Subject: [PATCH] Do not add participation for comment if comment validation failed The same fix is also done for the other social actions closes #6552 --- Changelog.md | 1 + app/models/user/social_actions.rb | 25 ++++++++++++++----------- spec/models/comment_spec.rb | 9 +++++++++ 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/Changelog.md b/Changelog.md index f0d6bc740..9be9daa03 100644 --- a/Changelog.md +++ b/Changelog.md @@ -7,6 +7,7 @@ * Correctly skip setting sidekiq logfile on Heroku [#6500](https://github.com/diaspora/diaspora/pull/6500) * Fix notifications for interactions by non-contacts [#6498](https://github.com/diaspora/diaspora/pull/6498) * Fix issue where the publisher was broken on profile pages [#6503](https://github.com/diaspora/diaspora/pull/6503) +* Prevent participations being created for invalid interactions [#6552](https://github.com/diaspora/diaspora/pull/6552) ## Features diff --git a/app/models/user/social_actions.rb b/app/models/user/social_actions.rb index ccc30b484..65ddafdc3 100644 --- a/app/models/user/social_actions.rb +++ b/app/models/user/social_actions.rb @@ -1,7 +1,8 @@ module User::SocialActions def comment!(target, text, opts={}) - find_or_create_participation!(target) - Comment::Generator.new(self, target, text).create!(opts) + Comment::Generator.new(self, target, text).create!(opts).tap do + find_or_create_participation!(target) + end end def participate!(target, opts={}) @@ -9,21 +10,23 @@ module User::SocialActions end def like!(target, opts={}) - find_or_create_participation!(target) - Like::Generator.new(self, target).create!(opts) + Like::Generator.new(self, target).create!(opts).tap do + find_or_create_participation!(target) + end end def participate_in_poll!(target, answer, opts={}) - find_or_create_participation!(target) - PollParticipation::Generator.new(self, target, answer).create!(opts) + PollParticipation::Generator.new(self, target, answer).create!(opts).tap do + find_or_create_participation!(target) + end end def reshare!(target, opts={}) - find_or_create_participation!(target) - reshare = build_post(:reshare, :root_guid => target.guid) - reshare.save! - Postzord::Dispatcher.defer_build_and_post(self, reshare) - reshare + build_post(:reshare, :root_guid => target.guid).tap do |reshare| + reshare.save! + find_or_create_participation!(target) + Postzord::Dispatcher.defer_build_and_post(self, reshare) + end end def build_comment(options={}) diff --git a/spec/models/comment_spec.rb b/spec/models/comment_spec.rb index b6a546706..1ba80fba8 100644 --- a/spec/models/comment_spec.rb +++ b/spec/models/comment_spec.rb @@ -57,6 +57,15 @@ describe Comment, :type => :model do comment_alice }.to change { Comment.count }.by(1) end + + it "does not create a participation if comment validation failed" do + begin + alice.comment!(status_bob, " ") + rescue ActiveRecord::RecordInvalid + end + participations = Participation.where(target_id: status_bob, author_id: alice.person.id) + expect(participations.count).to eq(0) + end end describe "counter cache" do