DEV: Make more group-based settings client: false (#474)

Affects the following settings:

ai_toxicity_groups_bypass
ai_helper_allowed_groups
ai_helper_custom_prompts_allowed_groups
post_ai_helper_allowed_groups

This turns off client: true for these group-based settings,
because there is no guarantee that the current user gets all
their group memberships serialized to the client. Better to check
server-side first.
This commit is contained in:
Krzysztof Kotlarek 2024-02-19 13:26:24 +11:00 committed by GitHub
parent d66915ecc1
commit dd6b073fc3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 16 additions and 32 deletions

View File

@ -234,12 +234,7 @@ export default class AiHelperContextMenu extends Component {
}
_showUserCustomPrompts() {
const allowedGroups =
this.siteSettings?.ai_helper_custom_prompts_allowed_groups
.split("|")
.map((id) => parseInt(id, 10));
return this.currentUser?.groups.some((g) => allowedGroups.includes(g.id));
return this.currentUser?.can_use_custom_prompts;
}
handleBoundaries() {

View File

@ -186,12 +186,7 @@ export default class AIHelperOptionsMenu extends Component {
}
_showUserCustomPrompts() {
const allowedGroups =
this.siteSettings?.ai_helper_custom_prompts_allowed_groups
.split("|")
.map((id) => parseInt(id, 10));
return this.currentUser?.groups.some((g) => allowedGroups.includes(g.id));
return this.currentUser?.can_use_custom_prompts;
}
@action

View File

@ -1,9 +1,6 @@
export function showComposerAIHelper(outletArgs, helper, featureType) {
const enableHelper = _helperEnabled(helper.siteSettings);
const enableAssistant = _canUseAssistant(
helper.currentUser,
_findAllowedGroups(helper.siteSettings.ai_helper_allowed_groups)
);
const enableAssistant = helper.currentUser.can_use_assistant;
const canShowInPM = helper.siteSettings.ai_helper_allowed_in_pm;
const enableFeature =
helper.siteSettings.ai_helper_enabled_features.includes(featureType);
@ -18,10 +15,7 @@ export function showComposerAIHelper(outletArgs, helper, featureType) {
export function showPostAIHelper(outletArgs, helper) {
return (
_helperEnabled(helper.siteSettings) &&
_canUseAssistant(
helper.currentUser,
_findAllowedGroups(helper.siteSettings.post_ai_helper_allowed_groups)
)
helper.currentUser.can_use_assistant_in_post
);
}
@ -30,11 +24,3 @@ function _helperEnabled(siteSettings) {
siteSettings.discourse_ai_enabled && siteSettings.composer_ai_helper_enabled
);
}
function _findAllowedGroups(setting) {
return setting.split("|").map((id) => parseInt(id, 10));
}
function _canUseAssistant(user, allowedGroups) {
return user?.groups.some((g) => allowedGroups.includes(g.id));
}

View File

@ -46,7 +46,6 @@ discourse_ai:
default: 60
client: false
ai_toxicity_groups_bypass:
client: true
type: group_list
list_type: compact
default: "3" # 3: @staff
@ -172,7 +171,6 @@ discourse_ai:
client: true
validator: "DiscourseAi::Configuration::LlmDependencyValidator"
ai_helper_allowed_groups:
client: true
type: group_list
list_type: compact
default: "3|14" # 3: @staff, 14: @trust_level_4
@ -188,14 +186,12 @@ discourse_ai:
enum: "DiscourseAi::Configuration::LlmEnumerator"
validator: "DiscourseAi::Configuration::LlmValidator"
ai_helper_custom_prompts_allowed_groups:
client: true
type: group_list
list_type: compact
default: "3" # 3: @staff
allow_any: false
refresh: true
post_ai_helper_allowed_groups:
client: true
type: group_list
list_type: compact
default: "3|14" # 3: @staff, 14: @trust_level_4

View File

@ -100,6 +100,18 @@ module DiscourseAi
bots
end
plugin.add_to_serializer(:current_user, :can_use_assistant) do
scope.user.in_any_groups?(SiteSetting.ai_helper_allowed_groups_map)
end
plugin.add_to_serializer(:current_user, :can_use_assistant_in_post) do
scope.user.in_any_groups?(SiteSetting.post_ai_helper_allowed_groups_map)
end
plugin.add_to_serializer(:current_user, :can_use_custom_prompts) do
scope.user.in_any_groups?(SiteSetting.ai_helper_custom_prompts_allowed_groups_map)
end
plugin.register_svg_icon("robot")
plugin.add_to_serializer(