Skip to content

Commit

Permalink
Merge pull request #2885 from manyfold3d/post-comments-with-activitypub
Browse files Browse the repository at this point in the history
Post ActivityPub Notes for new, updated, and deleted comments
  • Loading branch information
Floppy authored Oct 8, 2024
2 parents 7ea7960 + 0ca1c22 commit 2e38c8b
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 7 deletions.
3 changes: 0 additions & 3 deletions app/helpers/comments_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
module CommentsHelper
def url_for_comment(comment)
url_for([comment.commentable, comment, {only_path: false}])
end
end
30 changes: 30 additions & 0 deletions app/models/comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,34 @@ class Comment < ApplicationRecord

belongs_to :commenter, polymorphic: true
belongs_to :commentable, polymorphic: true

after_create :post_create_activity
after_update :post_update_activity
after_destroy :post_destroy_activity

def federated_url
Rails.application.routes.url_helpers.url_for([commentable, self, {only_path: false}])
end

private

def post_create_activity
post_activity "Create"
end

def post_update_activity
post_activity "Update"
end

def post_destroy_activity
post_activity "Delete"
end

def post_activity(action)
Federails::Activity.create!(
actor: commenter.actor,
action: action,
entity: self
)
end
end
2 changes: 1 addition & 1 deletion app/views/comments/show.activitypub.jbuilder
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Comments become Notes in ActvityPub world
json.set! "@context", "https://www.w3.org/ns/activitystreams"
json.id url_for_comment(@comment)
json.id @comment.federated_url
json.type "Note"
json.content markdownify(@comment.comment)
json.context url_for([@comment.commentable, only_path: false])
2 changes: 1 addition & 1 deletion spec/factories/comment.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
FactoryBot.define do
factory :comment do
text { Faker::Lorem.paragraph }
comment { Faker::Lorem.paragraph }
end
end
22 changes: 21 additions & 1 deletion spec/models/comment_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
require "rails_helper"

RSpec.describe Comment do
it "needs tests"
let!(:model) { create(:model) }
let!(:comment) { create(:comment, commenter: model, commentable: model) }

it "posts a Federails Activity on creation" do # rubocop:disable RSpec/MultipleExpectations
expect { create(:comment, commenter: model, commentable: model) }.to change(Federails::Activity, :count).by(1)
expect(Federails::Activity.last.action).to eq "Create"
end

it "posts a Federails Activity on update" do # rubocop:disable RSpec/MultipleExpectations
expect { comment.update(comment: "test") }.to change(Federails::Activity, :count).by(1)
expect(Federails::Activity.last.action).to eq "Update"
end

it "posts a Federails Activity on deletion" do # rubocop:disable RSpec/MultipleExpectations
expect { comment.destroy }.to change(Federails::Activity, :count).by(1)
expect(Federails::Activity.last.action).to eq "Delete"
end

it "has a federated_url method" do
expect(comment.federated_url).to eq "http://localhost:3214/models/#{model.public_id}/comments/#{comment.public_id}"
end
end
2 changes: 1 addition & 1 deletion spec/requests/comments_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
end

it "includes id" do
expect(response.parsed_body["id"]).to eq request.url
expect(response.parsed_body["id"]).to eq comment.federated_url
end

it "includes JSON-LD context" do
Expand Down

0 comments on commit 2e38c8b

Please sign in to comment.