Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 30 additions & 9 deletions app/controllers/discourse_ai/sentiment/sentiment_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ class SentimentController < ::Admin::StaffController
include Constants
requires_plugin ::DiscourseAi::PLUGIN_NAME

DEFAULT_POSTS_LIMIT = 50
MAX_POSTS_LIMIT = 100

def posts
group_by = params.required(:group_by)&.to_sym
group_value = params.required(:group_value).presence
Expand All @@ -15,10 +18,13 @@ def posts

raise Discourse::InvalidParameters if %i[category tag].exclude?(group_by)

limit = fetch_limit_from_params(default: DEFAULT_POSTS_LIMIT, max: MAX_POSTS_LIMIT)
offset = params[:offset].to_i || 0

case group_by
when :category
grouping_clause = "c.name"
grouping_join = "INNER JOIN categories c ON c.id = t.category_id"
grouping_join = "" # categories already joined
when :tag
grouping_clause = "tags.name"
grouping_join =
Expand All @@ -38,6 +44,11 @@ def posts
u.username,
u.name,
u.uploaded_avatar_id,
c.id AS category_id,
c.name AS category_name,
c.color AS category_color,
c.slug AS category_slug,
c.description AS category_description,
(CASE
WHEN (cr.classification::jsonb->'positive')::float > :threshold THEN 'positive'
WHEN (cr.classification::jsonb->'negative')::float > :threshold THEN 'negative'
Expand All @@ -47,6 +58,7 @@ def posts
INNER JOIN topics t ON t.id = p.topic_id
INNER JOIN classification_results cr ON cr.target_id = p.id AND cr.target_type = 'Post'
LEFT JOIN users u ON u.id = p.user_id
LEFT JOIN categories c ON c.id = t.category_id
#{grouping_join}
WHERE
#{grouping_clause} = :group_value AND
Expand All @@ -56,22 +68,31 @@ def posts
((:start_date IS NULL OR p.created_at > :start_date) AND (:end_date IS NULL OR p.created_at < :end_date))
AND p.deleted_at IS NULL
ORDER BY p.created_at DESC
LIMIT :limit OFFSET :offset
SQL
group_value: group_value,
start_date: start_date,
end_date: end_date,
threshold: threshold,
limit: limit + 1,
offset: offset,
)

has_more = posts.length > limit
posts.pop if has_more

render_json_dump(
serialize_data(
posts,
AiSentimentPostSerializer,
scope: guardian,
add_raw: true,
add_excerpt: true,
add_title: true,
),
posts:
serialize_data(
posts,
AiSentimentPostSerializer,
scope: guardian,
add_raw: true,
add_excerpt: true,
add_title: true,
),
has_more: has_more,
next_offset: has_more ? offset + limit : nil,
)
end
end
Expand Down
13 changes: 12 additions & 1 deletion app/serializers/ai_sentiment_post_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ class AiSentimentPostSerializer < ApplicationSerializer
:avatar_template,
:excerpt,
:sentiment,
:truncated
:truncated,
:category

def avatar_template
User.avatar_template(object.username, object.uploaded_avatar_id)
Expand All @@ -23,4 +24,14 @@ def excerpt
def truncated
true
end

def category
{
id: object.category_id,
name: object.category_name,
color: object.category_color,
slug: object.category_slug,
description: object.category_description,
}
end
end
Loading