@@ -17,10 +17,11 @@ export class ConversationService extends LoggerBase {
17
17
18
18
private async getConversation (
19
19
tenantId : string ,
20
+ segmentId : string ,
20
21
id : string ,
21
22
repo : ConversationRepository ,
22
23
) : Promise < IDbConversation > {
23
- const conversation = await repo . getConversation ( tenantId , id )
24
+ const conversation = await repo . getConversation ( tenantId , segmentId , id )
24
25
25
26
if ( ! conversation ) {
26
27
throw new Error ( `Conversation ${ id } does not exist!` )
@@ -29,10 +30,15 @@ export class ConversationService extends LoggerBase {
29
30
return conversation
30
31
}
31
32
32
- public async generateTitle ( tenantId : string , title : string , isHtml = false ) : Promise < string > {
33
+ public async generateTitle (
34
+ tenantId : string ,
35
+ segmentId : string ,
36
+ title : string ,
37
+ isHtml = false ,
38
+ ) : Promise < string > {
33
39
if ( ! title && getCleanString ( title ) . length === 0 ) {
34
40
const repo = new ConversationRepository ( this . store , this . log )
35
- const count = await repo . getConversationCount ( tenantId )
41
+ const count = await repo . getConversationCount ( tenantId , segmentId )
36
42
37
43
return `conversation-${ count } `
38
44
}
@@ -48,7 +54,7 @@ export class ConversationService extends LoggerBase {
48
54
}
49
55
50
56
static readonly MAX_SLUG_WORD_LENGTH = 10
51
- public async generateSlug ( tenantId : string , title : string ) : Promise < string > {
57
+ public async generateSlug ( tenantId : string , segmentId : string , title : string ) : Promise < string > {
52
58
// Remove non-standart characters and extra whitespaces
53
59
const cleanedTitle = getCleanString ( title )
54
60
@@ -68,7 +74,7 @@ export class ConversationService extends LoggerBase {
68
74
// check generated slug already exists in tenant
69
75
const repo = new ConversationRepository ( this . store , this . log )
70
76
71
- let slugExists = await repo . checkSlugExists ( tenantId , cleanedSlug )
77
+ let slugExists = await repo . checkSlugExists ( tenantId , segmentId , cleanedSlug )
72
78
73
79
// generated slug already exists in the tenant, start adding suffixes and re-check
74
80
if ( slugExists ) {
@@ -78,7 +84,7 @@ export class ConversationService extends LoggerBase {
78
84
79
85
while ( slugExists ) {
80
86
const suffixedSlug = `${ slugCopy } -${ suffix } `
81
- slugExists = await repo . checkSlugExists ( tenantId , cleanedSlug )
87
+ slugExists = await repo . checkSlugExists ( tenantId , segmentId , cleanedSlug )
82
88
suffix += 1
83
89
cleanedSlug = suffixedSlug
84
90
}
@@ -88,23 +94,27 @@ export class ConversationService extends LoggerBase {
88
94
}
89
95
90
96
// returns activity ids that were changed
91
- public async processActivity ( tenantId : string , activityId : string ) : Promise < string [ ] > {
97
+ public async processActivity (
98
+ tenantId : string ,
99
+ segmentId : string ,
100
+ activityId : string ,
101
+ ) : Promise < string [ ] > {
92
102
const repo = new ConversationRepository ( this . store , this . log )
93
103
94
- const activity = await repo . getActivityData ( tenantId , activityId )
104
+ const activity = await repo . getActivityData ( tenantId , segmentId , activityId )
95
105
96
106
if ( activity . parentId ) {
97
- const parent = await repo . getActivityData ( tenantId , activity . parentId )
98
- return await this . addToConversation ( tenantId , activity , parent )
107
+ const parent = await repo . getActivityData ( tenantId , segmentId , activity . parentId )
108
+ return await this . addToConversation ( tenantId , segmentId , activity , parent )
99
109
} else {
100
110
const ids : string [ ] = [ ]
101
111
await processPaginated (
102
112
async ( page ) => {
103
- return repo . getActivities ( tenantId , activity . sourceId , page , 10 )
113
+ return repo . getActivities ( tenantId , segmentId , activity . sourceId , page , 10 )
104
114
} ,
105
115
async ( activities ) => {
106
116
for ( const child of activities ) {
107
- const results = await this . addToConversation ( tenantId , child , activity )
117
+ const results = await this . addToConversation ( tenantId , segmentId , child , activity )
108
118
ids . push ( ...results )
109
119
}
110
120
} ,
@@ -116,6 +126,7 @@ export class ConversationService extends LoggerBase {
116
126
117
127
public async addToConversation (
118
128
tenantId : string ,
129
+ segmentId : string ,
119
130
child : IDbActivityInfo ,
120
131
parent : IDbActivityInfo ,
121
132
) : Promise < string [ ] > {
@@ -133,43 +144,59 @@ export class ConversationService extends LoggerBase {
133
144
134
145
// check if parent is in a conversation already
135
146
if ( parent . conversationId ) {
136
- conversation = await this . getConversation ( tenantId , parent . conversationId , txRepo )
137
- await txRepo . setActivityConversationId ( tenantId , child . id , parent . conversationId )
147
+ conversation = await this . getConversation (
148
+ tenantId ,
149
+ segmentId ,
150
+ parent . conversationId ,
151
+ txRepo ,
152
+ )
153
+ await txRepo . setActivityConversationId ( tenantId , segmentId , child . id , parent . conversationId )
138
154
affectedIds . push ( child . id )
139
155
}
140
156
// if child is already in a conversation
141
157
else if ( child . conversationId ) {
142
- conversation = await this . getConversation ( tenantId , child . conversationId , txRepo )
158
+ conversation = await this . getConversation ( tenantId , segmentId , child . conversationId , txRepo )
143
159
144
160
if ( ! conversation . published ) {
145
161
const txService = new ConversationService ( txStore , this . log )
146
162
const newConversationTitle = await txService . generateTitle (
147
163
tenantId ,
164
+ segmentId ,
148
165
parent . title || parent . body ,
149
166
ConversationService . hasHtmlActivities ( parent . platform as PlatformType ) ,
150
167
)
151
168
152
- const newConversationSlug = await txService . generateSlug ( tenantId , newConversationTitle )
169
+ const newConversationSlug = await txService . generateSlug (
170
+ tenantId ,
171
+ segmentId ,
172
+ newConversationTitle ,
173
+ )
153
174
154
175
await txRepo . setConversationTitleAndSlug (
155
176
tenantId ,
177
+ segmentId ,
156
178
conversation . id ,
157
179
newConversationTitle ,
158
180
newConversationSlug ,
159
181
)
160
182
}
161
183
162
- await txRepo . setActivityConversationId ( tenantId , parent . id , conversation . id )
184
+ await txRepo . setActivityConversationId ( tenantId , segmentId , parent . id , conversation . id )
163
185
affectedIds . push ( parent . id )
164
186
} else {
165
187
// create a new conversation
166
188
const txService = new ConversationService ( txStore , this . log )
167
189
const conversationTitle = await txService . generateTitle (
168
190
tenantId ,
191
+ segmentId ,
169
192
parent . title || parent . body ,
170
193
ConversationService . hasHtmlActivities ( parent . platform as PlatformType ) ,
171
194
)
172
- const conversationSlug = await txService . generateSlug ( tenantId , conversationTitle )
195
+ const conversationSlug = await txService . generateSlug (
196
+ tenantId ,
197
+ segmentId ,
198
+ conversationTitle ,
199
+ )
173
200
const conversationSettings = await txRepo . getConversationSettings ( tenantId )
174
201
const channel = ConversationService . getChannelFromActivity (
175
202
parent . platform as PlatformType ,
@@ -184,6 +211,7 @@ export class ConversationService extends LoggerBase {
184
211
185
212
const conversationId = await txRepo . createConversation (
186
213
tenantId ,
214
+ segmentId ,
187
215
conversationTitle ,
188
216
published ,
189
217
conversationSlug ,
@@ -194,8 +222,8 @@ export class ConversationService extends LoggerBase {
194
222
published,
195
223
}
196
224
197
- await txRepo . setActivityConversationId ( tenantId , parent . id , conversationId )
198
- await txRepo . setActivityConversationId ( tenantId , child . id , conversationId )
225
+ await txRepo . setActivityConversationId ( tenantId , segmentId , parent . id , conversationId )
226
+ await txRepo . setActivityConversationId ( tenantId , segmentId , child . id , conversationId )
199
227
affectedIds . push ( parent . id )
200
228
affectedIds . push ( child . id )
201
229
}
0 commit comments