FIX: gists are not html safe (#931)

Also allow "Everyone" in ai_hot_topic_gists_allowed_groups
This commit is contained in:
Sam 2024-11-20 10:54:49 +11:00 committed by GitHub
parent f09e74c05e
commit a0aec48606
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 28 additions and 8 deletions

View File

@ -1,7 +1,6 @@
import Component from "@glimmer/component"; import Component from "@glimmer/component";
import { service } from "@ember/service"; import { service } from "@ember/service";
import { htmlSafe } from "@ember/template"; import { htmlSafe } from "@ember/template";
import { emojiUnescape, sanitize } from "discourse/lib/text";
export default class AiTopicGist extends Component { export default class AiTopicGist extends Component {
@service gists; @service gists;
@ -10,20 +9,30 @@ export default class AiTopicGist extends Component {
return this.gists.preference === "table-ai" && this.gists.shouldShow; return this.gists.preference === "table-ai" && this.gists.shouldShow;
} }
get gistOrExcerpt() { get hasGist() {
const topic = this.args.topic; return !!this.gist;
const gist = topic.get("ai_topic_gist"); }
const excerpt = emojiUnescape(sanitize(topic.get("excerpt")));
return gist || excerpt; get gist() {
return this.args.topic.get("ai_topic_gist");
}
get escapedExceprt() {
return this.args.topic.get("escapedExcerpt");
} }
<template> <template>
{{#if this.shouldShow}} {{#if this.shouldShow}}
{{#if this.gistOrExcerpt}} {{#if this.hasGist}}
<div class="excerpt"> <div class="excerpt">
<div>{{htmlSafe this.gistOrExcerpt}}</div> <div>{{this.gist}}</div>
</div> </div>
{{else}}
{{#if this.esacpedExceprt}}
<div class="excerpt">
<div>{{htmlSafe this.escapedExceprt}}</div>
</div>
{{/if}}
{{/if}} {{/if}}
{{/if}} {{/if}}
</template> </template>

View File

@ -24,6 +24,9 @@ module DiscourseAi
def can_see_gists? def can_see_gists?
return false if !SiteSetting.ai_summarization_enabled return false if !SiteSetting.ai_summarization_enabled
return false if SiteSetting.ai_summarize_max_hot_topics_gists_per_batch.zero? return false if SiteSetting.ai_summarize_max_hot_topics_gists_per_batch.zero?
if SiteSetting.ai_hot_topic_gists_allowed_groups.to_s == Group::AUTO_GROUPS[:everyone].to_s
return true
end
return false if anonymous? return false if anonymous?
return false if SiteSetting.ai_hot_topic_gists_allowed_groups_map.empty? return false if SiteSetting.ai_hot_topic_gists_allowed_groups_map.empty?

View File

@ -89,6 +89,14 @@ describe DiscourseAi::GuardianExtensions do
end end
end end
context "when setting is set to everyone" do
before { SiteSetting.ai_hot_topic_gists_allowed_groups = Group::AUTO_GROUPS[:everyone] }
it "returns true" do
expect(guardian.can_see_gists?).to eq(true)
end
end
context "when there is a user but it's not a member of the allowed groups" do context "when there is a user but it's not a member of the allowed groups" do
before { SiteSetting.ai_hot_topic_gists_allowed_groups = "" } before { SiteSetting.ai_hot_topic_gists_allowed_groups = "" }