mirror of
https://gh.wpcy.net/https://github.com/discourse/discourse.git
synced 2026-05-28 03:34:27 +08:00
Currently in services, we don’t make a distinction between input
parameters, options and dependencies.
This can lead to user input modifying the service behavior, whereas it
was not the developer intention.
This patch addresses the issue by changing how data is provided to
services:
- `params` is now used to hold all data coming from outside (typically
user input from a controller) and a contract will take its values from
`params`.
- `options` is a new key to provide options to a service. This typically
allows changing a service behavior at runtime. It is, of course,
totally optional.
- `dependencies` is actually anything else provided to the service (like
`guardian`) and available directly from the context object.
The `service_params` helper in controllers has been updated to reflect
those changes, so most of the existing services didn’t need specific
changes.
The options block has the same DSL as contracts, as it’s also based on
`ActiveModel`. There aren’t any validations, though. Here’s an example:
```ruby
options do
attribute :allow_changing_hidden, :boolean, default: false
end
```
And here’s an example of how to call a service with the new keys:
```ruby
MyService.call(params: { key1: value1, … }, options: { my_option: true }, guardian:, …)
```
108 lines
3.9 KiB
Ruby
Vendored
108 lines
3.9 KiB
Ruby
Vendored
# frozen_string_literal: true
|
|
|
|
RSpec.describe Chat::RestoreMessage do
|
|
fab!(:current_user) { Fabricate(:user) }
|
|
let!(:guardian) { Guardian.new(current_user) }
|
|
fab!(:message) { Fabricate(:chat_message, user: current_user) }
|
|
|
|
before do
|
|
message.trash!
|
|
message.chat_channel.update!(
|
|
last_message_id: message.chat_channel.latest_not_deleted_message_id,
|
|
)
|
|
end
|
|
|
|
describe ".call" do
|
|
subject(:result) { described_class.call(params:, **dependencies) }
|
|
|
|
let(:dependencies) { { guardian: } }
|
|
|
|
context "when params are not valid" do
|
|
let(:params) { {} }
|
|
|
|
it { is_expected.to fail_a_contract }
|
|
end
|
|
|
|
context "when params are valid" do
|
|
let(:params) { { message_id: message.id, channel_id: message.chat_channel_id } }
|
|
|
|
context "when the user does not have permission to restore" do
|
|
before { message.update!(user: Fabricate(:admin)) }
|
|
|
|
it { is_expected.to fail_a_policy(:invalid_access) }
|
|
end
|
|
|
|
context "when the channel does not match the message" do
|
|
let(:params) { { message_id: message.id, channel_id: Fabricate(:chat_channel).id } }
|
|
|
|
it { is_expected.to fail_to_find_a_model(:message) }
|
|
end
|
|
|
|
context "when the user has permission to restore" do
|
|
it { is_expected.to run_successfully }
|
|
|
|
it "restores the message" do
|
|
result
|
|
expect(Chat::Message.find_by(id: message.id)).not_to be_nil
|
|
end
|
|
|
|
it "updates the channel last_message_id if the message is now the last one in the channel" do
|
|
expect(message.chat_channel.reload.last_message_id).to be_nil
|
|
result
|
|
expect(message.chat_channel.reload.last_message_id).to eq(message.id)
|
|
end
|
|
|
|
it "does not update the channel last_message_id if the message is not the last one in the channel" do
|
|
next_message = Fabricate(:chat_message, chat_channel: message.chat_channel)
|
|
message.chat_channel.update!(last_message: next_message)
|
|
result
|
|
expect(message.chat_channel.reload.last_message_id).to eq(next_message.id)
|
|
end
|
|
|
|
it "publishes associated Discourse and MessageBus events" do
|
|
freeze_time
|
|
messages = nil
|
|
event =
|
|
DiscourseEvent
|
|
.track_events { messages = MessageBus.track_publish { result } }
|
|
.find { |e| e[:event_name] == :chat_message_restored }
|
|
|
|
expect(event).to be_present
|
|
expect(event[:params]).to eq([message, message.chat_channel, current_user])
|
|
expect(
|
|
messages.find { |m| m.channel == "/chat/#{message.chat_channel_id}" }.data,
|
|
).to include({ "type" => "restore" })
|
|
end
|
|
|
|
context "when the message has a thread" do
|
|
fab!(:thread) { Fabricate(:chat_thread, channel: message.chat_channel) }
|
|
|
|
before do
|
|
message.update!(thread: thread)
|
|
thread.update_last_message_id!
|
|
thread.original_message.update!(created_at: message.created_at - 2.hours)
|
|
end
|
|
|
|
it "increments the thread reply count" do
|
|
thread.set_replies_count_cache(1)
|
|
result
|
|
expect(thread.replies_count_cache).to eq(2)
|
|
end
|
|
|
|
it "updates the thread last_message_id if the message is now the last one in the thread" do
|
|
expect(message.thread.reload.last_message_id).to eq(thread.original_message_id)
|
|
result
|
|
expect(message.thread.reload.last_message_id).to eq(message.id)
|
|
end
|
|
|
|
it "does not update the thread last_message_id if the message is not the last one in the channel" do
|
|
next_message =
|
|
Fabricate(:chat_message, thread: message.thread, chat_channel: message.chat_channel)
|
|
message.thread.update!(last_message: next_message)
|
|
expect { result }.not_to change { message.thread.reload.last_message }
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|