fix: upgrade mongoose and refactor
Signed-off-by: Innei <tukon479@gmail.com>
This commit is contained in:
@@ -116,7 +116,7 @@
|
||||
"marked": "4.0.18",
|
||||
"mime-types": "*",
|
||||
"mkdirp": "*",
|
||||
"mongoose": "6.5.0",
|
||||
"mongoose": "6.5.1",
|
||||
"mongoose-aggregate-paginate-v2": "1.0.6",
|
||||
"mongoose-lean-getters": "0.3.5",
|
||||
"mongoose-lean-id": "0.3.0",
|
||||
|
||||
526
pnpm-lock.yaml
generated
526
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
@@ -1,6 +1,6 @@
|
||||
import { ErrorCodeEnum } from '~/constants/error-code.constant'
|
||||
|
||||
import { BusinessException } from './business.exception'
|
||||
import { BusinessException } from './biz.exception'
|
||||
|
||||
export class BanInDemoExcpetion extends BusinessException {
|
||||
constructor() {
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
import { ErrorCodeEnum } from '~/constants/error-code.constant'
|
||||
|
||||
import { BizException } from './biz.exception'
|
||||
|
||||
export class NoContentCanBeModifiedException extends BizException {
|
||||
constructor() {
|
||||
super(ErrorCodeEnum.NoContentCanBeModified)
|
||||
}
|
||||
}
|
||||
@@ -22,7 +22,7 @@ import { isDev } from '~/global/env.global'
|
||||
import { EventManagerService } from '~/processors/helper/helper.event.service'
|
||||
|
||||
import { getIp } from '../../utils/ip.util'
|
||||
import { BizException } from '../exceptions/business.exception'
|
||||
import { BizException } from '../exceptions/biz.exception'
|
||||
import { LoggingInterceptor } from '../interceptors/logging.interceptor'
|
||||
|
||||
type myError = {
|
||||
|
||||
@@ -4,6 +4,7 @@ export enum ErrorCodeEnum {
|
||||
BanInDemo = 'ban_in_demo',
|
||||
MasterLost = 'master_lost',
|
||||
ServerlessError = 'function_error',
|
||||
NoContentCanBeModified = 'no_content_can_be_modified',
|
||||
}
|
||||
|
||||
export const ErrorCode = Object.freeze<Record<ErrorCodeEnum, [string, number]>>(
|
||||
@@ -12,5 +13,9 @@ export const ErrorCode = Object.freeze<Record<ErrorCodeEnum, [string, number]>>(
|
||||
[ErrorCodeEnum.BanInDemo]: ['Demo 模式下此操作不可用', 400],
|
||||
[ErrorCodeEnum.MasterLost]: ['站点主人信息已丢失', 500],
|
||||
[ErrorCodeEnum.ServerlessError]: ['Function 执行报错', 500],
|
||||
[ErrorCodeEnum.NoContentCanBeModified]: [
|
||||
'内容不存在,没有内容可被修改',
|
||||
400,
|
||||
],
|
||||
},
|
||||
)
|
||||
|
||||
@@ -10,7 +10,7 @@ import {
|
||||
import { DocumentType } from '@typegoose/typegoose'
|
||||
import { BeAnObject, ReturnModelType } from '@typegoose/typegoose/lib/types'
|
||||
|
||||
import { BusinessException } from '~/common/exceptions/business.exception'
|
||||
import { BusinessException } from '~/common/exceptions/biz.exception'
|
||||
import { CannotFindException } from '~/common/exceptions/cant-find.exception'
|
||||
import { ErrorCodeEnum } from '~/constants/error-code.constant'
|
||||
import { DatabaseService } from '~/processors/database/database.service'
|
||||
@@ -23,9 +23,6 @@ import { InjectModel } from '~/transformers/model.transformer'
|
||||
import { hasChinese } from '~/utils'
|
||||
|
||||
import { ConfigsService } from '../configs/configs.service'
|
||||
import { NoteModel } from '../note/note.model'
|
||||
import { PageModel } from '../page/page.model'
|
||||
import { PostModel } from '../post/post.model'
|
||||
import { ToolService } from '../tool/tool.service'
|
||||
import { UserService } from '../user/user.service'
|
||||
import BlockedKeywords from './block-keywords.json'
|
||||
@@ -53,17 +50,14 @@ export class CommentService {
|
||||
|
||||
private getModelByRefType(
|
||||
type: CommentRefTypes,
|
||||
): ReturnModelType<
|
||||
typeof NoteModel | typeof PostModel | typeof PageModel,
|
||||
BeAnObject
|
||||
> {
|
||||
): ReturnModelType<typeof WriteBaseModel> {
|
||||
switch (type) {
|
||||
case CommentRefTypes.Note:
|
||||
return this.databaseService.getModelByRefType('Note')
|
||||
return this.databaseService.getModelByRefType('Note') as any
|
||||
case CommentRefTypes.Page:
|
||||
return this.databaseService.getModelByRefType('Page')
|
||||
return this.databaseService.getModelByRefType('Page') as any
|
||||
case CommentRefTypes.Post:
|
||||
return this.databaseService.getModelByRefType('Post')
|
||||
return this.databaseService.getModelByRefType('Post') as any
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ import { Inject, Injectable, forwardRef } from '@nestjs/common'
|
||||
import { DocumentType } from '@typegoose/typegoose'
|
||||
|
||||
import { CannotFindException } from '~/common/exceptions/cant-find.exception'
|
||||
import { NoContentCanBeModifiedException } from '~/common/exceptions/no-content-canbe-modified.exception'
|
||||
import { BusinessEvents, EventScope } from '~/constants/business-event.constant'
|
||||
import { EventBusEvents } from '~/constants/event-bus.constant'
|
||||
import { EventManagerService } from '~/processors/helper/helper.event.service'
|
||||
@@ -103,7 +104,14 @@ export class NoteService {
|
||||
this.eventManager.emit(BusinessEvents.NOTE_CREATE, doc.toJSON(), {
|
||||
scope: EventScope.TO_SYSTEM,
|
||||
}),
|
||||
this.imageService.recordImageDimensions(this.noteModel, doc._id),
|
||||
this.imageService.saveImageDimensionsFromMarkdownText(
|
||||
doc.text,
|
||||
doc.images,
|
||||
(images) => {
|
||||
doc.images = images
|
||||
return doc.save()
|
||||
},
|
||||
),
|
||||
doc.hide || doc.password
|
||||
? null
|
||||
: this.eventManager.broadcast(
|
||||
@@ -131,19 +139,45 @@ export class NoteService {
|
||||
doc.modified = new Date()
|
||||
}
|
||||
|
||||
const updated = await this.noteModel.findOneAndUpdate(
|
||||
const updated = await this.noteModel
|
||||
.findOneAndUpdate(
|
||||
{
|
||||
_id: id,
|
||||
},
|
||||
{ ...doc },
|
||||
{ new: true },
|
||||
)
|
||||
.lean({
|
||||
getters: true,
|
||||
})
|
||||
|
||||
if (!updated) {
|
||||
throw new NoContentCanBeModifiedException()
|
||||
}
|
||||
|
||||
process.nextTick(async () => {
|
||||
this.eventManager.emit(EventBusEvents.CleanAggregateCache, null, {
|
||||
scope: EventScope.TO_SYSTEM,
|
||||
})
|
||||
await Promise.all([
|
||||
this.imageService.recordImageDimensions(this.noteModel, id),
|
||||
this.imageService.saveImageDimensionsFromMarkdownText(
|
||||
updated.text,
|
||||
updated.images,
|
||||
(images) => {
|
||||
return this.model
|
||||
.updateOne(
|
||||
{
|
||||
_id: id,
|
||||
},
|
||||
{
|
||||
$set: {
|
||||
images,
|
||||
},
|
||||
},
|
||||
)
|
||||
.exec()
|
||||
},
|
||||
),
|
||||
this.model
|
||||
.findById(id)
|
||||
.lean()
|
||||
|
||||
@@ -33,11 +33,16 @@ export class PageService {
|
||||
slug: slugify(doc.slug),
|
||||
created: new Date(),
|
||||
})
|
||||
process.nextTick(async () => {
|
||||
await Promise.all([
|
||||
this.imageService.recordImageDimensions(this.pageModel, res._id),
|
||||
])
|
||||
})
|
||||
|
||||
this.imageService.saveImageDimensionsFromMarkdownText(
|
||||
doc.text,
|
||||
res.images,
|
||||
(images) => {
|
||||
res.images = images
|
||||
return res.save()
|
||||
},
|
||||
)
|
||||
|
||||
return res
|
||||
}
|
||||
|
||||
@@ -63,7 +68,15 @@ export class PageService {
|
||||
|
||||
process.nextTick(async () => {
|
||||
await Promise.all([
|
||||
this.imageService.recordImageDimensions(this.pageModel, id),
|
||||
this.imageService.saveImageDimensionsFromMarkdownText(
|
||||
newDoc.text,
|
||||
newDoc.images,
|
||||
(images) => {
|
||||
return this.model
|
||||
.updateOne({ _id: id }, { $set: { images } })
|
||||
.exec()
|
||||
},
|
||||
),
|
||||
this.eventManager.broadcast(BusinessEvents.PAGE_UPDATED, newDoc, {
|
||||
scope: EventScope.TO_SYSTEM,
|
||||
}),
|
||||
|
||||
@@ -10,7 +10,7 @@ import {
|
||||
forwardRef,
|
||||
} from '@nestjs/common'
|
||||
|
||||
import { BusinessException } from '~/common/exceptions/business.exception'
|
||||
import { BusinessException } from '~/common/exceptions/biz.exception'
|
||||
import { BusinessEvents, EventScope } from '~/constants/business-event.constant'
|
||||
import { ErrorCodeEnum } from '~/constants/error-code.constant'
|
||||
import { EventBusEvents } from '~/constants/event-bus.constant'
|
||||
@@ -72,6 +72,16 @@ export class PostService {
|
||||
})
|
||||
|
||||
const doc = res.toJSON()
|
||||
|
||||
this.imageService.saveImageDimensionsFromMarkdownText(
|
||||
doc.text,
|
||||
doc.images,
|
||||
(images) => {
|
||||
res.images = images
|
||||
return res.save()
|
||||
},
|
||||
)
|
||||
|
||||
process.nextTick(async () => {
|
||||
await Promise.all([
|
||||
this.eventManager.emit(EventBusEvents.CleanAggregateCache, null, {
|
||||
@@ -98,11 +108,6 @@ export class PostService {
|
||||
scope: EventScope.TO_VISITOR,
|
||||
},
|
||||
),
|
||||
|
||||
this.imageService.recordImageDimensions(
|
||||
this.postModel as MongooseModel<PostModel>,
|
||||
res._id,
|
||||
),
|
||||
])
|
||||
})
|
||||
|
||||
@@ -154,9 +159,13 @@ export class PostService {
|
||||
scope: EventScope.TO_SYSTEM,
|
||||
}),
|
||||
data.text &&
|
||||
this.imageService.recordImageDimensions(
|
||||
this.postModel as MongooseModel<PostModel>,
|
||||
id,
|
||||
this.imageService.saveImageDimensionsFromMarkdownText(
|
||||
data.text,
|
||||
doc?.images,
|
||||
(images) => {
|
||||
oldDocument.images = images
|
||||
return oldDocument.save()
|
||||
},
|
||||
),
|
||||
doc &&
|
||||
this.eventManager.broadcast(
|
||||
|
||||
@@ -14,7 +14,7 @@ import {
|
||||
} from '@nestjs/common'
|
||||
import { Interval } from '@nestjs/schedule'
|
||||
|
||||
import { BizException } from '~/common/exceptions/business.exception'
|
||||
import { BizException } from '~/common/exceptions/biz.exception'
|
||||
import { RedisKeys } from '~/constants/cache.constant'
|
||||
import { ErrorCodeEnum } from '~/constants/error-code.constant'
|
||||
import { DATA_DIR, NODE_REQUIRE_PATH } from '~/constants/path.constant'
|
||||
|
||||
@@ -10,9 +10,8 @@ import {
|
||||
} from '@nestjs/common'
|
||||
import { ReturnModelType } from '@typegoose/typegoose'
|
||||
|
||||
import { BusinessException } from '~/common/exceptions/business.exception'
|
||||
import { BusinessException } from '~/common/exceptions/biz.exception'
|
||||
import { ErrorCodeEnum } from '~/constants/error-code.constant'
|
||||
import { CacheService } from '~/processors/redis/cache.service'
|
||||
import { InjectModel } from '~/transformers/model.transformer'
|
||||
import { getAvatar, sleep } from '~/utils'
|
||||
|
||||
@@ -26,7 +25,6 @@ export class UserService {
|
||||
@InjectModel(UserModel)
|
||||
private readonly userModel: ReturnModelType<typeof UserModel>,
|
||||
private readonly authService: AuthService,
|
||||
private readonly redis: CacheService,
|
||||
) {}
|
||||
public get model() {
|
||||
return this.userModel
|
||||
|
||||
@@ -1,15 +1,9 @@
|
||||
import imageSize from 'image-size'
|
||||
|
||||
import {
|
||||
Injectable,
|
||||
InternalServerErrorException,
|
||||
Logger,
|
||||
} from '@nestjs/common'
|
||||
import { ReturnModelType } from '@typegoose/typegoose'
|
||||
import { Injectable, Logger } from '@nestjs/common'
|
||||
|
||||
import { ConfigsService } from '~/modules/configs/configs.service'
|
||||
import { TextImageRecordType } from '~/shared/model/base.model'
|
||||
import { WriteBaseModel } from '~/shared/model/write-base.model'
|
||||
import { ImageModel } from '~/shared/model/image.model'
|
||||
import { getAverageRGB, pickImagesFromMarkdown } from '~/utils/pic.util'
|
||||
|
||||
import { HttpService } from './helper.http.service'
|
||||
@@ -24,25 +18,19 @@ export class ImageService {
|
||||
this.logger = new Logger(ImageService.name)
|
||||
}
|
||||
|
||||
async recordImageDimensions<T extends WriteBaseModel>(
|
||||
_model: MongooseModel<T>,
|
||||
id: string,
|
||||
async saveImageDimensionsFromMarkdownText(
|
||||
text: string,
|
||||
originImages: ImageModel[] | undefined,
|
||||
updateFn: (images: ImageModel[]) => Promise<any>,
|
||||
) {
|
||||
const model = _model as any as ReturnModelType<typeof WriteBaseModel>
|
||||
const document = await model.findById(id).lean()
|
||||
if (!document) {
|
||||
throw new InternalServerErrorException(
|
||||
`document not found, can not record image dimensions`,
|
||||
)
|
||||
}
|
||||
const { text } = document
|
||||
const newImages = pickImagesFromMarkdown(text)
|
||||
|
||||
const result = [] as TextImageRecordType[]
|
||||
const result = [] as ImageModel[]
|
||||
|
||||
const oldImages = document.images || []
|
||||
const oldImagesMap = new Map(oldImages.map((image) => [image.src, image]))
|
||||
const task = [] as Promise<TextImageRecordType>[]
|
||||
const oldImagesMap = new Map(
|
||||
(originImages ?? []).map((image) => [image.src, image]),
|
||||
)
|
||||
const task = [] as Promise<ImageModel>[]
|
||||
for (const src of newImages) {
|
||||
const originImage = oldImagesMap.get(src)
|
||||
const keys = new Set(Object.keys(originImage || {}))
|
||||
@@ -58,7 +46,7 @@ export class ImageService {
|
||||
result.push(originImage)
|
||||
continue
|
||||
}
|
||||
const promise = new Promise<TextImageRecordType>((resolve) => {
|
||||
const promise = new Promise<ImageModel>((resolve) => {
|
||||
this.logger.log(`Get --> ${src}`)
|
||||
this.getOnlineImageSizeAndMeta(src)
|
||||
.then(({ size, accent }) => {
|
||||
@@ -91,11 +79,7 @@ export class ImageService {
|
||||
const images = await Promise.all(task)
|
||||
result.push(...images)
|
||||
|
||||
await model.updateOne(
|
||||
{ _id: id },
|
||||
// 过滤多余的
|
||||
{ images: result.filter(({ src }) => newImages.includes(src!)) },
|
||||
)
|
||||
await updateFn(result.filter(({ src }) => newImages.includes(src!)))
|
||||
}
|
||||
|
||||
getOnlineImageSizeAndMeta = async (image: string) => {
|
||||
|
||||
@@ -5,8 +5,6 @@ import Paginate from 'mongoose-paginate-v2'
|
||||
import { ApiHideProperty } from '@nestjs/swagger'
|
||||
import { index, modelOptions, plugin } from '@typegoose/typegoose'
|
||||
|
||||
import { ImageModel } from './image.model'
|
||||
|
||||
const mongooseLeanGetters = require('mongoose-lean-getters')
|
||||
@plugin(mongooseLeanVirtuals)
|
||||
@plugin(Paginate)
|
||||
@@ -36,5 +34,3 @@ export class BaseModel {
|
||||
return ['created', 'id', '_id']
|
||||
}
|
||||
}
|
||||
|
||||
export type { ImageModel as TextImageRecordType }
|
||||
|
||||
Reference in New Issue
Block a user