Skip to content

Commit

Permalink
refactor report creation into a service object
Browse files Browse the repository at this point in the history
  • Loading branch information
Floppy committed Nov 27, 2024
1 parent 969c3ab commit 5409030
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 5 deletions.
3 changes: 0 additions & 3 deletions app/models/federails/moderation/report.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
class Federails::Moderation::Report
def self.handle_flag(activity)
Rails.logger.debug activity.inspect
end
end
7 changes: 7 additions & 0 deletions app/services/federails/moderation/application_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Federails::Moderation
class ApplicationService
def self.call(*args, &block)
new(*args, &block).call
end
end
end
11 changes: 11 additions & 0 deletions app/services/federails/moderation/report_creation_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Federails::Moderation
class ReportCreationService < ApplicationService
def initialize(activity)
@activity = activity
end

def call
Rails.logger.debug @activity.inspect
end
end
end
2 changes: 1 addition & 1 deletion config/initializers/handlers.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require "fediverse/inbox"

Rails.application.config.after_initialize do
Fediverse::Inbox.register_handler "Flag", "*", Federails::Moderation::Report, :handle_flag
Fediverse::Inbox.register_handler "Flag", "*", Federails::Moderation::ReportCreationService, :call
end
4 changes: 3 additions & 1 deletion spec/requests/reports_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
end

it "should call registered Report handler when a flag is received by Federails" do
expect(Federails::Moderation::Report).to receive(:handle_flag).once.and_call_original
expect(Federails::Moderation::ReportCreationService).to receive(:call).once { |args|
expect(args["content"]).to eq "Test, please ignore"
}
post user.actor.inbox_url, params: report_body, as: :json
end
end

0 comments on commit 5409030

Please sign in to comment.