Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.

Commit bab6f0b

Browse files
authored
FIX: Ensure category badging present in sentiment reports (#1222)
This PR ensures that the category badges are present in the sentiment analysis report. Since the core change in discourse/discourse#31795, there was a regression in the post list drill-down where category badges were not being shown. This PR fixes that and also ensures icons/emojis are shown when categories make use of them. This PR also adds the category badge in the table list.
1 parent 1b05da7 commit bab6f0b

File tree

4 files changed

+24
-17
lines changed

4 files changed

+24
-17
lines changed

app/controllers/discourse_ai/sentiment/sentiment_controller.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,6 @@ def posts
4646
u.name,
4747
u.uploaded_avatar_id,
4848
c.id AS category_id,
49-
c.name AS category_name,
50-
c.color AS category_color,
51-
c.slug AS category_slug,
52-
c.description AS category_description,
5349
(CASE
5450
WHEN (cr.classification::jsonb->'positive')::float > :threshold THEN 'positive'
5551
WHEN (cr.classification::jsonb->'negative')::float > :threshold THEN 'negative'

app/serializers/ai_sentiment_post_serializer.rb

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class AiSentimentPostSerializer < ApplicationSerializer
1111
:excerpt,
1212
:sentiment,
1313
:truncated,
14-
:category,
14+
:category_id,
1515
:created_at
1616

1717
def avatar_template
@@ -25,14 +25,4 @@ def excerpt
2525
def truncated
2626
object.post_cooked.length > SiteSetting.post_excerpt_maxlength
2727
end
28-
29-
def category
30-
{
31-
id: object.category_id,
32-
name: object.category_name,
33-
color: object.category_color,
34-
slug: object.category_slug,
35-
description: object.category_description,
36-
}
37-
end
3828
end

assets/javascripts/discourse/components/admin-report-sentiment-analysis.gjs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@ import DButton from "discourse/components/d-button";
1111
import HorizontalOverflowNav from "discourse/components/horizontal-overflow-nav";
1212
import PostList from "discourse/components/post-list";
1313
import bodyClass from "discourse/helpers/body-class";
14+
import categoryBadge from "discourse/helpers/category-badge";
1415
import dIcon from "discourse/helpers/d-icon";
1516
import replaceEmoji from "discourse/helpers/replace-emoji";
1617
import { ajax } from "discourse/lib/ajax";
1718
import { popupAjaxError } from "discourse/lib/ajax-error";
1819
import { getAbsoluteURL } from "discourse/lib/get-url";
1920
import discourseLater from "discourse/lib/later";
2021
import { clipboardCopy } from "discourse/lib/utilities";
22+
import Category from "discourse/models/category";
2123
import Post from "discourse/models/post";
2224
import closeOnClickOutside from "discourse/modifiers/close-on-click-outside";
2325
import { i18n } from "discourse-i18n";
@@ -111,6 +113,7 @@ export default class AdminReportSentimentAnalysis extends Component {
111113
get transformedData() {
112114
return this.args.model.data.map((data) => {
113115
return {
116+
category: Category.findById(data.category_id),
114117
title: data.category_name || data.tag_name,
115118
scores: [
116119
data.positive_count,
@@ -139,6 +142,7 @@ export default class AdminReportSentimentAnalysis extends Component {
139142

140143
return this.posts.filter((post) => {
141144
post.topic_title = replaceEmoji(post.topic_title);
145+
post.category = Category.findById(post.category_id);
142146

143147
if (this.activeFilter === "all") {
144148
return true;
@@ -347,7 +351,13 @@ export default class AdminReportSentimentAnalysis extends Component {
347351
)
348352
}}
349353
>
350-
<td class="sentiment-analysis-table__title">{{data.title}}</td>
354+
<td class="sentiment-analysis-table__title">
355+
{{#if data.category}}
356+
{{categoryBadge data.category}}
357+
{{else}}
358+
{{data.title}}
359+
{{/if}}
360+
</td>
351361
<td
352362
class="sentiment-analysis-table__total-score"
353363
>{{data.total_score}}</td>

lib/sentiment/sentiment_analysis_report.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ def self.fetch_data(report, opts)
8080
case grouping
8181
when :category
8282
<<~SQL
83+
c.id AS category_id,
8384
c.name AS category_name,
8485
SQL
8586
when :tag
@@ -90,6 +91,16 @@ def self.fetch_data(report, opts)
9091
raise Discourse::InvalidParameters
9192
end
9293

94+
group_by_clause =
95+
case grouping
96+
when :category
97+
"GROUP BY c.id, c.name"
98+
when :tag
99+
"GROUP BY tags.name"
100+
else
101+
raise Discourse::InvalidParameters
102+
end
103+
93104
grouping_join =
94105
case grouping
95106
when :category
@@ -154,7 +165,7 @@ def self.fetch_data(report, opts)
154165
cr.model_used = 'cardiffnlp/twitter-roberta-base-sentiment-latest' AND
155166
(p.created_at > :report_start AND p.created_at < :report_end)
156167
#{where_clause}
157-
GROUP BY 1
168+
#{group_by_clause}
158169
#{order_by_clause}
159170
SQL
160171
report_start: report.start_date,

0 commit comments

Comments
 (0)