From da70714a2fcd6b4f6775e68ef13e09375b26a404 Mon Sep 17 00:00:00 2001 From: James Smith Date: Tue, 8 Oct 2024 12:04:00 +0100 Subject: [PATCH 1/3] move url generation into comment model --- app/helpers/comments_helper.rb | 3 --- app/models/comment.rb | 4 ++++ app/views/comments/show.activitypub.jbuilder | 2 +- spec/models/comment_spec.rb | 7 ++++++- spec/requests/comments_spec.rb | 2 +- 5 files changed, 12 insertions(+), 6 deletions(-) diff --git a/app/helpers/comments_helper.rb b/app/helpers/comments_helper.rb index c002d2671..0ec9ca5f2 100644 --- a/app/helpers/comments_helper.rb +++ b/app/helpers/comments_helper.rb @@ -1,5 +1,2 @@ module CommentsHelper - def url_for_comment(comment) - url_for([comment.commentable, comment, {only_path: false}]) - end end diff --git a/app/models/comment.rb b/app/models/comment.rb index a5bd8413c..ca8285807 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -3,4 +3,8 @@ class Comment < ApplicationRecord belongs_to :commenter, polymorphic: true belongs_to :commentable, polymorphic: true + def federated_url + Rails.application.routes.url_helpers.url_for([commentable, self, {only_path: false}]) + end + end diff --git a/app/views/comments/show.activitypub.jbuilder b/app/views/comments/show.activitypub.jbuilder index 778b2eecc..d61ce35b7 100644 --- a/app/views/comments/show.activitypub.jbuilder +++ b/app/views/comments/show.activitypub.jbuilder @@ -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]) diff --git a/spec/models/comment_spec.rb b/spec/models/comment_spec.rb index a377aed7d..cecde43ba 100644 --- a/spec/models/comment_spec.rb +++ b/spec/models/comment_spec.rb @@ -1,5 +1,10 @@ require "rails_helper" RSpec.describe Comment do - it "needs tests" + let!(:model) { create(:model) } + let!(:comment) { create(:comment, commenter: model, commentable: model) } + + 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 diff --git a/spec/requests/comments_spec.rb b/spec/requests/comments_spec.rb index 6458b86e4..c6c392b27 100644 --- a/spec/requests/comments_spec.rb +++ b/spec/requests/comments_spec.rb @@ -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 From 1fca48ab8fa8ee0653e679d75547b4a0544a99a5 Mon Sep 17 00:00:00 2001 From: James Smith Date: Tue, 8 Oct 2024 12:04:09 +0100 Subject: [PATCH 2/3] fix comment factory --- spec/factories/comment.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/factories/comment.rb b/spec/factories/comment.rb index 526c24002..0c93acbc7 100644 --- a/spec/factories/comment.rb +++ b/spec/factories/comment.rb @@ -1,5 +1,5 @@ FactoryBot.define do factory :comment do - text { Faker::Lorem.paragraph } + comment { Faker::Lorem.paragraph } end end From 0ca1c22beb74b431f4533f9727f436e0e31eb4f5 Mon Sep 17 00:00:00 2001 From: James Smith Date: Tue, 8 Oct 2024 12:06:04 +0100 Subject: [PATCH 3/3] post lifecycle activities --- app/models/comment.rb | 26 ++++++++++++++++++++++++++ spec/models/comment_spec.rb | 15 +++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/app/models/comment.rb b/app/models/comment.rb index ca8285807..61da951af 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -3,8 +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 diff --git a/spec/models/comment_spec.rb b/spec/models/comment_spec.rb index cecde43ba..beea74bd0 100644 --- a/spec/models/comment_spec.rb +++ b/spec/models/comment_spec.rb @@ -4,6 +4,21 @@ 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