2025-04-10 08:16:31 -07:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module DiscourseAi
|
|
|
|
module Admin
|
|
|
|
class AiFeaturesController < ::Admin::AdminController
|
|
|
|
requires_plugin ::DiscourseAi::PLUGIN_NAME
|
|
|
|
|
|
|
|
def index
|
2025-06-10 14:37:53 -03:00
|
|
|
render json: serialize_modules(DiscourseAi::Configuration::Module.all)
|
2025-04-10 08:16:31 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
def edit
|
|
|
|
raise Discourse::InvalidParameters.new(:id) if params[:id].blank?
|
2025-06-10 14:37:53 -03:00
|
|
|
|
|
|
|
a_module = DiscourseAi::Configuration::Module.find_by(id: params[:id].to_i)
|
|
|
|
|
|
|
|
render json: serialize_module(a_module)
|
2025-04-10 08:16:31 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2025-06-10 14:37:53 -03:00
|
|
|
def serialize_modules(modules)
|
2025-06-09 16:13:09 -03:00
|
|
|
modules.map { |a_module| serialize_module(a_module) }
|
2025-04-10 08:16:31 -07:00
|
|
|
end
|
|
|
|
|
2025-06-09 16:13:09 -03:00
|
|
|
def serialize_module(a_module)
|
|
|
|
return nil if a_module.blank?
|
2025-04-10 08:16:31 -07:00
|
|
|
|
2025-06-10 14:37:53 -03:00
|
|
|
{
|
|
|
|
id: a_module.id,
|
|
|
|
module_name: a_module.name,
|
|
|
|
module_enabled: a_module.enabled?,
|
|
|
|
features: a_module.features.map { |f| serialize_feature(f) },
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def serialize_feature(feature)
|
|
|
|
{
|
|
|
|
name: feature.name,
|
|
|
|
persona: serialize_persona(persona_id_obj_hash[feature.persona_id]),
|
|
|
|
enabled: feature.enabled?,
|
|
|
|
}
|
2025-04-10 08:16:31 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
def serialize_persona(persona)
|
|
|
|
return nil if persona.blank?
|
|
|
|
|
|
|
|
serialize_data(persona, AiFeaturesPersonaSerializer, root: false)
|
|
|
|
end
|
2025-06-10 14:37:53 -03:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def persona_id_obj_hash
|
|
|
|
@persona_id_obj_hash ||=
|
|
|
|
begin
|
|
|
|
setting_names = DiscourseAi::Configuration::Feature.all_persona_setting_names
|
|
|
|
ids = setting_names.map { |sn| SiteSetting.public_send(sn) }
|
|
|
|
|
|
|
|
AiPersona.where(id: ids).index_by(&:id)
|
|
|
|
end
|
|
|
|
end
|
2025-04-10 08:16:31 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|