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

Commit 34a8273

Browse files
committed
DEV: Migration to backfill bot PM custom field
1 parent b7b9179 commit 34a8273

File tree

3 files changed

+42
-7
lines changed

3 files changed

+42
-7
lines changed

app/controllers/discourse_ai/ai_bot/conversations_controller.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ def index
1515
Topic
1616
.private_messages_for_user(current_user)
1717
.where(user: current_user) # Only show PMs where the current user is the author
18-
.joins(:topic_users)
19-
.where(topic_users: { user_id: bot_user_ids })
18+
.joins("INNER JOIN topic_custom_fields tcf ON tcf.topic_id = topics.id")
19+
.where("tcf.name = ? AND tcf.value = 't'", DiscourseAi::AiBot::TOPIC_AI_BOT_PM_FIELD)
2020
.distinct
2121

2222
total = base_query.count

assets/javascripts/initializers/ai-conversations-sidebar.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { bind } from "discourse/lib/decorators";
55
import { withPluginApi } from "discourse/lib/plugin-api";
66
import { i18n } from "discourse-i18n";
77
import AiBotSidebarNewConversation from "../discourse/components/ai-bot-sidebar-new-conversation";
8-
import { isPostFromAiBot } from "../discourse/lib/ai-bot-helper";
98
import { AI_CONVERSATIONS_PANEL } from "../discourse/services/ai-conversations-sidebar-manager";
109

1110
export default {
@@ -249,10 +248,7 @@ export default {
249248
if (
250249
topic?.archetype === "private_message" &&
251250
topic.user_id === currentUser.id &&
252-
(topic.is_bot_pm ||
253-
topic.postStream.posts.some((post) =>
254-
isPostFromAiBot(post, currentUser)
255-
))
251+
topic.is_bot_pm
256252
) {
257253
return aiConversationsSidebarManager.forceCustomSidebar();
258254
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# frozen_string_literal: true
2+
3+
class SetAiBotPmCustomFields < ActiveRecord::Migration[7.2]
4+
def up
5+
# Set the topic custom field for past bot PMs:
6+
# - Created by a "real" user (user_id > 0)
7+
# - Include exactly 2 participants (creator and 1 bot)
8+
# - One participant is a bot (ID <= -1200)
9+
10+
execute <<~SQL
11+
INSERT INTO topic_custom_fields (topic_id, name, value, created_at, updated_at)
12+
SELECT t.id, 'is_ai_bot_pm', 't', NOW(), NOW()
13+
FROM topics t
14+
WHERE t.archetype = 'private_message'
15+
AND t.user_id > 0 -- Created by a real user
16+
AND (
17+
SELECT COUNT(*)
18+
FROM topic_allowed_users tau
19+
WHERE tau.topic_id = t.id
20+
) = 2 -- Only 2 participants total
21+
AND (
22+
SELECT COUNT(*)
23+
FROM topic_allowed_users tau
24+
WHERE tau.topic_id = t.id
25+
AND tau.user_id <= -1200 -- Bot users have IDs <= -1200
26+
) = 1 -- One of those participants is a bot
27+
AND NOT EXISTS (
28+
SELECT 1
29+
FROM topic_custom_fields tcf
30+
WHERE tcf.topic_id = t.id
31+
AND tcf.name = 'is_ai_bot_pm'
32+
) -- Don't duplicate existing custom fields
33+
SQL
34+
end
35+
36+
def down
37+
raise ActiveRecord::IrreversibleMigration
38+
end
39+
end

0 commit comments

Comments
 (0)