From 03eb1b8c878ceacc1b7ccffba704a10b968e8fba Mon Sep 17 00:00:00 2001 From: Keegan George Date: Mon, 3 Mar 2025 09:37:58 -0800 Subject: [PATCH 01/15] DEV: Conditionally show sentiment reports --- app/models/classification_result.rb | 4 ++ .../initializers/ai-sentiment-admin-nav.js | 38 +++++++++++-------- config/settings.yml | 3 ++ lib/sentiment/entry_point.rb | 18 +++++++-- 4 files changed, 43 insertions(+), 20 deletions(-) diff --git a/app/models/classification_result.rb b/app/models/classification_result.rb index 8da1c16d6..1d3f7b12c 100644 --- a/app/models/classification_result.rb +++ b/app/models/classification_result.rb @@ -2,6 +2,10 @@ class ClassificationResult < ActiveRecord::Base belongs_to :target, polymorphic: true + + def self.has_sentiment_classification? + where(classification_type: "sentiment").exists? + end end # == Schema Information diff --git a/assets/javascripts/initializers/ai-sentiment-admin-nav.js b/assets/javascripts/initializers/ai-sentiment-admin-nav.js index 5b485e4e7..c73b1add6 100644 --- a/assets/javascripts/initializers/ai-sentiment-admin-nav.js +++ b/assets/javascripts/initializers/ai-sentiment-admin-nav.js @@ -1,21 +1,27 @@ import { apiInitializer } from "discourse/lib/api"; -export default apiInitializer("1.15.0", (api) => { - const settings = api.container.lookup("service:site-settings"); +export default apiInitializer("1.15.0", async (api) => { + const currentUser = api.getCurrentUser(); - if (settings.ai_sentiment_enabled) { - api.addAdminSidebarSectionLink("reports", { - name: "sentiment_overview", - route: "admin.dashboardSentiment", - label: "discourse_ai.sentiments.sidebar.overview", - icon: "chart-column", - }); - api.addAdminSidebarSectionLink("reports", { - name: "sentiment_analysis", - route: "adminReports.show", - routeModels: ["sentiment_analysis"], - label: "discourse_ai.sentiments.sidebar.analysis", - icon: "chart-pie", - }); + if ( + !currentUser || + !currentUser.admin || + !currentUser.can_see_sentiment_reports + ) { + return; } + + api.addAdminSidebarSectionLink("reports", { + name: "sentiment_overview", + route: "admin.dashboardSentiment", + label: "discourse_ai.sentiments.sidebar.overview", + icon: "chart-column", + }); + api.addAdminSidebarSectionLink("reports", { + name: "sentiment_analysis", + route: "adminReports.show", + routeModels: ["sentiment_analysis"], + label: "discourse_ai.sentiments.sidebar.analysis", + icon: "chart-pie", + }); }); diff --git a/config/settings.yml b/config/settings.yml index 665c2c83a..2246fed7e 100644 --- a/config/settings.yml +++ b/config/settings.yml @@ -25,6 +25,9 @@ discourse_ai: ai_sentiment_backfill_post_max_age_days: default: 60 hidden: true + ai_sentiment_reports_enabled: + default: true + client: true ai_openai_dall_e_3_url: "https://api.openai.com/v1/images/generations" ai_openai_embeddings_url: diff --git a/lib/sentiment/entry_point.rb b/lib/sentiment/entry_point.rb index 2325f81ee..be0ecdf8d 100644 --- a/lib/sentiment/entry_point.rb +++ b/lib/sentiment/entry_point.rb @@ -14,10 +14,20 @@ def inject_into(plugin) plugin.on(:post_created, &sentiment_analysis_cb) plugin.on(:post_edited, &sentiment_analysis_cb) - EmotionFilterOrder.register!(plugin) - EmotionDashboardReport.register!(plugin) - SentimentDashboardReport.register!(plugin) - SentimentAnalysisReport.register!(plugin) + plugin.add_to_serializer( + :current_user, + :can_see_sentiment_reports, + include_condition: -> do + SiteSetting.ai_sentiment_enabled && SiteSetting.ai_sentiment_reports_enabled + end, + ) { ClassificationResult.has_sentiment_classification? } + + if ClassificationResult.has_sentiment_classification? + EmotionFilterOrder.register!(plugin) + EmotionDashboardReport.register!(plugin) + SentimentDashboardReport.register!(plugin) + SentimentAnalysisReport.register!(plugin) + end end end end From 10c229e97a47b27d24611729fa4213d7aefe490a Mon Sep 17 00:00:00 2001 From: Keegan George Date: Mon, 3 Mar 2025 10:13:32 -0800 Subject: [PATCH 02/15] FIX --- lib/sentiment/entry_point.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/sentiment/entry_point.rb b/lib/sentiment/entry_point.rb index be0ecdf8d..f70d8c6a5 100644 --- a/lib/sentiment/entry_point.rb +++ b/lib/sentiment/entry_point.rb @@ -22,7 +22,8 @@ def inject_into(plugin) end, ) { ClassificationResult.has_sentiment_classification? } - if ClassificationResult.has_sentiment_classification? + if ClassificationResult.has_sentiment_classification? && SiteSetting.ai_sentiment_enabled && + SiteSetting.ai_sentiment_reports_enabled EmotionFilterOrder.register!(plugin) EmotionDashboardReport.register!(plugin) SentimentDashboardReport.register!(plugin) From fb6d722c73edfb6b0583ea8ac211422d2c308e14 Mon Sep 17 00:00:00 2001 From: Keegan George Date: Mon, 3 Mar 2025 15:38:01 -0800 Subject: [PATCH 03/15] FIX: infinite scroll not working --- .../admin-report-sentiment-analysis.gjs | 22 +++++-------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/assets/javascripts/discourse/components/admin-report-sentiment-analysis.gjs b/assets/javascripts/discourse/components/admin-report-sentiment-analysis.gjs index a1ca1e6f9..e3817afe7 100644 --- a/assets/javascripts/discourse/components/admin-report-sentiment-analysis.gjs +++ b/assets/javascripts/discourse/components/admin-report-sentiment-analysis.gjs @@ -18,7 +18,7 @@ import DoughnutChart from "discourse/plugins/discourse-ai/discourse/components/d export default class AdminReportSentimentAnalysis extends Component { @tracked selectedChart = null; - @tracked posts = null; + @tracked posts = []; @tracked hasMorePosts = false; @tracked nextOffset = 0; @tracked showingSelectedChart = false; @@ -71,20 +71,6 @@ export default class AdminReportSentimentAnalysis extends Component { } } - doughnutTitle(data) { - const MAX_TITLE_LENGTH = 18; - const title = data?.title || ""; - const score = data?.total_score ? ` (${data.total_score})` : ""; - - if (title.length + score.length > MAX_TITLE_LENGTH) { - return ( - title.substring(0, MAX_TITLE_LENGTH - score.length) + "..." + score - ); - } - - return title + score; - } - async postRequest() { return await ajax("/discourse-ai/sentiment/posts", { data: { @@ -217,7 +203,10 @@ export default class AdminReportSentimentAnalysis extends Component { this.hasMorePosts = response.has_more; this.nextOffset = response.next_offset; - return response.posts.map((post) => Post.create(post)); + + const mappedPosts = response.posts.map((post) => Post.create(post)); + this.posts.pushObjects(mappedPosts); + return mappedPosts; } catch (e) { popupAjaxError(e); } @@ -228,6 +217,7 @@ export default class AdminReportSentimentAnalysis extends Component { this.showingSelectedChart = false; this.selectedChart = null; this.activeFilter = "all"; + this.posts = []; }