This repository has been archived on 2023-03-18. You can view files and clone it, but cannot push or open issues or pull requests.
osr-discourse-src/plugins/chat/app/controllers/chat/api/channels_archives_controller.rb
Joffrey JAFFEUX 12a18d4d55
DEV: properly namespace chat (#20690)
This commit main goal was to comply with Zeitwerk and properly rely on autoloading. To achieve this, most resources have been namespaced under the `Chat` module.

- Given all models are now namespaced with `Chat::` and would change the stored types in DB when using polymorphism or STI (single table inheritance), this commit uses various Rails methods to ensure proper class is loaded and the stored name in DB is unchanged, eg: `Chat::Message` model will be stored as `"ChatMessage"`, and `"ChatMessage"` will correctly load `Chat::Message` model.
- Jobs are now using constants only, eg: `Jobs::Chat::Foo` and should only be enqueued this way

Notes:
- This commit also used this opportunity to limit the number of registered css files in plugin.rb
- `discourse_dev` support has been removed within this commit and will be reintroduced later

<!-- NOTE: All pull requests should have tests (rspec in Ruby, qunit in JavaScript). If your code does not include test coverage, please include an explanation of why it was omitted. -->
2023-03-17 14:24:38 +01:00

56 lines
1.7 KiB
Ruby

# frozen_string_literal: true
class Chat::Api::ChannelsArchivesController < Chat::Api::ChannelsController
def create
existing_archive = channel_from_params.chat_channel_archive
if existing_archive.present?
guardian.ensure_can_change_channel_status!(channel_from_params, :archived)
raise Discourse::InvalidAccess if !existing_archive.failed?
Chat::ChannelArchiveService.retry_archive_process(chat_channel: channel_from_params)
return render json: success_json
end
new_topic = archive_params[:type] == "new_topic"
raise Discourse::InvalidParameters if new_topic && archive_params[:title].blank?
raise Discourse::InvalidParameters if !new_topic && archive_params[:topic_id].blank?
if !guardian.can_change_channel_status?(channel_from_params, :read_only)
raise Discourse::InvalidAccess.new(I18n.t("chat.errors.channel_cannot_be_archived"))
end
begin
Chat::ChannelArchiveService.create_archive_process(
chat_channel: channel_from_params,
acting_user: current_user,
topic_params: topic_params,
)
rescue Chat::ChannelArchiveService::ArchiveValidationError => err
return render json: failed_json.merge(errors: err.errors), status: 400
end
render json: success_json
end
private
def archive_params
@archive_params ||=
params
.require(:archive)
.tap do |ca|
ca.require(:type)
ca.permit(:title, :topic_id, :category_id, tags: [])
end
end
def topic_params
@topic_params ||= {
topic_id: archive_params[:topic_id],
topic_title: archive_params[:title],
category_id: archive_params[:category_id],
tags: archive_params[:tags],
}
end
end