From 23b88537d9aca44b3917c3f60e43f3576c5e5376 Mon Sep 17 00:00:00 2001 From: Keegan George Date: Wed, 14 Aug 2024 10:17:36 -0700 Subject: [PATCH] FIX: Prevent AI caption setting from showing unless all criteria is met (#753) --- .../user-preferences-nav/ai-preferences.gjs | 13 +++++-- .../discourse/controllers/preferences-ai.js | 11 ++++++ .../discourse/templates/preferences/ai.hbs | 37 +++++++++++-------- .../system/ai_helper/ai_image_caption_spec.rb | 10 +++++ .../page_objects/pages/user_preferences_ai.rb | 8 ++++ 5 files changed, 61 insertions(+), 18 deletions(-) diff --git a/assets/javascripts/discourse/connectors/user-preferences-nav/ai-preferences.gjs b/assets/javascripts/discourse/connectors/user-preferences-nav/ai-preferences.gjs index 8f0283a0..ec289dfb 100644 --- a/assets/javascripts/discourse/connectors/user-preferences-nav/ai-preferences.gjs +++ b/assets/javascripts/discourse/connectors/user-preferences-nav/ai-preferences.gjs @@ -3,19 +3,26 @@ import { LinkTo } from "@ember/routing"; import dIcon from "discourse-common/helpers/d-icon"; import i18n from "discourse-common/helpers/i18n"; -function showAiPreferences(user) { +function showAiPreferences(user, settings) { // Since we only have one AI related user setting we don't show // AI preferences if these conditions aren't met. // If we add more user settings in the future we can move this // logic to the the specific settings and conditionally show it in the template. - return user?.user_allowed_ai_auto_image_captions; + const aiHelperEnabledFeatures = + settings.ai_helper_enabled_features.split("|"); + + return ( + user?.user_allowed_ai_auto_image_captions && + aiHelperEnabledFeatures.includes("image_caption") && + settings.ai_helper_enabled + ); } export default class AutoImageCaptionSetting extends Component { static shouldRender(outletArgs, helper) { return ( helper.siteSettings.discourse_ai_enabled && - showAiPreferences(outletArgs.model) + showAiPreferences(outletArgs.model, helper.siteSettings) ); } diff --git a/assets/javascripts/discourse/controllers/preferences-ai.js b/assets/javascripts/discourse/controllers/preferences-ai.js index 58856a68..5ae900bc 100644 --- a/assets/javascripts/discourse/controllers/preferences-ai.js +++ b/assets/javascripts/discourse/controllers/preferences-ai.js @@ -11,6 +11,17 @@ export default class PreferencesAiController extends Controller { @service siteSettings; @tracked saved = false; + get showAutoImageCaptionSetting() { + const aiHelperEnabledFeatures = + this.siteSettings.ai_helper_enabled_features.split("|"); + + return ( + this.model?.user_allowed_ai_auto_image_captions && + aiHelperEnabledFeatures.includes("image_caption") && + this.siteSettings.ai_helper_enabled + ); + } + @action save() { this.saved = false; diff --git a/assets/javascripts/discourse/templates/preferences/ai.hbs b/assets/javascripts/discourse/templates/preferences/ai.hbs index dceec402..182a2f73 100644 --- a/assets/javascripts/discourse/templates/preferences/ai.hbs +++ b/assets/javascripts/discourse/templates/preferences/ai.hbs @@ -1,17 +1,24 @@ - +{{! + Later when we have more preferences, + move the conditional (showAutoImageCaptionSetting) + to be only around the auto-image-caption preference. + }} +{{#if this.showAutoImageCaptionSetting}} + -
- + +
+ + - - - \ No newline at end of file +{{/if}} \ No newline at end of file diff --git a/spec/system/ai_helper/ai_image_caption_spec.rb b/spec/system/ai_helper/ai_image_caption_spec.rb index 821666d6..b0107893 100644 --- a/spec/system/ai_helper/ai_image_caption_spec.rb +++ b/spec/system/ai_helper/ai_image_caption_spec.rb @@ -22,6 +22,7 @@ RSpec.describe "AI image caption", type: :system, js: true do Group.find_by(id: Group::AUTO_GROUPS[:admins]).add(user) assign_fake_provider_to(:ai_helper_model) assign_fake_provider_to(:ai_helper_image_caption_model) + SiteSetting.ai_helper_enabled = true SiteSetting.ai_helper_enabled_features = "image_caption" sign_in(user) end @@ -87,6 +88,15 @@ RSpec.describe "AI image caption", type: :system, js: true do end describe "automatic image captioning" do + context "when ai helper is disabled" do + before { SiteSetting.ai_helper_enabled = false } + + it "should not have the setting present in the user preferences page" do + user_preferences_ai_page.visit(user) + expect(user_preferences_ai_page).to have_no_ai_preference("pref-auto-image-caption") + end + end + context "when toggling the setting from the user preferences page" do before { user.user_option.update!(auto_image_caption: false) } diff --git a/spec/system/page_objects/pages/user_preferences_ai.rb b/spec/system/page_objects/pages/user_preferences_ai.rb index 889bba76..c4c599f2 100644 --- a/spec/system/page_objects/pages/user_preferences_ai.rb +++ b/spec/system/page_objects/pages/user_preferences_ai.rb @@ -12,6 +12,14 @@ module PageObjects page.find(".#{preference} input").checked? end + def has_ai_preference?(preference) + page.has_css?(".#{preference} input") + end + + def has_no_ai_preference?(preference) + page.has_no_css?(".#{preference} input") + end + def toggle_setting(preference) page.find(".#{preference} input").click end