Skip to content

Commit

Permalink
Allow fetching comments
Browse files Browse the repository at this point in the history
Now with likes on comments, diaspora also tries to fetch comments if it
receives a like for a comment it doesn't know yet. So this now also
allows to fetch comments with `/fetch/comment/<guid>`.
  • Loading branch information
SuperTux88 committed Jan 21, 2024
1 parent 389b187 commit 9e61693
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
6 changes: 6 additions & 0 deletions app/models/comment.rb
Expand Up @@ -37,6 +37,12 @@ class Comment < ApplicationRecord
scope :including_author, -> { includes(:author => :profile) }
scope :for_a_stream, -> { including_author.merge(order('created_at ASC')) }

scope :all_public, -> {
where("commentable_type = 'Post' AND EXISTS(
SELECT 1 FROM posts WHERE posts.id = commentable_id AND posts.public = true
)")
}

before_save do
self.text.strip! unless self.text.nil?
end
Expand Down
4 changes: 2 additions & 2 deletions config/initializers/diaspora_federation.rb
Expand Up @@ -120,10 +120,10 @@
on :fetch_public_entity do |entity_type, guid|
entity = Diaspora::Federation::Mappings.model_class_for(entity_type).all_public.find_by(guid: guid)
case entity
when Post
Diaspora::Federation::Entities.post(entity)
when Poll
Diaspora::Federation::Entities.status_message(entity.status_message)
else
Diaspora::Federation::Entities.build(entity) if entity
end
end

Expand Down
27 changes: 27 additions & 0 deletions spec/federation_callbacks_spec.rb
Expand Up @@ -465,6 +465,33 @@
DiasporaFederation.callbacks.trigger(:fetch_public_entity, "Post", "unknown-guid")
).to be_nil
end

context "comment" do
it "fetches a Comment" do
post = FactoryBot.create(:status_message, author: alice.person, public: true)
comment = FactoryBot.create(:comment, author: alice.person, commentable: post)
entity = DiasporaFederation.callbacks.trigger(:fetch_public_entity, "Comment", comment.guid)

expect(entity.guid).to eq(comment.guid)
expect(entity.author).to eq(alice.diaspora_handle)
expect(entity.parent.public).to be_truthy
end

it "does not fetch a Comment from a private post" do
post = FactoryBot.create(:status_message, author: alice.person, public: false)
comment = FactoryBot.create(:comment, author: alice.person, commentable: post)

expect(
DiasporaFederation.callbacks.trigger(:fetch_public_entity, "Comment", comment.guid)
).to be_nil
end

it "returns nil, if the comment is unknown" do
expect(
DiasporaFederation.callbacks.trigger(:fetch_public_entity, "Comment", "unknown-guid")
).to be_nil
end
end
end

describe ":fetch_person_url_to" do
Expand Down

0 comments on commit 9e61693

Please sign in to comment.