Skip to content

Commit e8166b6

Browse files
committed
feat(ai-translation): extend article handling to include PageModel
- Updated ArticleDocument type to incorporate PageModel, allowing for better integration of page articles in the translation service. - Enhanced article mapping and retrieval logic to support pages alongside posts and notes. - Implemented new event handlers for page creation, deletion, and updates to ensure consistent processing of page articles. These changes improve the AI translation service's capability to handle various article types, enhancing overall functionality and user experience. Signed-off-by: Innei <tukon479@gmail.com>
1 parent b991f3e commit e8166b6

File tree

1 file changed

+49
-9
lines changed

1 file changed

+49
-9
lines changed

apps/core/src/modules/ai/ai-translation/ai-translation.service.ts

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import dayjs from 'dayjs'
1919
import removeMdCodeblock from 'remove-md-codeblock'
2020
import { ConfigsService } from '../../configs/configs.service'
2121
import type { NoteModel } from '../../note/note.model'
22+
import type { PageModel } from '../../page/page.model'
2223
import type { PostModel } from '../../post/post.model'
2324
import { AiInFlightService } from '../ai-inflight/ai-inflight.service'
2425
import type { AiStreamEvent } from '../ai-inflight/ai-inflight.types'
@@ -49,7 +50,7 @@ interface ArticleContent {
4950
meta?: { lang?: string }
5051
}
5152

52-
type ArticleDocument = PostModel | NoteModel
53+
type ArticleDocument = PostModel | NoteModel | PageModel
5354

