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

Post ActivityPub message when a creator adds a new model #2900

Merged
merged 6 commits into from
Oct 9, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions app/jobs/activity/creator_added_model_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Activity::CreatorAddedModelJob < ApplicationJob
queue_as :activity

def perform(model_id)
model = Model.find(model_id)
Comment.create!(
system: true,
commentable: model,
commenter: model.creator,
comment: I18n.t("jobs.activity.creator_added_model.comment", # rubocop:disable I18n/RailsI18n/DecorateStringFormattingUsingInterpolation
model_name: model.name,
url: model.actor.profile_url,
tags: model.tag_list.map { |t| "##{t}" }.join(" "))
)
end
end
26 changes: 0 additions & 26 deletions app/models/concerns/followable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ module Followable

included do
delegate :following_followers, to: :actor
after_commit :post_creation_activity, on: :create
after_commit :post_update_activity, on: :update

after_followed :auto_accept
end
Expand All @@ -22,30 +20,6 @@ def followed_by?(follower)

private

def post_creation_activity
user = permitted_users.with_permission("own").first || SiteSettings.default_user
return if user.nil?
user.create_actor_if_missing
Federails::Activity.create!(
actor: user.actor,
action: "Create",
entity: actor,
created_at: created_at
)
end

def post_update_activity
return if actor&.activities_as_entity&.where(created_at: TIMEOUT.minutes.ago..)&.count&.> 0
user = permitted_users.with_permission("own").first || SiteSettings.default_user
return if user.nil?
Federails::Activity.create!(
actor: user.actor,
action: "Update",
entity: actor,
created_at: updated_at
)
end

def auto_accept
actor.following_followers.where(status: "pending").find_each { |x| x.accept! }
end
Expand Down
14 changes: 14 additions & 0 deletions app/models/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ class Model < ApplicationRecord
# In Rails 7.1 we will be able to do this instead:
# normalizes :license, with: -> license { license.blank? ? nil : license }

after_create :post_creation_activity
before_update :move_files, if: :need_to_move_files?
after_update :post_update_activity
after_commit :check_integrity, on: :update

validates :name, presence: true
Expand Down Expand Up @@ -196,4 +198,16 @@ def move_files
def check_integrity
Scan::CheckModelIntegrityJob.set(wait: 5.seconds).perform_later(id)
end

def post_creation_activity
if creator.present?
Activity::CreatorAddedModelJob.set(wait: 5.seconds).perform_later(id)
end
end

def post_update_activity
if creator_previously_changed?
Activity::CreatorAddedModelJob.set(wait: 5.seconds).perform_later(id)
end
end
end
8 changes: 8 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,14 @@ en:
placeholder: What are you looking for?
update_actor: updated by %{name} %{time} ago
jobs:
activity:
creator_added_model:
comment: |
I've just added a new model: "%{model_name}".

Find it at %{url}

%{tags}
analysis:
analyse_model_file:
detect_duplicates: Detecting duplicate files
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20241009122540_add_system_to_comments.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddSystemToComments < ActiveRecord::Migration[7.1]
def change
add_column :comments, :system, :boolean, null: false, default: false
end
end
3 changes: 2 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 48 additions & 0 deletions spec/jobs/activity/creator_added_model_job_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
require "rails_helper"

RSpec.describe Activity::CreatorAddedModelJob do
let(:creator) { create(:creator) }
let(:model) { create(:model, creator: creator, tag_list: "tag1, tag2") }

it "adds a comment" do
expect { described_class.new.perform(model.id) }.to change(Comment, :count).by(1)
end

it "ends up queueing an ActivityPub publish job" do
expect { described_class.new.perform(model.id) }.to have_enqueued_job Federails::NotifyInboxJob
end

context "with a comment" do
subject(:comment) { model.comments.first }

before do
described_class.new.perform(model.id)
end

it "sets creator as author" do
expect(comment.commenter).to eq creator
end

it "sets model as the subject" do
expect(comment.commentable).to eq model
end

it "marks comment as a system comment" do
expect(comment.system).to be true
end

it "includes model name in text" do
expect(comment.comment).to include model.name
end

it "includes tags in text as hashtags" do
model.tag_list.each do |tag|
expect(comment.comment).to include "##{tag}"
end
end

it "includes URL in text" do
expect(comment.comment).to include "http://localhost:3214/models/#{model.public_id}"
end
end
end
27 changes: 0 additions & 27 deletions spec/models/concerns/followable_shared.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,4 @@
expect(target.followers.count).to eq 1
end
end

context "when being created" do
before do
create(:admin)
end

it "posts an activity" do
expect { create(described_class.to_s.underscore.to_sym) }.to change(Federails::Activity, :count).by(1)
end
end

context "when being updated" do
let!(:entity) { create(described_class.to_s.underscore.to_sym) }

before do
create(:admin)
end

it "posts an activity after update" do
expect { entity.update caption: "test" }.to change(Federails::Activity, :count).by(1)
end

it "doesn't posts an activity after update if there's already been one recently" do
entity.update caption: "change"
expect { entity.update caption: "test" }.not_to change(Federails::Activity, :count)
end
end
end
14 changes: 14 additions & 0 deletions spec/models/model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -385,4 +385,18 @@
expect(file).to have_received(:delete_from_disk_and_destroy).once
end
end

context "when making changes" do
it "queues creator-specific model creation job when model is created if a creator is set" do
model = create(:model, creator: create(:creator))
expect(Activity::CreatorAddedModelJob).to have_been_enqueued.with(model.id)
end

it "queues creator-specific model creation job when model is updated if a creator has changed" do
model = create(:model)
ActiveJob::Base.queue_adapter.enqueued_jobs.clear
model.update(creator: create(:creator))
expect(Activity::CreatorAddedModelJob).to have_been_enqueued.with(model.id)
end
end
end
Loading