fix: recenly ref

This commit is contained in:
Innei
2022-06-18 17:00:16 +08:00
parent 1c7ba43bb6
commit c5fbd40734
4 changed files with 83 additions and 17 deletions

View File

@@ -120,7 +120,7 @@ export class CommentController {
@Query() query: CommentRefTypesDto,
) {
if (!isMaster) {
await this.commentService.ValidAuthorName(body.author)
await this.commentService.validAuthorName(body.author)
}
const { ref } = query
@@ -176,7 +176,7 @@ export class CommentController {
@IpLocation() ipLocation: IpRecord,
) {
if (!isMaster) {
await this.commentService.ValidAuthorName(author)
await this.commentService.validAuthorName(author)
}
const { id } = params

View File

@@ -148,7 +148,7 @@ export class CommentService {
return comment
}
async ValidAuthorName(author: string): Promise<void> {
async validAuthorName(author: string): Promise<void> {
const isExist = await this.userService.model.findOne({
name: author,
})

View File

@@ -1,9 +1,17 @@
import { IsMongoId, IsOptional, IsString } from 'class-validator'
import {
IsEnum,
IsMongoId,
IsOptional,
IsString,
ValidateIf,
} from 'class-validator'
import { modelOptions, prop } from '@typegoose/typegoose'
import { BaseModel } from '~/shared/model/base.model'
import { CommentRefTypes } from '../comment/comment.model'
export type RefType = {
title: string
url: string
@@ -18,12 +26,6 @@ export class RecentlyModel extends BaseModel {
@prop({ required: true })
@IsString()
content: string
@prop()
@IsOptional()
@IsMongoId()
refId?: string
ref?: RefType
/**
* @deprecated
@@ -39,4 +41,30 @@ export class RecentlyModel extends BaseModel {
@IsString()
@IsOptional()
language?: string
@prop({ refPath: 'refType' })
@IsOptional()
@IsMongoId()
ref: RefType
@prop({ enum: CommentRefTypes })
@IsEnum(CommentRefTypes)
@ValidateIf((model) => model.ref)
refType: CommentRefTypes
get refId() {
return (this.ref as any)?._id ?? this.ref
}
set refId(id: string) {
return
// if (!id) {
// return
// }
// if (this.ref) {
// ;(this.ref as any)._id = id
// } else {
// this.ref = id as any
// }
}
}

View File

@@ -21,7 +21,15 @@ export class RecentlyService {
}
async getAll() {
return this.model.find().sort({ created: -1 }).lean()
return this.model
.find()
.sort({ created: -1 })
.populate({
path: 'ref',
select: '-text',
})
.lean()
}
async getOffset({
@@ -49,10 +57,25 @@ export class RecentlyService {
)
.limit(size)
.sort({ _id: -1 })
.populate([
{
path: 'ref',
select: '-text',
},
])
.lean()
}
async getLatestOne() {
return await this.model.findOne().sort({ created: -1 }).lean()
return await this.model
.findOne()
.sort({ created: -1 })
.populate([
{
path: 'ref',
select: '-text',
},
])
.lean()
}
async create(model: RecentlyModel) {
@@ -67,14 +90,29 @@ export class RecentlyService {
content: model.content,
language: model.language,
project: model.project,
refId: model.refId,
ref: model.refId,
refType: model.refType,
})
const withRef = await this.model
.findById(res._id)
.populate([
{
path: 'ref',
select: '-text',
},
])
.lean()
process.nextTick(async () => {
await this.eventManager.broadcast(BusinessEvents.RECENTLY_CREATE, res, {
scope: EventScope.TO_SYSTEM_VISITOR,
})
await this.eventManager.broadcast(
BusinessEvents.RECENTLY_CREATE,
withRef,
{
scope: EventScope.TO_SYSTEM_VISITOR,
},
)
})
return res
return withRef
}
async delete(id: string) {