Skip to content

Commit

Permalink
Merge pull request #8441 from SuperTux88/fix-fetch-comments
Browse files Browse the repository at this point in the history
Allow fetching comments
  • Loading branch information
SuperTux88 committed Jan 24, 2024
2 parents 7cc48be + 9e61693 commit ae3b780
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
1 change: 1 addition & 0 deletions Changelog.md
Expand Up @@ -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

Expand Down
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 ae3b780

Please sign in to comment.