FIX: Prevent AI caption setting from showing unless all criteria is met (#753)

This commit is contained in:
Keegan George 2024-08-14 10:17:36 -07:00 committed by GitHub
parent 72980a3d84
commit 23b88537d9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 61 additions and 18 deletions

View File

@ -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)
);
}

View File

@ -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;

View File

@ -1,17 +1,24 @@
<label class="control-label">{{i18n "discourse_ai.title"}}</label>
{{!
Later when we have more preferences,
move the conditional (showAutoImageCaptionSetting)
to be only around the auto-image-caption preference.
}}
{{#if this.showAutoImageCaptionSetting}}
<label class="control-label">{{i18n "discourse_ai.title"}}</label>
<div class="control-group ai-setting">
<PreferenceCheckbox
@labelKey="discourse_ai.ai_helper.image_caption.automatic_caption_setting"
@checked={{this.model.user_option.auto_image_caption}}
data-setting-name="auto-image-caption"
class="pref-auto-image-caption"
<div class="control-group ai-setting">
<PreferenceCheckbox
@labelKey="discourse_ai.ai_helper.image_caption.automatic_caption_setting"
@checked={{this.model.user_option.auto_image_caption}}
data-setting-name="auto-image-caption"
class="pref-auto-image-caption"
/>
</div>
<SaveControls
@id="user_ai_preference_save"
@model={{this.model}}
@action={{this.save}}
@saved={{this.saved}}
/>
</div>
<SaveControls
@id="user_ai_preference_save"
@model={{this.model}}
@action={{this.save}}
@saved={{this.saved}}
/>
{{/if}}

View File

@ -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) }

View File

@ -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