Skip to content
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
9 changes: 9 additions & 0 deletions apps/app/src/features/openai/server/routes/delete-thread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type { ApiV3Response } from '~/server/routes/apiv3/interfaces/apiv3-respo
import loggerFactory from '~/utils/logger';

import type { IApiv3DeleteThreadParams } from '../../interfaces/thread-relation';
import ThreadRelationModel from '../models/thread-relation';
import { getOpenaiService } from '../services/openai';
import { certifyAiService } from './middlewares/certify-ai-service';

Expand Down Expand Up @@ -68,6 +69,14 @@ export const deleteThreadFactory = (crowi: Crowi): RequestHandler[] => {
}

try {
const threadRelation = await ThreadRelationModel.findOne({
_id: threadRelationId,
userId: user._id,
});
if (threadRelation == null) {
return res.apiv3Err(new ErrorV3('Thread not found'), 404);
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ThreadRelationModel.findOne による userId 付きクエリで所有権を確認してからサービスメソッドを呼び出すよう変更

const deletedThreadRelation =
await openaiService.deleteThread(threadRelationId);
return res.apiv3({ deletedThreadRelation });
Expand Down
1 change: 1 addition & 0 deletions apps/app/src/features/openai/server/routes/edit/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ export const postMessageToEditHandlersFactory = (

const threadRelation = await ThreadRelationModel.findOne({
threadId: { $eq: threadId },
userId: user._id,
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

既存の findOne クエリに userId 条件を追加した

});
if (threadRelation == null) {
return res.apiv3Err(new ErrorV3('ThreadRelation not found'), 404);
Expand Down
6 changes: 4 additions & 2 deletions apps/app/src/features/openai/server/routes/get-threads.ts
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

userId を渡して所有者のスレッドのみ取得するよう変更

Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,10 @@ export const getThreadsFactory = (crowi: Crowi): RequestHandler[] => {
);
}

const threads =
await openaiService.getThreadsByAiAssistantId(aiAssistantId);
const threads = await openaiService.getThreadsByAiAssistantId(
aiAssistantId,
user._id,
);

return res.apiv3({ threads });
} catch (err) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import loginRequiredFactory from '~/server/middlewares/login-required';
import type { ApiV3Response } from '~/server/routes/apiv3/interfaces/apiv3-response';
import loggerFactory from '~/utils/logger';

import ThreadRelationModel from '../../models/thread-relation';
import { getOpenaiService } from '../../services/openai';
import { certifyAiService } from '../middlewares/certify-ai-service';

Expand Down Expand Up @@ -81,6 +82,14 @@ export const getMessagesFactory = (crowi: Crowi): RequestHandler[] => {
);
}

const threadRelation = await ThreadRelationModel.findOne({
threadId: { $eq: threadId },
userId: user._id,
});
if (threadRelation == null) {
return res.apiv3Err(new ErrorV3('Thread not found'), 404);
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ThreadRelationModel.findOne による userId 付きクエリで所有権を確認してからメッセージ取得するよう変更した

const messages = await openaiService.getMessageData(
threadId,
user.lang,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,10 @@ export const postMessageHandlersFactory = (crowi: Crowi): RequestHandler[] => {
return res.apiv3Err(new ErrorV3('AI assistant not found'), 404);
}

const threadRelation = await ThreadRelationModel.findOne({ threadId });
const threadRelation = await ThreadRelationModel.findOne({
threadId: { $eq: threadId },
userId: user._id,
});
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ThreadRelationModel.findOne による userId 付きクエリで所有権を確認してからメッセージ取得するよう変更した

if (threadRelation == null) {
return res.apiv3Err(new ErrorV3('ThreadRelation not found'), 404);
}
Expand Down
14 changes: 12 additions & 2 deletions apps/app/src/features/openai/server/services/openai.ts
Copy link
Author

@ryotaro-nagahara ryotaro-nagahara Feb 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getThreadsByAiAssistantIduserId: string を追加すると、274行目のメソッド updateThreads がこのメソッドを userId なしで呼んでいるためコンパイルエラーになりました。
そのため、userId?: stringとし、オプショナルにすることで、ルートハンドラからは userId 付きでユーザー自身のスレッドのみを取得し、内部の updateThreads からは従来通り userId なしで全スレッドを対象に処理できるようにしました。

Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ export interface IOpenaiService {
): Promise<ThreadRelationDocument>;
getThreadsByAiAssistantId(
aiAssistantId: string,
userId?: string,
): Promise<ThreadRelationDocument[]>;
deleteThread(threadRelationId: string): Promise<ThreadRelationDocument>;
deleteExpiredThreads(limit: number, apiCallInterval: number): Promise<void>; // for CronJob
Expand Down Expand Up @@ -290,12 +291,21 @@ class OpenaiService implements IOpenaiService {

async getThreadsByAiAssistantId(
aiAssistantId: string,
userId?: string,
type: ThreadType = ThreadType.KNOWLEDGE,
): Promise<ThreadRelationDocument[]> {
const threadRelations = await ThreadRelationModel.find({
const query: { aiAssistant: string; type: ThreadType; userId?: string } = {
aiAssistant: aiAssistantId,
type,
}).sort({ updatedAt: -1 });
};

if (userId != null) {
query.userId = userId;
}

const threadRelations = await ThreadRelationModel.find(query).sort({
updatedAt: -1,
});
return threadRelations;
}

Expand Down
Loading