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

Commit 5b32d54

Browse files
committed
Refactor to DRY the logic identifying posts from an AI bot
1 parent 23aa739 commit 5b32d54

File tree

4 files changed

+32
-65
lines changed

4 files changed

+32
-65
lines changed

assets/javascripts/discourse/components/post-menu/ai-debug-button.gjs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,12 @@ import Component from "@glimmer/component";
22
import { action } from "@ember/object";
33
import { inject as service } from "@ember/service";
44
import DButton from "discourse/components/d-button";
5+
import { isPostFromAiBot } from "../../lib/ai-bot-helper";
56
import DebugAiModal from "../modal/debug-ai-modal";
67

7-
const MAX_PERSONA_USER_ID = -1200;
8-
98
export default class AiDebugButton extends Component {
109
static shouldRender(args) {
11-
if (
12-
!args.state.currentUser.ai_enabled_chat_bots.any(
13-
(bot) => args.post.username === bot.username
14-
)
15-
) {
16-
// special handling for personas (persona bot users start at ID -1200 and go down)
17-
if (args.post.user_id > MAX_PERSONA_USER_ID) {
18-
return false;
19-
}
20-
}
21-
22-
return true;
10+
return isPostFromAiBot(args.post, args.state.currentUser);
2311
}
2412

2513
// TODO (glimmer-post-menu): Remove this static function and move the code into the button action after the widget code is removed

assets/javascripts/discourse/components/post-menu/ai-share-button.gjs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,15 @@ import Component from "@glimmer/component";
22
import { action } from "@ember/object";
33
import { inject as service } from "@ember/service";
44
import DButton from "discourse/components/d-button";
5+
import { isPostFromAiBot } from "../../lib/ai-bot-helper";
56
import copyConversation from "../../lib/copy-conversation";
67
import ShareModal from "../modal/share-modal";
78

89
const AUTO_COPY_THRESHOLD = 4;
9-
const MAX_PERSONA_USER_ID = -1200;
1010

1111
export default class AiDebugButton extends Component {
1212
static shouldRender(args) {
13-
if (
14-
!args.state.currentUser.ai_enabled_chat_bots.any(
15-
(bot) => args.post.username === bot.username
16-
)
17-
) {
18-
// special handling for personas (persona bot users start at ID -1200 and go down)
19-
if (args.post.user_id > MAX_PERSONA_USER_ID) {
20-
return false;
21-
}
22-
}
23-
24-
return true;
13+
return isPostFromAiBot(args.post, args.state.currentUser);
2514
}
2615

2716
// TODO (glimmer-post-menu): Remove this static function and move the code into the button action after the widget code is removed

assets/javascripts/discourse/lib/ai-bot-helper.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@ import Composer from "discourse/models/composer";
44
import I18n from "I18n";
55
import ShareFullTopicModal from "../components/modal/share-full-topic-modal";
66

7+
const MAX_PERSONA_USER_ID = -1200;
8+
9+
export function isPostFromAiBot(post, currentUser) {
10+
return (
11+
post.user_id <= MAX_PERSONA_USER_ID ||
12+
!!currentUser?.ai_enabled_chat_bots?.any(
13+
(bot) => post.username === bot.username
14+
)
15+
);
16+
}
17+
718
export function showShareConversationModal(modal, topicId) {
819
ajax(`/discourse-ai/ai-bot/shared-ai-conversations/preview/${topicId}.json`)
920
.then((payload) => {

assets/javascripts/initializers/ai-bot-replies.js

Lines changed: 17 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ import AiBotHeaderIcon from "../discourse/components/ai-bot-header-icon";
1212
import AiCancelStreamingButton from "../discourse/components/post-menu/ai-cancel-streaming-button";
1313
import AiDebugButton from "../discourse/components/post-menu/ai-debug-button";
1414
import AiShareButton from "../discourse/components/post-menu/ai-share-button";
15-
import { showShareConversationModal } from "../discourse/lib/ai-bot-helper";
15+
import {
16+
isPostFromAiBot,
17+
showShareConversationModal,
18+
} from "../discourse/lib/ai-bot-helper";
1619
import { streamPostText } from "../discourse/lib/ai-streamer/progress-handlers";
1720

1821
let enabledChatBotIds = [];
@@ -85,20 +88,20 @@ function initializePersonaDecorator(api) {
8588
);
8689
}
8790

88-
const MAX_PERSONA_USER_ID = -1200;
91+
const POST_MENU_BUTTONS_POSITION_BEFORE = [
92+
POST_MENU_LIKE_BUTTON_KEY,
93+
POST_MENU_COPY_LINK_BUTTON_KEY,
94+
POST_MENU_SHARE_BUTTON_KEY,
95+
POST_MENU_SHOW_MORE_BUTTON_KEY,
96+
];
8997

9098
function initializePauseButton(api) {
9199
const transformerRegistered = api.registerValueTransformer(
92100
"post-menu-buttons",
93101
({ value: dag, context: { post } }) => {
94102
if (isGPTBot(post.user)) {
95103
dag.add("ai-cancel-gpt", AiCancelStreamingButton, {
96-
before: [
97-
POST_MENU_LIKE_BUTTON_KEY,
98-
POST_MENU_COPY_LINK_BUTTON_KEY,
99-
POST_MENU_SHARE_BUTTON_KEY,
100-
POST_MENU_SHOW_MORE_BUTTON_KEY,
101-
],
104+
before: POST_MENU_BUTTONS_POSITION_BEFORE,
102105
after: ["ai-share", "ai-debug"],
103106
});
104107
}
@@ -142,12 +145,7 @@ function initializeDebugButton(api) {
142145
({ value: dag, context: { post } }) => {
143146
if (post.topic?.archetype === "private_message") {
144147
dag.add("ai-debug", AiDebugButton, {
145-
before: [
146-
POST_MENU_LIKE_BUTTON_KEY,
147-
POST_MENU_COPY_LINK_BUTTON_KEY,
148-
POST_MENU_SHARE_BUTTON_KEY,
149-
POST_MENU_SHOW_MORE_BUTTON_KEY,
150-
],
148+
before: POST_MENU_BUTTONS_POSITION_BEFORE,
151149
after: "ai-share",
152150
});
153151
}
@@ -175,15 +173,8 @@ function initializeDebugWidgetButton(api) {
175173
return;
176174
}
177175

178-
if (
179-
!currentUser.ai_enabled_chat_bots.any(
180-
(bot) => post.username === bot.username
181-
)
182-
) {
183-
// special handling for personas (persona bot users start at ID -1200 and go down)
184-
if (post.user_id > MAX_PERSONA_USER_ID) {
185-
return;
186-
}
176+
if (!isPostFromAiBot(post, currentUser)) {
177+
return;
187178
}
188179

189180
return {
@@ -207,12 +198,7 @@ function initializeShareButton(api) {
207198
({ value: dag, context: { post } }) => {
208199
if (post.topic?.archetype === "private_message") {
209200
dag.add("ai-share", AiShareButton, {
210-
before: [
211-
POST_MENU_LIKE_BUTTON_KEY,
212-
POST_MENU_COPY_LINK_BUTTON_KEY,
213-
POST_MENU_SHARE_BUTTON_KEY,
214-
POST_MENU_SHOW_MORE_BUTTON_KEY,
215-
],
201+
before: POST_MENU_BUTTONS_POSITION_BEFORE,
216202
});
217203
}
218204

@@ -240,15 +226,8 @@ function initializeShareWidgetButton(api) {
240226
return;
241227
}
242228

243-
if (
244-
!currentUser.ai_enabled_chat_bots.any(
245-
(bot) => post.username === bot.username
246-
)
247-
) {
248-
// special handling for personas (persona bot users start at ID -1200 and go down)
249-
if (post.user_id > MAX_PERSONA_USER_ID) {
250-
return;
251-
}
229+
if (!isPostFromAiBot(post, currentUser)) {
230+
return;
252231
}
253232

254233
return {

0 commit comments

Comments
 (0)