From 0be292f2470f3f2122a6bc8df03b96777b603445 Mon Sep 17 00:00:00 2001 From: Keegan George Date: Thu, 8 Aug 2024 13:37:26 -0700 Subject: [PATCH] FIX: `auto_image_caption` not always present for current user. (#746) --- lib/ai_helper/entry_point.rb | 10 ++++++++++ spec/lib/modules/ai_helper/entry_point_spec.rb | 11 +++++++++++ spec/models/user_option_spec.rb | 17 +++++++++++++++++ 3 files changed, 38 insertions(+) create mode 100644 spec/models/user_option_spec.rb diff --git a/lib/ai_helper/entry_point.rb b/lib/ai_helper/entry_point.rb index bb3103a4..f6085b2a 100644 --- a/lib/ai_helper/entry_point.rb +++ b/lib/ai_helper/entry_point.rb @@ -57,6 +57,16 @@ module DiscourseAi scope.user.in_any_groups?(SiteSetting.ai_auto_image_caption_allowed_groups_map) end, ) { object.auto_image_caption } + + plugin.add_to_serializer( + :current_user_option, + :auto_image_caption, + include_condition: -> do + SiteSetting.composer_ai_helper_enabled && + SiteSetting.ai_helper_enabled_features.include?("image_caption") && + scope.user.in_any_groups?(SiteSetting.ai_auto_image_caption_allowed_groups_map) + end, + ) { object.auto_image_caption } end end end diff --git a/spec/lib/modules/ai_helper/entry_point_spec.rb b/spec/lib/modules/ai_helper/entry_point_spec.rb index 70268a8c..1eb255c3 100644 --- a/spec/lib/modules/ai_helper/entry_point_spec.rb +++ b/spec/lib/modules/ai_helper/entry_point_spec.rb @@ -36,4 +36,15 @@ describe DiscourseAi::AiHelper::EntryPoint do ) end end + + it "will include auto_image_caption field in the user_option if image caption is enabled" do + assign_fake_provider_to(:ai_helper_model) + assign_fake_provider_to(:ai_helper_image_caption_model) + SiteSetting.composer_ai_helper_enabled = true + SiteSetting.ai_helper_enabled_features = "image_caption" + SiteSetting.ai_auto_image_caption_allowed_groups = "10" # tl0 + serializer = CurrentUserSerializer.new(english_user, scope: Guardian.new(english_user)) + + expect(serializer.user_option.auto_image_caption).to eq(false) + end end diff --git a/spec/models/user_option_spec.rb b/spec/models/user_option_spec.rb new file mode 100644 index 00000000..dd36fef9 --- /dev/null +++ b/spec/models/user_option_spec.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +RSpec.describe UserOption do + before do + assign_fake_provider_to(:ai_helper_model) + assign_fake_provider_to(:ai_helper_image_caption_model) + SiteSetting.composer_ai_helper_enabled = true + SiteSetting.ai_helper_enabled_features = "image_caption" + SiteSetting.ai_auto_image_caption_allowed_groups = "10" # tl0 + end + + describe "#auto_image_caption" do + it "is present" do + expect(described_class.new.auto_image_caption).to eq(false) + end + end +end