Fix participations for likes on comments in the backend

When liking a comment, the post also gets a participation, and if all
likes/comments get removed again, the participation also gets removed
again.

The only thing still not working properly is the frontend, but that is
already broken when unliking a post. So it shows an invalids state in
the frontend when unliking the post/comment.
This commit is contained in:
Benjamin Neff 2023-11-11 00:30:18 +01:00
parent edfb603965
commit df8275f000
No known key found for this signature in database
GPG key ID: 971464C3F1A90194
4 changed files with 27 additions and 9 deletions

View file

@ -44,7 +44,8 @@ app.models.LikeInteractions = Backbone.Model.extend({
var self = this;
this.userLike().destroy({
success: function() {
// TODO: If user unlikes a post and the last like of all comments, then set participation to false
// TODO: unlike always sets participation to false in the UI, even if there are more participations left
// in the backend (from manually participating, other likes or comments)
self.post.set({participation: false});
self.trigger("change");
self.set({"likes_count": self.get("likes_count") - 1});

View file

@ -32,7 +32,8 @@ class Like < ApplicationRecord
after_destroy do
self.parent.update_likes_counter
participation = author.participations.find_by(target_id: target.id)
participation_target_id = parent.is_a?(Comment) ? parent.commentable.id : parent.id
participation = author.participations.find_by(target_id: participation_target_id)
participation.unparticipate! if participation.present?
end

View file

@ -18,7 +18,9 @@ module User::SocialActions
end
def like_comment!(target, opts={})
Like::Generator.new(self, target).create!(opts)
Like::Generator.new(self, target).create!(opts).tap do
update_or_create_participation!(target.commentable)
end
end
def participate_in_poll!(target, answer, opts={})

View file

@ -12,20 +12,34 @@ describe Like, type: :model do
end
describe "#destroy" do
before do
@like = alice.like!(status)
end
let!(:like) { alice.like!(status) }
it "should delete a participation" do
expect { @like.destroy }.to change { Participation.count }.by(-1)
expect { like.destroy }.to change { Participation.count }.by(-1)
end
it "should decrease count participation" do
alice.comment!(status, "Are you there?")
@like.destroy
participations = Participation.where(target_id: @like.target_id, author_id: @like.author_id)
like.destroy
participations = Participation.where(target_id: status.id, author_id: like.author_id)
expect(participations.first.count).to eq(1)
end
context "on comment" do
let(:comment) { bob.comment!(status, "Are you there?") }
let!(:like) { alice.like_comment!(comment) }
it "should delete a participation" do
expect { like.destroy }.to change { Participation.count }.by(-1)
end
it "should decrease count participation" do
alice.comment!(status, "Yes, I am here!")
like.destroy
participations = Participation.where(target_id: status.id, author_id: like.author_id)
expect(participations.first.count).to eq(1)
end
end
end
describe "counter cache" do