5455
type ArticleEventDocument = ArticleDocument & {
5556
_id?: { toString?: () => string } | string
@@ -60,9 +61,10 @@ type ArticleEventPayload = ArticleEventDocument | { data: string }
6061
type GlobalArticle =
6162
| { document: PostModel; type: CollectionRefTypes.Post }
6263
| { document: NoteModel; type: CollectionRefTypes.Note }
64+
| { document: PageModel; type: CollectionRefTypes.Page }
6365
| {
6466
document: unknown
65-
type: CollectionRefTypes.Page | CollectionRefTypes.Recently
67+
type: CollectionRefTypes.Recently
6668
}
6769

6870
@Injectable()
@@ -221,6 +223,9 @@ export class AiTranslationService implements OnModuleInit {
221223
for (const note of articles.notes) {
222224
articleMap.set(note.id, { title: note.title, type: 'Note' })
223225
}
226+
for (const page of articles.pages) {
227+
articleMap.set(page.id, { title: page.title, type: 'Page' })
228+
}
224229

225230
const createdTaskIds: string[] = []
226231

@@ -297,8 +302,11 @@ export class AiTranslationService implements OnModuleInit {
297302
const noteModel = this.databaseService.getModelByRefType(
298303
CollectionRefTypes.Note,
299304
)
305+
const pageModel = this.databaseService.getModelByRefType(
306+
CollectionRefTypes.Page,
307+
)
300308

301-
const [posts, notes] = await Promise.all([
309+
const [posts, notes, pages] = await Promise.all([
302310
postModel
303311
.find({ isPublished: { $ne: false } })
304312
.select('_id title')
@@ -311,6 +319,7 @@ export class AiTranslationService implements OnModuleInit {
311319
})
312320
.select('_id title')
313321
.lean(),
322+
pageModel.find().select('_id title').lean(),
314323
])
315324

316325
// Build article info map
@@ -321,6 +330,9 @@ export class AiTranslationService implements OnModuleInit {
321330
for (const note of notes) {
322331
articleMap.set(note._id.toString(), { title: note.title, type: 'Note' })
323332
}
333+
for (const page of pages) {
334+
articleMap.set(page._id.toString(), { title: page.title, type: 'Page' })
335+
}
324336

325337
const allArticleIds = Array.from(articleMap.keys())
326338
const total = allArticleIds.length
@@ -443,6 +455,12 @@ export class AiTranslationService implements OnModuleInit {
443455
return article.type === CollectionRefTypes.Note
444456
}
445457

458+
private isPageArticle(
459+
article: GlobalArticle,
460+
): article is { type: CollectionRefTypes.Page; document: PageModel } {
461+
return article.type === CollectionRefTypes.Page
462+
}
463+
446464
private isArticleVisible(article: GlobalArticle): boolean {
447465
if (this.isPostArticle(article)) {
448466
return article.document.isPublished !== false
@@ -458,6 +476,10 @@ export class AiTranslationService implements OnModuleInit {
458476
return true
459477
}
460478

479+
if (this.isPageArticle(article)) {
480+
return true
481+
}
482+
461483
return false
462484
}
463485

@@ -466,17 +488,17 @@ export class AiTranslationService implements OnModuleInit {
466488
*/
467489
private async resolveArticleForTranslation(articleId: string): Promise<{
468490
document: ArticleDocument
469-
type: CollectionRefTypes.Post | CollectionRefTypes.Note
491+
type:
492+
| CollectionRefTypes.Post
493+
| CollectionRefTypes.Note
494+
| CollectionRefTypes.Page
470495
}> {
471496
const article = await this.databaseService.findGlobalById(articleId)
472497
if (!article || !article.document) {
473498
throw new BizException(ErrorCodeEnum.ContentNotFoundCantProcess)
474499
}
475500

476-
if (
477-
article.type === CollectionRefTypes.Recently ||
478-
article.type === CollectionRefTypes.Page
479-
) {
501+
if (article.type === CollectionRefTypes.Recently) {
480502
throw new BizException(ErrorCodeEnum.ContentNotFoundCantProcess)
481503
}
482504

@@ -905,8 +927,11 @@ export class AiTranslationService implements OnModuleInit {
905927
const noteModel = this.databaseService.getModelByRefType(
906928
CollectionRefTypes.Note,
907929
)
930+
const pageModel = this.databaseService.getModelByRefType(
931+
CollectionRefTypes.Page,
932+
)
908933

909-
const [matchedPosts, matchedNotes] = await Promise.all([
934+
const [matchedPosts, matchedNotes, matchedPages] = await Promise.all([
910935
postModel
911936
.find({ title: { $regex: keyword, $options: 'i' } })
912937
.select('_id')
@@ -915,11 +940,16 @@ export class AiTranslationService implements OnModuleInit {
915940
.find({ title: { $regex: keyword, $options: 'i' } })
916941
.select('_id')
917942
.lean(),
943+
pageModel
944+
.find({ title: { $regex: keyword, $options: 'i' } })
945+
.select('_id')
946+
.lean(),
918947
])
919948

920949
matchedRefIds = [
921950
...matchedPosts.map((p) => p._id.toString()),
922951
...matchedNotes.map((n) => n._id.toString()),
952+
...matchedPages.map((p) => p._id.toString()),
923953
]
924954

925955
if (matchedRefIds.length === 0) {
@@ -1008,6 +1038,13 @@ export class AiTranslationService implements OnModuleInit {
10081038
type: CollectionRefTypes.Post,
10091039
}
10101040
}
1041+
for (const a of articles.pages) {
1042+
articleMap[a.id] = {
1043+
title: a.title,
1044+
id: a.id,
1045+
type: CollectionRefTypes.Page,
1046+
}
1047+
}
10111048

10121049
const translationsByRefId = translations.reduce(
10131050
(acc, trans) => {
@@ -1228,6 +1265,7 @@ export class AiTranslationService implements OnModuleInit {
12281265

12291266
@OnEvent(BusinessEvents.POST_DELETE)
12301267
@OnEvent(BusinessEvents.NOTE_DELETE)
1268+
@OnEvent(BusinessEvents.PAGE_DELETE)
12311269
async handleDeleteArticle(event: ArticleEventPayload) {
12321270
const id = this.extractIdFromEvent(event)
12331271
if (!id) return
@@ -1236,6 +1274,7 @@ export class AiTranslationService implements OnModuleInit {
12361274

12371275
@OnEvent(BusinessEvents.POST_CREATE)
12381276
@OnEvent(BusinessEvents.NOTE_CREATE)
1277+
@OnEvent(BusinessEvents.PAGE_CREATE)
12391278
async handleCreateArticle(event: ArticleEventPayload) {
12401279
const aiConfig = await this.configService.get('ai')
12411280

@@ -1278,6 +1317,7 @@ export class AiTranslationService implements OnModuleInit {
12781317

12791318
@OnEvent(BusinessEvents.POST_UPDATE)
12801319
@OnEvent(BusinessEvents.NOTE_UPDATE)
1320+
@OnEvent(BusinessEvents.PAGE_UPDATE)
12811321
async handleUpdateArticle(event: ArticleEventPayload) {
12821322
const aiConfig = await this.configService.get('ai')
12831323
if (

0 commit comments

Comments
 (0)