Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow fetching comments #8441

Merged
merged 1 commit into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions app/models/comment.rb
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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