FIX: Move image caption group check logic to server side (#645)

* FIX: User groups error before initialization
* DEV: Move group check logic to server side
This commit is contained in:
Keegan George 2024-05-27 17:29:11 -07:00 committed by GitHub
parent 71affe75bf
commit dae9d6f14e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 7 additions and 8 deletions

View File

@ -3,24 +3,19 @@ import { LinkTo } from "@ember/routing";
import dIcon from "discourse-common/helpers/d-icon"; import dIcon from "discourse-common/helpers/d-icon";
import i18n from "discourse-common/helpers/i18n"; import i18n from "discourse-common/helpers/i18n";
function showAiPreferences(user, siteSettings) { function showAiPreferences(user) {
// Since we only have one AI related user setting we don't show // Since we only have one AI related user setting we don't show
// AI preferences if these conditions aren't met. // AI preferences if these conditions aren't met.
// If we add more user settings in the future we can move this // 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. // logic to the the specific settings and conditionally show it in the template.
const userGroups = user.groups.map((g) => g.id); return user?.user_allowed_ai_auto_image_captions;
const captionGroups = siteSettings.ai_auto_image_caption_allowed_groups
.split("|")
.map((id) => parseInt(id, 10));
return userGroups.some((groupId) => captionGroups.includes(groupId));
} }
export default class AutoImageCaptionSetting extends Component { export default class AutoImageCaptionSetting extends Component {
static shouldRender(outletArgs, helper) { static shouldRender(outletArgs, helper) {
return ( return (
helper.siteSettings.discourse_ai_enabled && helper.siteSettings.discourse_ai_enabled &&
showAiPreferences(outletArgs.model, helper.siteSettings) showAiPreferences(outletArgs.model)
); );
} }

View File

@ -43,6 +43,10 @@ module DiscourseAi
) )
end end
plugin.add_to_serializer(:current_user, :user_allowed_ai_auto_image_captions) do
scope.user.in_any_groups?(SiteSetting.ai_auto_image_caption_allowed_groups_map)
end
UserUpdater::OPTION_ATTR.push(:auto_image_caption) UserUpdater::OPTION_ATTR.push(:auto_image_caption)
plugin.add_to_serializer( plugin.add_to_serializer(
:user_option, :user_option,