Skip to content

A platform for community discussion. Free, open, simple.

FEATURE: Show gists in suggested and related topics (#36303)

Rafael dos Santos Silvacddde9397983
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-gist-toggle.gjs b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-gist-toggle.gjs
index bfea2e96d04..0d60e9cab4e 100644
--- a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-gist-toggle.gjs
+++ b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-gist-toggle.gjs
@@ -1,11 +1,10 @@
import Component from "@glimmer/component";
-import { concat, fn } from "@ember/helper";
+import { fn } from "@ember/helper";
import { action } from "@ember/object";
import { service } from "@ember/service";
import DButton from "discourse/components/d-button";
import DropdownMenu from "discourse/components/dropdown-menu";
import DMenu from "discourse/float-kit/components/d-menu";
-import bodyClass from "discourse/helpers/body-class";
import icon from "discourse/helpers/d-icon";
import { eq } from "discourse/truth-helpers";
import { i18n } from "discourse-i18n";
@@ -49,13 +48,12 @@ export default class AiGistToggle extends Component {
@action
onSelect(optionId) {
- this.gists.setPreference(optionId, this.gists.isPm);
+ this.gists.setPreference(optionId);
this.dMenu.close();
}
<template>
{{#if this.gists.showToggle}}
- {{bodyClass (concat "topic-list-layout-" this.gists.currentPreference)}}
<DMenu
@modalForMobile={{true}}
@autofocus={{true}}
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-topic-gist.gjs b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-topic-gist.gjs
index d01fe693a05..caab71adbde 100644
--- a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-topic-gist.gjs
+++ b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-topic-gist.gjs
@@ -1,15 +1,22 @@
import Component from "@glimmer/component";
import { service } from "@ember/service";
import { htmlSafe } from "@ember/template";
+import bodyClass from "discourse/helpers/body-class";
import { TABLE_AI_LAYOUT } from "../services/gists";
export default class AiTopicGist extends Component {
@service gists;
get shouldShow() {
- return (
- this.gists.currentPreference === TABLE_AI_LAYOUT && this.gists.showToggle
- );
+ if (!this.prefersTableAiLayout) {
+ return false;
+ }
+
+ return this.gists.showToggle || this.hasGist;
+ }
+
+ get prefersTableAiLayout() {
+ return this.gists.currentPreference === TABLE_AI_LAYOUT;
}
get hasGist() {
@@ -26,6 +33,7 @@ export default class AiTopicGist extends Component {
<template>
{{#if this.shouldShow}}
+ {{bodyClass "topic-list-layout-table-ai"}}
{{#if this.hasGist}}
<a href={{@topic.lastUnreadUrl}} class="excerpt">
<div class="excerpt__contents">{{this.gist}}</div>
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/initializers/ai-gist-connector.gjs b/plugins/discourse-ai/assets/javascripts/discourse/initializers/ai-gist-connector.gjs
index 01bfd00169e..dd714e31d97 100644
--- a/plugins/discourse-ai/assets/javascripts/discourse/initializers/ai-gist-connector.gjs
+++ b/plugins/discourse-ai/assets/javascripts/discourse/initializers/ai-gist-connector.gjs
@@ -105,6 +105,10 @@ export default apiInitializer((api) => {
return true;
}
+ if (routeName?.startsWith("topic.")) {
+ return true;
+ }
+
return false;
}
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/services/gists.js b/plugins/discourse-ai/assets/javascripts/discourse/services/gists.js
index b00037a4c0f..fc0a5ebaa0f 100644
--- a/plugins/discourse-ai/assets/javascripts/discourse/services/gists.js
+++ b/plugins/discourse-ai/assets/javascripts/discourse/services/gists.js
@@ -2,7 +2,7 @@ import { tracked } from "@glimmer/tracking";
import Service, { service } from "@ember/service";
const TOPIC_LIST_LAYOUT_KEY = "topicListLayout";
-const PM_TOPIC_LIST_LAYOUT_KEY = "pmTopicListLayout";
+const DEPRECATED_PM_TOPIC_LIST_LAYOUT_KEY = "pmTopicListLayout";
export const TABLE_LAYOUT = "table";
export const TABLE_AI_LAYOUT = "table-ai";
@@ -10,21 +10,60 @@ export const TABLE_AI_LAYOUT = "table-ai";
export default class Gists extends Service {
@service router;
- @tracked preference = localStorage.getItem(TOPIC_LIST_LAYOUT_KEY);
- @tracked pmPreference = localStorage.getItem(PM_TOPIC_LIST_LAYOUT_KEY);
+ @tracked preference = this.#loadPreference();
- get routerAttributes() {
- return this.router.currentRoute.attributes;
+ #loadPreference() {
+ // Migrate from old PM-specific key if it exists and main key doesn't
+ const oldPmPreference = localStorage.getItem(
+ DEPRECATED_PM_TOPIC_LIST_LAYOUT_KEY
+ );
+ const currentPreference = localStorage.getItem(TOPIC_LIST_LAYOUT_KEY);
+
+ if (oldPmPreference && !currentPreference) {
+ // Migrate the PM preference to the unified key
+ localStorage.setItem(TOPIC_LIST_LAYOUT_KEY, oldPmPreference);
+ localStorage.removeItem(DEPRECATED_PM_TOPIC_LIST_LAYOUT_KEY);
+ return oldPmPreference;
+ } else if (oldPmPreference) {
+ // Just clean up the old key if main key exists
+ localStorage.removeItem(DEPRECATED_PM_TOPIC_LIST_LAYOUT_KEY);
+ }
+
+ return currentPreference;
}
- get isPm() {
- const routeName = this.router.currentRouteName;
- return routeName?.startsWith("userPrivateMessages");
+ get routerAttributes() {
+ return this.router.currentRoute.attributes;
}
get topics() {
// covers discovery, filter, and pm routes
- return this.routerAttributes?.list?.topics ?? this.routerAttributes?.topics;
+ const listTopics = this.routerAttributes?.list?.topics;
+ if (listTopics) {
+ return listTopics;
+ }
+
+ const directTopics = this.routerAttributes?.topics;
+ if (directTopics) {
+ return directTopics;
+ }
+
+ const topic = this.routerAttributes?.topic;
+ if (!topic) {
+ return null;
+ }
+
+ const relatedTopics = topic.relatedTopics;
+ if (relatedTopics?.length) {
+ return relatedTopics;
+ }
+
+ const suggestedTopics = topic.suggestedTopics;
+ if (suggestedTopics?.length) {
+ return suggestedTopics;
+ }
+
+ return null;
}
get showToggle() {
@@ -32,16 +71,11 @@ export default class Gists extends Service {
}
get currentPreference() {
- return this.isPm ? this.pmPreference : this.preference;
+ return this.preference;
}
- setPreference(value, isPm = false) {
- if (isPm) {
- this.pmPreference = value;
- localStorage.setItem(PM_TOPIC_LIST_LAYOUT_KEY, value);
- } else {
- this.preference = value;
- localStorage.setItem(TOPIC_LIST_LAYOUT_KEY, value);
- }
+ setPreference(value) {
+ this.preference = value;
+ localStorage.setItem(TOPIC_LIST_LAYOUT_KEY, value);
}
}
diff --git a/plugins/discourse-ai/lib/summarization/entry_point.rb b/plugins/discourse-ai/lib/summarization/entry_point.rb
index e0dcacc108d..b600701f03c 100644
--- a/plugins/discourse-ai/lib/summarization/entry_point.rb
+++ b/plugins/discourse-ai/lib/summarization/entry_point.rb
@@ -51,13 +51,21 @@ module DiscourseAi
}
end
- # Don't add gists to the following topic lists.
- gist_skipped_lists = %i[suggested semantic_related]
-
plugin.register_modifier(:topic_query_create_list_topics) do |topics, options|
- if SiteSetting.ai_summarization_enabled && SiteSetting.ai_summary_gists_enabled &&
- !gist_skipped_lists.include?(options[:filter])
+ unless SiteSetting.ai_summarization_enabled && SiteSetting.ai_summary_gists_enabled
+ next topics
+ end
+
+ if topics.respond_to?(:includes)
+ # For ActiveRecord relations, use includes to preload gists
topics.includes(:ai_gist_summary)
+ elsif topics.is_a?(Array) && topics.present?
+ # For Arrays (like suggested topics), preload associations manually
+ ActiveRecord::Associations::Preloader.new(
+ records: topics,
+ associations: :ai_gist_summary,
+ ).call
+ topics
else
topics
end
@@ -67,10 +75,13 @@ module DiscourseAi
:topic_list_item,
:ai_topic_gist,
include_condition: -> { scope.can_see_gists? },
- ) do
- return if gist_skipped_lists.include?(options[:filter])
- object.ai_gist_summary&.summarized_text
- end
+ ) { object.ai_gist_summary&.summarized_text }
+
+ plugin.add_to_serializer(
+ :suggested_topic,
+ :ai_topic_gist,
+ include_condition: -> { scope.can_see_gists? },
+ ) { object.ai_gist_summary&.summarized_text }
# As this event can be triggered quite often, let's be overly cautious enqueueing
# jobs if the feature is disabled.
diff --git a/plugins/discourse-ai/spec/lib/modules/summarization/entry_point_spec.rb b/plugins/discourse-ai/spec/lib/modules/summarization/entry_point_spec.rb
index 1f004d20bde..9328276da01 100644
--- a/plugins/discourse-ai/spec/lib/modules/summarization/entry_point_spec.rb
+++ b/plugins/discourse-ai/spec/lib/modules/summarization/entry_point_spec.rb
@@ -151,6 +151,51 @@ RSpec.describe DiscourseAi::Summarization::EntryPoint do
expect(serialized[:ai_topic_gist]).to be_nil
end
+
+ it "includes gists in suggested topics with TopicListItemSerializer" do
+ main_topic = Fabricate(:topic)
+ gist_topic = topic_ai_gist.target
+
+ suggested_list = topic_query.list_suggested_for(main_topic)
+ suggested_topic = suggested_list.topics.find { |t| t.id == gist_topic.id }
+
+ skip "suggested topic not found in results" if suggested_topic.nil?
+
+ # Verify that ai_gist_summary association is preloaded
+ expect(suggested_topic.association(:ai_gist_summary).loaded?).to eq(true)
+
+ serialized =
+ TopicListItemSerializer.new(
+ suggested_topic,
+ scope: Guardian.new(user),
+ root: false,
+ filter: :suggested,
+ ).as_json
+
+ expect(serialized[:ai_topic_gist]).to eq(topic_ai_gist.summarized_text)
+ end
+
+ it "includes gists in suggested topics with SuggestedTopicSerializer" do
+ main_topic = Fabricate(:topic)
+ gist_topic = topic_ai_gist.target
+
+ suggested_list = topic_query.list_suggested_for(main_topic)
+ suggested_topic = suggested_list.topics.find { |t| t.id == gist_topic.id }
+
+ skip "suggested topic not found in results" if suggested_topic.nil?
+
+ # Verify that ai_gist_summary association is preloaded
+ expect(suggested_topic.association(:ai_gist_summary).loaded?).to eq(true)
+
+ serialized =
+ SuggestedTopicSerializer.new(
+ suggested_topic,
+ scope: Guardian.new(user),
+ root: false,
+ ).as_json
+
+ expect(serialized[:ai_topic_gist]).to eq(topic_ai_gist.summarized_text)
+ end
end
end
end
diff --git a/plugins/discourse-ai/spec/system/summarization/gists_toggle_spec.rb b/plugins/discourse-ai/spec/system/summarization/gists_toggle_spec.rb
index a45dfd845f8..be7222c3744 100644
--- a/plugins/discourse-ai/spec/system/summarization/gists_toggle_spec.rb
+++ b/plugins/discourse-ai/spec/system/summarization/gists_toggle_spec.rb
@@ -84,30 +84,6 @@ describe "Gists Toggle Functionality", type: :system do
expect(page).to have_css("body.topic-list-layout-table-ai")
end
-
- it "PM and public topic toggles are independent from each other" do
- visit("/latest")
- find(".topic-list-layout-trigger").click
- find(
- ".dropdown-menu__item .d-button-label",
- text: I18n.t("js.discourse_ai.summarization.topic_list_layout.button.compact"),
- ).click
-
- visit("/u/#{admin.username}/messages/new")
- find(".topic-list-layout-trigger").click
- find(
- ".dropdown-menu__item .d-button-label",
- text: I18n.t("js.discourse_ai.summarization.topic_list_layout.button.expanded"),
- ).click
-
- visit("/latest")
- expect(page).to have_css("body.topic-list-layout-table")
- expect(page).not_to have_css("body.topic-list-layout-table-ai")
-
- visit("/u/#{admin.username}/messages/new")
- expect(page).to have_css("body.topic-list-layout-table-ai")
- expect(page).not_to have_css("body.topic-list-layout-table")
- end
end
context "when no gists are available" do
diff --git a/plugins/discourse-ai/test/javascripts/integration/components/ai-topic-gist-test.gjs b/plugins/discourse-ai/test/javascripts/integration/components/ai-topic-gist-test.gjs
new file mode 100644
index 00000000000..3b1ad56ce19
--- /dev/null
+++ b/plugins/discourse-ai/test/javascripts/integration/components/ai-topic-gist-test.gjs
@@ -0,0 +1,85 @@
+import EmberObject from "@ember/object";
+import Service from "@ember/service";
+import { render } from "@ember/test-helpers";
+import { module, test } from "qunit";
+import { setupRenderingTest } from "discourse/tests/helpers/component-test";
+import AiTopicGist from "discourse/plugins/discourse-ai/discourse/components/ai-topic-gist";
+import {
+ TABLE_AI_LAYOUT,
+ TABLE_LAYOUT,
+} from "discourse/plugins/discourse-ai/discourse/services/gists";
+
+module("Integration | Component | ai-topic-gist", function (hooks) {
+ setupRenderingTest(hooks);
+
+ hooks.beforeEach(function () {
+ this.topic = EmberObject.create({
+ ai_topic_gist: "A helpful AI summary",
+ escapedExcerpt: "<p>Standard excerpt</p>",
+ lastUnreadUrl: "/t/topic/1",
+ });
+ });
+
+ function registerGistsService(owner, { preference, showToggle = true }) {
+ const layoutPreference = preference;
+ const toggleVisibility = showToggle;
+
+ owner.unregister?.("service:gists");
+
+ owner.register(
+ "service:gists",
+ class extends Service {
+ constructor(...args) {
+ super(...args);
+ this.currentPreference = layoutPreference;
+ this.showToggle = toggleVisibility;
+ }
+ }
+ );
+ }
+
+ test("adds the body class when gists show without the toggle", async function (assert) {
+ assert.strictEqual(
+ this.topic.get("ai_topic_gist"),
+ "A helpful AI summary",
+ "topic is accessible in tests"
+ );
+
+ registerGistsService(this.owner, {
+ preference: TABLE_AI_LAYOUT,
+ showToggle: false,
+ });
+
+ const gistService = this.owner.lookup("service:gists");
+ assert.strictEqual(
+ gistService.currentPreference,
+ TABLE_AI_LAYOUT,
+ "gist service uses the ai layout preference"
+ );
+
+ await render(<template><AiTopicGist @topic={{this.topic}} /></template>);
+
+ assert
+ .dom(".excerpt__contents")
+ .hasText("A helpful AI summary", "gist content is rendered");
+
+ assert.true(
+ document.body.classList.contains("topic-list-layout-table-ai"),
+ "body class is applied when ai layout is active"
+ );
+ });
+
+ test("does not add the body class when compact layout is active", async function (assert) {
+ registerGistsService(this.owner, {
+ preference: TABLE_LAYOUT,
+ showToggle: true,
+ });
+
+ await render(<template><AiTopicGist @topic={{this.topic}} /></template>);
+
+ assert.false(
+ document.body.classList.contains("topic-list-layout-table-ai"),
+ "body class is not applied when ai layout is inactive"
+ );
+ });
+});