discourse/lib/tasks
Martin Brennan 60ad836313
DEV: Chat service object initial implementation (#19814)
This is a combined work of Martin Brennan, Loïc Guitaut, and Joffrey Jaffeux.

---

This commit implements a base service object when working in chat. The documentation is available at https://discourse.github.io/discourse/chat/backend/Chat/Service.html

Generating documentation has been made as part of this commit with a bigger goal in mind of generally making it easier to dive into the chat project.

Working with services generally involves 3 parts:

- The service object itself, which is a series of steps where few of them are specialized (model, transaction, policy)

```ruby
class UpdateAge
  include Chat::Service::Base

  model :user, :fetch_user
  policy :can_see_user
  contract
  step :update_age

  class Contract
    attribute :age, :integer
  end

  def fetch_user(user_id:, **)
    User.find_by(id: user_id)
  end

  def can_see_user(guardian:, **)
    guardian.can_see_user(user)
  end

  def update_age(age:, **)
    user.update!(age: age)
  end
end
```

- The `with_service` controller helper, handling success and failure of the service within a service and making easy to return proper response to it from the controller

```ruby
def update
  with_service(UpdateAge) do
    on_success { render_serialized(result.user, BasicUserSerializer, root: "user") }
  end
end
```

- Rspec matchers and steps inspector, improving the dev experience while creating specs for a service

```ruby
RSpec.describe(UpdateAge) do
  subject(:result) do
    described_class.call(guardian: guardian, user_id: user.id, age: age)
  end

  fab!(:user) { Fabricate(:user) }
  fab!(:current_user) { Fabricate(:admin) }

  let(:guardian) { Guardian.new(current_user) }
  let(:age) { 1 }

   it { expect(user.reload.age).to eq(age) }
end
```

Note in case of unexpected failure in your spec, the output will give all the relevant information:

```
  1) UpdateAge when no channel_id is given is expected to fail to find a model named 'user'
     Failure/Error: it { is_expected.to fail_to_find_a_model(:user) }

       Expected model 'foo' (key: 'result.model.user') was not found in the result object.

       [1/4] [model] 'user' 
       [2/4] [policy] 'can_see_user'
       [3/4] [contract] 'default'
       [4/4] [step] 'update_age'

       /Users/joffreyjaffeux/Code/pr-discourse/plugins/chat/app/services/update_age.rb:32:in `fetch_user': missing keyword: :user_id (ArgumentError)
       	from /Users/joffreyjaffeux/Code/pr-discourse/plugins/chat/app/services/base.rb:202:in `instance_exec'
       	from /Users/joffreyjaffeux/Code/pr-discourse/plugins/chat/app/services/base.rb:202:in `call'
       	from /Users/joffreyjaffeux/Code/pr-discourse/plugins/chat/app/services/base.rb:219:in `call'
       	from /Users/joffreyjaffeux/Code/pr-discourse/plugins/chat/app/services/base.rb:417:in `block in run!'
       	from /Users/joffreyjaffeux/Code/pr-discourse/plugins/chat/app/services/base.rb:417:in `each'
       	from /Users/joffreyjaffeux/Code/pr-discourse/plugins/chat/app/services/base.rb:417:in `run!'
       	from /Users/joffreyjaffeux/Code/pr-discourse/plugins/chat/app/services/base.rb:411:in `run'
       	from <internal:kernel>:90:in `tap'
       	from /Users/joffreyjaffeux/Code/pr-discourse/plugins/chat/app/services/base.rb:302:in `call'
       	from /Users/joffreyjaffeux/Code/pr-discourse/plugins/chat/spec/services/update_age_spec.rb:15:in `block (3 levels) in <main>'
```
2023-02-13 13:09:57 +01:00
..
add_topic_to_quotes.rake DEV: Apply syntax_tree formatting to `lib/*` 2023-01-09 12:10:19 +00:00
admin.rake DEV: Apply syntax_tree formatting to `lib/*` 2023-01-09 12:10:19 +00:00
annotate.rake DEV: Apply syntax_tree formatting to `lib/*` 2023-01-09 12:10:19 +00:00
api.rake FEATURE: Hash API keys in the database (#8438) 2019-12-12 11:45:00 +00:00
assets.rake DEV: Prefer \A and \z over ^ and $ in regexes (#19936) 2023-01-20 12:52:49 -06:00
auto_annotate_models.rake DEV: Apply syntax_tree formatting to `lib/*` 2023-01-09 12:10:19 +00:00
autospec.rake DEV: Apply syntax_tree formatting to `lib/*` 2023-01-09 12:10:19 +00:00
avatars.rake DEV: Apply syntax_tree formatting to `lib/*` 2023-01-09 12:10:19 +00:00
backfill.thor DEV: enable frozen string literal on all files 2019-05-13 09:31:32 +08:00
categories.rake DEV: Apply syntax_tree formatting to `lib/*` 2023-01-09 12:10:19 +00:00
cdn.rake DEV: Prefer \A and \z over ^ and $ in regexes (#19936) 2023-01-20 12:52:49 -06:00
db.rake DEV: Prefer \A and \z over ^ and $ in regexes (#19936) 2023-01-20 12:52:49 -06:00
destroy.rake DEV: Apply syntax_tree formatting to `lib/*` 2023-01-09 12:10:19 +00:00
dev.rake DEV: Apply syntax_tree formatting to `lib/*` 2023-01-09 12:10:19 +00:00
docker.rake DEV: Apply syntax_tree formatting to `lib/*` 2023-01-09 12:10:19 +00:00
documentation.rake DEV: Chat service object initial implementation (#19814) 2023-02-13 13:09:57 +01:00
emails.rake DEV: Apply syntax_tree formatting to `lib/*` 2023-01-09 12:10:19 +00:00
emoji.rake DEV: Prefer \A and \z over ^ and $ in regexes (#19936) 2023-01-20 12:52:49 -06:00
export.rake DEV: Apply syntax_tree formatting to `lib/*` 2023-01-09 12:10:19 +00:00
groups.rake DEV: Apply syntax_tree formatting to `lib/*` 2023-01-09 12:10:19 +00:00
hashtags.rake FEATURE: Add rake task to mark old hashtag format for rebake (#19876) 2023-01-18 10:16:05 +10:00
i18n.rake DEV: Apply syntax_tree formatting to `lib/*` 2023-01-09 12:10:19 +00:00
images.rake DEV: Upgrade to Rails 7 2022-04-28 11:51:03 +02:00
import.rake DEV: Apply syntax_tree formatting to `lib/*` 2023-01-09 12:10:19 +00:00
incoming_emails.rake DEV: Apply syntax_tree formatting to `lib/*` 2023-01-09 12:10:19 +00:00
integration.rake DEV: Apply syntax_tree formatting to `lib/*` 2023-01-09 12:10:19 +00:00
javascript.rake DEV: Add `yaml` support to `<AceEditor />` (#20198) 2023-02-07 12:49:12 -08:00
log.rake FIX: whoops, linting 2020-09-03 12:10:11 +01:00
maxminddb.rake DEV: Apply syntax_tree formatting to `lib/*` 2023-01-09 12:10:19 +00:00
plugin.rake DEV: Prefer \A and \z over ^ and $ in regexes (#19936) 2023-01-20 12:52:49 -06:00
populate.rake DEV: Apply syntax_tree formatting to `lib/*` 2023-01-09 12:10:19 +00:00
populate.thor DEV: Correctly tag heredocs (#16061) 2022-02-28 20:50:55 +01:00
posts.rake DEV: Replace #pluck_first freedom patch with AR #pick in core (#19893) 2023-02-13 12:39:45 +08:00
profile.rake DEV: Apply syntax_tree formatting to `lib/*` 2023-01-09 12:10:19 +00:00
qunit.rake DEV: Apply syntax_tree formatting to `lib/*` 2023-01-09 12:10:19 +00:00
redis.rake DEV: Apply syntax_tree formatting to `lib/*` 2023-01-09 12:10:19 +00:00
release_note.rake DEV: Prefer \A and \z over ^ and $ in regexes (#19936) 2023-01-20 12:52:49 -06:00
revisions.rake DEV: Apply syntax_tree formatting to `lib/*` 2023-01-09 12:10:19 +00:00
rspec.rake DEV: Apply syntax_tree formatting to `lib/*` 2023-01-09 12:10:19 +00:00
s3.rake DEV: Apply syntax_tree formatting to `lib/*` 2023-01-09 12:10:19 +00:00
scheduler.rake DEV: Apply syntax_tree formatting to `lib/*` 2023-01-09 12:10:19 +00:00
search.rake DEV: Apply syntax_tree formatting to `lib/*` 2023-01-09 12:10:19 +00:00
site.rake DEV: Apply syntax_tree formatting to `lib/*` 2023-01-09 12:10:19 +00:00
site_settings.rake DEV: Apply syntax_tree formatting to `lib/*` 2023-01-09 12:10:19 +00:00
smoke_test.rake DEV: Apply syntax_tree formatting to `lib/*` 2023-01-09 12:10:19 +00:00
svg_icons.rake DEV: Apply syntax_tree formatting to `lib/*` 2023-01-09 12:10:19 +00:00
tags.rake DEV: Apply syntax_tree formatting to `lib/*` 2023-01-09 12:10:19 +00:00
themes.rake DEV: Apply syntax_tree formatting to `lib/*` 2023-01-09 12:10:19 +00:00
topics.rake DEV: Apply syntax_tree formatting to `lib/*` 2023-01-09 12:10:19 +00:00
turbo.rake DEV: Apply syntax_tree formatting to `lib/*` 2023-01-09 12:10:19 +00:00
typepad.thor DEV: Prefer \A and \z over ^ and $ in regexes (#19936) 2023-01-20 12:52:49 -06:00
uploads.rake DEV: Prefer \A and \z over ^ and $ in regexes (#19936) 2023-01-20 12:52:49 -06:00
users.rake DEV: Apply syntax_tree formatting to `lib/*` 2023-01-09 12:10:19 +00:00