@@ -6,6 +6,9 @@ class SentimentController < ::Admin::StaffController
66 include Constants
77 requires_plugin ::DiscourseAi ::PLUGIN_NAME
88
9+ DEFAULT_POSTS_LIMIT = 50
10+ MAX_POSTS_LIMIT = 100
11+
912 def posts
1013 group_by = params . required ( :group_by ) &.to_sym
1114 group_value = params . required ( :group_value ) . presence
@@ -15,10 +18,13 @@ def posts
1518
1619 raise Discourse ::InvalidParameters if %i[ category tag ] . exclude? ( group_by )
1720
21+ limit = fetch_limit_from_params ( default : DEFAULT_POSTS_LIMIT , max : MAX_POSTS_LIMIT )
22+ offset = params [ :offset ] . to_i || 0
23+
1824 case group_by
1925 when :category
2026 grouping_clause = "c.name"
21- grouping_join = "INNER JOIN categories c ON c.id = t.category_id"
27+ grouping_join = "" # categories already joined
2228 when :tag
2329 grouping_clause = "tags.name"
2430 grouping_join =
@@ -38,6 +44,11 @@ def posts
3844 u.username,
3945 u.name,
4046 u.uploaded_avatar_id,
47+ c.id AS category_id,
48+ c.name AS category_name,
49+ c.color AS category_color,
50+ c.slug AS category_slug,
51+ c.description AS category_description,
4152 (CASE
4253 WHEN (cr.classification::jsonb->'positive')::float > :threshold THEN 'positive'
4354 WHEN (cr.classification::jsonb->'negative')::float > :threshold THEN 'negative'
@@ -47,6 +58,7 @@ def posts
4758 INNER JOIN topics t ON t.id = p.topic_id
4859 INNER JOIN classification_results cr ON cr.target_id = p.id AND cr.target_type = 'Post'
4960 LEFT JOIN users u ON u.id = p.user_id
61+ LEFT JOIN categories c ON c.id = t.category_id
5062 #{ grouping_join }
5163 WHERE
5264 #{ grouping_clause } = :group_value AND
@@ -56,22 +68,31 @@ def posts
5668 ((:start_date IS NULL OR p.created_at > :start_date) AND (:end_date IS NULL OR p.created_at < :end_date))
5769 AND p.deleted_at IS NULL
5870 ORDER BY p.created_at DESC
71+ LIMIT :limit OFFSET :offset
5972 SQL
6073 group_value : group_value ,
6174 start_date : start_date ,
6275 end_date : end_date ,
6376 threshold : threshold ,
77+ limit : limit + 1 ,
78+ offset : offset ,
6479 )
6580
81+ has_more = posts . length > limit
82+ posts . pop if has_more
83+
6684 render_json_dump (
67- serialize_data (
68- posts ,
69- AiSentimentPostSerializer ,
70- scope : guardian ,
71- add_raw : true ,
72- add_excerpt : true ,
73- add_title : true ,
74- ) ,
85+ posts :
86+ serialize_data (
87+ posts ,
88+ AiSentimentPostSerializer ,
89+ scope : guardian ,
90+ add_raw : true ,
91+ add_excerpt : true ,
92+ add_title : true ,
93+ ) ,
94+ has_more : has_more ,
95+ next_offset : has_more ? offset + limit : nil ,
7596 )
7697 end
7798 end
0 commit comments