DEV: Move channel creation for category into service (#21167)

This commit moves the category channel creation out
of the Chat::Api::Channel controller and into a
dedicated CreateCategoryChannel service. A follow up
commit will move the DM channel creation out of
the old DirectMessageChannelCreator service.

Also includes a new on_model_errors helper
for chat service class usage, that collects model
validation errors to present in a nice way.

---------

Co-authored-by: Loïc Guitaut <loic@discourse.org>
This commit is contained in:
Martin Brennan
2023-04-24 09:15:16 +10:00
committed by GitHub
parent f0bdb2ee9a
commit 21f93731a3
12 changed files with 345 additions and 54 deletions

View File

@ -155,7 +155,7 @@ RSpec.describe Chat::Api::ChannelsController do
get "/chat/api/channels/#{channel_1.id}"
expect(response.status).to eq(200)
expect(response.parsed_body["channel"]["id"]).to eq(channel_1.id)
expect(response.parsed_body.dig("channel", "id")).to eq(channel_1.id)
end
end
end
@ -248,8 +248,9 @@ RSpec.describe Chat::Api::ChannelsController do
it "creates a channel associated to a category" do
post "/chat/api/channels", params: params
expect(response.status).to eq(200)
new_channel = Chat::Channel.last
new_channel = Chat::Channel.find(response.parsed_body.dig("channel", "id"))
expect(new_channel.name).to eq(params[:channel][:name])
expect(new_channel.slug).to eq("channel-name")
@ -262,16 +263,31 @@ RSpec.describe Chat::Api::ChannelsController do
new_params = params.dup
new_params[:channel][:slug] = "wow-so-cool"
post "/chat/api/channels", params: new_params
expect(response.status).to eq(200)
new_channel = Chat::Channel.last
new_channel = Chat::Channel.find(response.parsed_body.dig("channel", "id"))
expect(new_channel.slug).to eq("wow-so-cool")
end
context "when the user-provided slug already exists for a channel" do
before do
params[:channel][:slug] = "wow-so-cool"
post "/chat/api/channels", params: params
params[:channel][:name] = "new name"
end
it "returns an error" do
post "/chat/api/channels", params: params
expect(response).to have_http_status :unprocessable_entity
end
end
it "creates a channel sets auto_join_users to false by default" do
post "/chat/api/channels", params: params
expect(response.status).to eq(200)
new_channel = Chat::Channel.last
new_channel = Chat::Channel.find(response.parsed_body.dig("channel", "id"))
expect(new_channel.auto_join_users).to eq(false)
end
@ -279,8 +295,9 @@ RSpec.describe Chat::Api::ChannelsController do
it "creates a channel with auto_join_users set to true" do
params[:channel][:auto_join_users] = true
post "/chat/api/channels", params: params
expect(response.status).to eq(200)
new_channel = Chat::Channel.last
new_channel = Chat::Channel.find(response.parsed_body.dig("channel", "id"))
expect(new_channel.auto_join_users).to eq(true)
end
@ -298,6 +315,7 @@ RSpec.describe Chat::Api::ChannelsController do
it "joins the user when auto_join_users is true" do
params[:channel][:auto_join_users] = true
post "/chat/api/channels", params: params
expect(response.status).to eq(200)
created_channel_id = response.parsed_body.dig("channel", "id")
membership_exists =
@ -313,6 +331,7 @@ RSpec.describe Chat::Api::ChannelsController do
it "doesn't join the user when auto_join_users is false" do
params[:channel][:auto_join_users] = false
post "/chat/api/channels", params: params
expect(response.status).to eq(200)
created_channel_id = response.parsed_body.dig("channel", "id")
membership_exists =
@ -519,7 +538,7 @@ RSpec.describe Chat::Api::ChannelsController do
it "joins the user when auto_join_users is true" do
put "/chat/api/channels/#{channel.id}", params: { channel: { auto_join_users: true } }
created_channel_id = response.parsed_body["channel"]["id"]
created_channel_id = response.parsed_body.dig("channel", "id")
membership_exists =
Chat::UserChatChannelMembership.find_by(
user: another_user,
@ -533,7 +552,7 @@ RSpec.describe Chat::Api::ChannelsController do
it "doesn't join the user when auto_join_users is false" do
put "/chat/api/channels/#{channel.id}", params: { channel: { auto_join_users: false } }
created_channel_id = response.parsed_body["channel"]["id"]
created_channel_id = response.parsed_body.dig("channel", "id")
expect(created_channel_id).to be_present