diff --git a/Changelog.md b/Changelog.md index 97798c15bb2..114d08405d0 100644 --- a/Changelog.md +++ b/Changelog.md @@ -83,6 +83,7 @@ We recommend setting up new pods using Ruby 3.1, and updating existing pods to t * Add a more detailed modal when reporting a post or a comment [#8035](https://github.com/diaspora/diaspora/pull/8035) * Re-introduce likes on comments [#8203](https://github.com/diaspora/diaspora/pull/8203) * New redesigned registration page [#8285](https://github.com/diaspora/diaspora/pull/8285) +* Allow comments to be fetched [#8441](https://github.com/diaspora/diaspora/pull/8441) # 0.7.18.2 diff --git a/app/models/comment.rb b/app/models/comment.rb index a766566fca1..b98a5752a89 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -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 diff --git a/config/initializers/diaspora_federation.rb b/config/initializers/diaspora_federation.rb index 19a9fdfe499..941eae17cb6 100644 --- a/config/initializers/diaspora_federation.rb +++ b/config/initializers/diaspora_federation.rb @@ -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 diff --git a/spec/federation_callbacks_spec.rb b/spec/federation_callbacks_spec.rb index 6200af86659..044d492e838 100644 --- a/spec/federation_callbacks_spec.rb +++ b/spec/federation_callbacks_spec.rb @@ -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