discourse-ai/lib/modules/ai_bot/site_settings_extension.rb
Sam 1500308437
FEATURE: defer creation of bot users (#258)
Also fixes it so users without bot in header can send it messages.

Previous to this change we would seed all bots with database seeds.

This lead to lots of confusion for people who do not enable ai bot.

Instead:

1. We do not seed any bots **until** user enables the ai_bot_enabled setting
2. If it is disabled we will
  a. If no messages were created by bot - delete it
  b. Otherwise we will deactivate account
2023-10-23 17:00:58 +11:00

42 lines
1.2 KiB
Ruby

# frozen_string_literal: true
module DiscourseAi::AiBot::SiteSettingsExtension
def self.enable_or_disable_ai_bots
enabled_bots = SiteSetting.ai_bot_enabled_chat_bots_map
enabled_bots = [] if !SiteSetting.ai_bot_enabled
DiscourseAi::AiBot::EntryPoint::BOTS.each do |id, bot_name, name|
active = enabled_bots.include?(name)
user = User.find_by(id: id)
if active
if !user
user =
User.new(
id: id,
email: "no_email_#{name}",
name: bot_name.titleize,
username: UserNameSuggester.suggest(bot_name),
active: true,
approved: true,
admin: true,
moderator: true,
trust_level: TrustLevel[4],
)
user.save!(validate: false)
else
user.update!(active: true)
end
elsif !active && user
# will include deleted
has_posts = DB.query_single("SELECT 1 FROM posts WHERE user_id = #{id} LIMIT 1").present?
if has_posts
user.update!(active: false) if user.active
else
user.destroy
end
end
end
end
end