diaspora/app/models/like.rb
Benjamin Neff df8275f000
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.
2023-11-13 02:27:55 +01:00

48 lines
1.2 KiB
Ruby

# frozen_string_literal: true
# Copyright (c) 2010-2011, Diaspora Inc. This file is
# licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file.
class Like < ApplicationRecord
include Diaspora::Federated::Base
include Diaspora::Fields::Guid
include Diaspora::Fields::Author
include Diaspora::Fields::Target
include Diaspora::Relayable
has_one :signature, class_name: "LikeSignature", dependent: :delete
alias_attribute :parent, :target
class Generator < Diaspora::Federated::Generator
def self.federated_class
Like
end
def relayable_options
{:target => @target, :positive => true}
end
end
after_commit :on => :create do
self.parent.update_likes_counter
end
after_destroy do
self.parent.update_likes_counter
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
# NOTE API V1 to be extracted
acts_as_api
api_accessible :backbone do |t|
t.add :id
t.add :guid
t.add :author
t.add :created_at
end
end