fix: check activity ref type

Signed-off-by: Innei <i@innei.in>
This commit is contained in:
Innei
2024-04-17 20:57:37 +08:00
parent ec58221ee6
commit 9c899870fd
2 changed files with 25 additions and 8 deletions

View File

@@ -19,7 +19,6 @@ import { BadRequestException, Injectable, Logger } from '@nestjs/common'
import { ArticleTypeEnum } from '~/constants/article.constant'
import { BusinessEvents, EventScope } from '~/constants/business-event.constant'
import {
CollectionRefTypes,
NOTE_COLLECTION_NAME,
POST_COLLECTION_NAME,
RECENTLY_COLLECTION_NAME,
@@ -31,7 +30,7 @@ import { CountingService } from '~/processors/helper/helper.counting.service'
import { EventManagerService } from '~/processors/helper/helper.event.service'
import { InjectModel } from '~/transformers/model.transformer'
import { transformDataToPaginate } from '~/transformers/paginate.transformer'
import { getAvatar } from '~/utils'
import { checkRefModelCollectionType, getAvatar } from '~/utils'
import { CommentState } from '../comment/comment.model'
import { CommentService } from '../comment/comment.service'
@@ -513,7 +512,8 @@ export class ActivityService implements OnModuleInit, OnModuleDestroy {
},
})
.populate('ref', 'title nid slug category')
.populate('ref', 'title nid slug subtitle content categoryId')
.lean({ getters: true })
.sort({
created: -1,
@@ -523,13 +523,10 @@ export class ActivityService implements OnModuleInit, OnModuleDestroy {
return Object.assign(
{},
pick(doc, 'created', 'author', 'text', 'avatar'),
doc.ref,
pick(doc.ref, 'title', 'nid', 'slug', 'id'),
!doc.avatar ? { avatar: getAvatar(doc.mail) } : {},
{
type:
'nid' in doc.ref
? CollectionRefTypes.Note
: CollectionRefTypes.Post,
type: checkRefModelCollectionType(doc.ref),
},
)
})

View File

@@ -1,5 +1,6 @@
import { DEMO_MODE } from '~/app.config'
import { BanInDemoExcpetion } from '~/common/exceptions/ban-in-demo.exception'
import { CollectionRefTypes } from '~/constants/db.constant'
/**
* 检查是否在 demo 模式下,禁用此功能
@@ -9,3 +10,22 @@ export const banInDemo = () => {
throw new BanInDemoExcpetion()
}
}
export const checkRefModelCollectionType = (ref: any) => {
if (!ref && typeof ref !== 'object') throw new TypeError()
if ('nid' in ref) {
return CollectionRefTypes.Note
}
if ('title' in ref && 'categoryId' in ref) {
return CollectionRefTypes.Post
}
if ('title' in ref && 'subtitle' in ref) {
return CollectionRefTypes.Page
}
if ('content' in ref) {
return CollectionRefTypes.Recently
}
throw new ReferenceError()
}