1
1
import { Resolver , Subscription , Args , Query , Mutation } from '@nestjs/graphql' ;
2
2
import { ChatCompletionChunk } from './chat.model' ;
3
3
import { ChatProxyService , ChatService } from './chat.service' ;
4
+ import { UserService } from 'src/user/user.service' ;
4
5
import { Chat } from './chat.model' ;
5
6
import { Message } from 'src/chat/message.model' ;
6
7
import {
7
8
NewChatInput ,
8
9
UpateChatTitleInput ,
9
10
ChatInput ,
10
11
} from 'src/chat/dto/chat.input' ;
12
+ import { UseGuards } from '@nestjs/common' ;
13
+ import { ChatGuard } from '../guard/chat.guard' ;
14
+ import { GetUserIdFromToken } from '../decorator/get-auth-token' ;
11
15
12
16
@Resolver ( 'Chat' )
13
17
export class ChatResolver {
14
18
constructor (
15
19
private chatProxyService : ChatProxyService ,
16
20
private chatService : ChatService ,
21
+ private userService : UserService ,
17
22
) { }
18
23
19
24
@Subscription ( ( ) => ChatCompletionChunk , {
@@ -26,7 +31,7 @@ export class ChatResolver {
26
31
for await ( const chunk of iterator ) {
27
32
if ( chunk ) {
28
33
await this . chatService . saveMessage (
29
- input . id ,
34
+ input . chatId ,
30
35
chunk . id ,
31
36
chunk . choices [ 0 ] . delta . content ,
32
37
) ;
@@ -39,8 +44,15 @@ export class ChatResolver {
39
44
}
40
45
}
41
46
47
+ @Query ( ( ) => [ Chat ] , { nullable : true } )
48
+ async getUserChats ( @GetUserIdFromToken ( ) userId : string ) : Promise < Chat [ ] > {
49
+ const user = await this . userService . getUserChats ( userId ) ;
50
+ return user ? user . chats : [ ] ; // Return chats if user exists, otherwise return an empty array
51
+ }
52
+
42
53
@Query ( ( ) => Message , { nullable : true } )
43
54
async getMessageDetail (
55
+ @GetUserIdFromToken ( ) userId : string ,
44
56
@Args ( 'messageId' ) messageId : string ,
45
57
) : Promise < Message > {
46
58
return this . chatService . getMessageById ( messageId ) ;
@@ -51,6 +63,7 @@ export class ChatResolver {
51
63
return this . chatService . getChatHistory ( chatId ) ;
52
64
}
53
65
66
+ @UseGuards ( ChatGuard )
54
67
@Query ( ( ) => Chat , { nullable : true } )
55
68
async getChatDetails ( @Args ( 'chatId' ) chatId : string ) : Promise < Chat > {
56
69
return this . chatService . getChatDetails ( chatId ) ;
@@ -63,21 +76,25 @@ export class ChatResolver {
63
76
64
77
@Mutation ( ( ) => Chat )
65
78
async createChat (
79
+ @GetUserIdFromToken ( ) userId : string ,
66
80
@Args ( 'newChatInput' ) newChatInput : NewChatInput ,
67
81
) : Promise < Chat > {
68
- return this . chatService . createChat ( newChatInput ) ;
82
+ return this . chatService . createChat ( userId , newChatInput ) ;
69
83
}
70
84
85
+ @UseGuards ( ChatGuard )
71
86
@Mutation ( ( ) => Boolean )
72
87
async deleteChat ( @Args ( 'chatId' ) chatId : string ) : Promise < boolean > {
73
88
return this . chatService . deleteChat ( chatId ) ;
74
89
}
75
90
91
+ @UseGuards ( ChatGuard )
76
92
@Mutation ( ( ) => Boolean )
77
93
async clearChatHistory ( @Args ( 'chatId' ) chatId : string ) : Promise < boolean > {
78
94
return this . chatService . clearChatHistory ( chatId ) ;
79
95
}
80
96
97
+ @UseGuards ( ChatGuard )
81
98
@Mutation ( ( ) => Chat , { nullable : true } )
82
99
async updateChatTitle (
83
100
@Args ( 'upateChatTitleInput' ) upateChatTitleInput : UpateChatTitleInput ,
0 commit comments