2023-10-23 02:00:58 -04:00
|
|
|
# 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
|
2024-02-15 00:37:59 -05:00
|
|
|
|
2023-10-23 02:00:58 -04:00
|
|
|
DiscourseAi::AiBot::EntryPoint::BOTS.each do |id, bot_name, name|
|
2024-01-10 23:56:40 -05:00
|
|
|
if id == DiscourseAi::AiBot::EntryPoint::FAKE_ID
|
|
|
|
next if Rails.env.production?
|
|
|
|
end
|
2023-10-23 02:00:58 -04:00
|
|
|
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
|
2024-01-04 22:39:32 -05:00
|
|
|
user.update_columns(active: true)
|
2023-10-23 02:00:58 -04:00
|
|
|
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
|
2024-01-04 22:39:32 -05:00
|
|
|
user.update_columns(active: false) if user.active
|
2023-10-23 02:00:58 -04:00
|
|
|
else
|
|
|
|
user.destroy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|