@@ -18,6 +18,7 @@ import {
1818} from 'omniboxd/messages/entities/message.entity' ;
1919import { WizardTaskService } from 'omniboxd/tasks/wizard-task.service' ;
2020import { Task } from 'omniboxd/tasks/tasks.entity' ;
21+ import { Share } from 'omniboxd/shares/entities/share.entity' ;
2122
2223const TASK_PRIORITY = 5 ;
2324
@@ -50,6 +51,16 @@ export class ConversationsService {
5051 return await this . conversationRepository . save ( conversation ) ;
5152 }
5253
54+ async createConversationForShare ( share : Share ) {
55+ const conversation = this . conversationRepository . create ( {
56+ namespaceId : share . namespaceId ,
57+ userId : null ,
58+ title : '' ,
59+ shareId : share . id ,
60+ } ) ;
61+ return await this . conversationRepository . save ( conversation ) ;
62+ }
63+
5364 async update ( id : string , title : string ) {
5465 const conversation = await this . findOne ( id ) ;
5566 conversation . title = title ;
@@ -192,22 +203,17 @@ export class ConversationsService {
192203 } ;
193204 }
194205
195- async getDetail ( id : string , user : User ) : Promise < ConversationDetailDto > {
196- const conversation = await this . conversationRepository . findOneOrFail ( {
197- where : { id, userId : user . id } ,
198- } ) ;
199-
206+ private convertToConversationDetail (
207+ conversation : Conversation ,
208+ messages : Message [ ] ,
209+ ) : ConversationDetailDto {
200210 const detail : ConversationDetailDto = {
201211 id : conversation . id ,
202212 title : conversation . title ,
203213 created_at : conversation . createdAt . toISOString ( ) ,
204214 updated_at : conversation . updatedAt ?. toISOString ( ) ,
205215 mapping : { } ,
206216 } ;
207- const messages = await this . messagesService . findAll (
208- user . id ,
209- conversation . id ,
210- ) ;
211217 if ( messages . length === 0 ) {
212218 return detail ;
213219 }
@@ -251,6 +257,34 @@ export class ConversationsService {
251257 return detail ;
252258 }
253259
260+ async getConversationForUser (
261+ conversationId : string ,
262+ user : User ,
263+ ) : Promise < ConversationDetailDto > {
264+ const conversation = await this . conversationRepository . findOneOrFail ( {
265+ where : { id : conversationId , userId : user . id } ,
266+ } ) ;
267+ const messages = await this . messagesService . findAll (
268+ user . id ,
269+ conversation . id ,
270+ ) ;
271+ return this . convertToConversationDetail ( conversation , messages ) ;
272+ }
273+
274+ async getConversationForShare (
275+ conversationId : string ,
276+ share : Share ,
277+ ) : Promise < ConversationDetailDto > {
278+ const conversation = await this . conversationRepository . findOneOrFail ( {
279+ where : { id : conversationId , shareId : share . id } ,
280+ } ) ;
281+ const messages = await this . messagesService . findAll (
282+ undefined ,
283+ conversation . id ,
284+ ) ;
285+ return this . convertToConversationDetail ( conversation , messages ) ;
286+ }
287+
254288 async findOne ( id : string ) {
255289 return await this . conversationRepository . findOneOrFail ( {
256290 where : { id } ,
0 commit comments