From cf982603aafdfe36bd124f20049ecfb949353fc5 Mon Sep 17 00:00:00 2001 From: Innei Date: Sun, 25 Jun 2023 23:30:44 +0800 Subject: [PATCH] feat: comment modal add `avatar` and `source` Signed-off-by: Innei --- apps/core/src/modules/comment/comment.controller.ts | 5 +++-- apps/core/src/modules/comment/comment.dto.ts | 13 +++++++++++++ apps/core/src/modules/comment/comment.model.ts | 12 ++++-------- apps/core/src/modules/comment/comment.service.ts | 13 ++++++++++--- 4 files changed, 30 insertions(+), 13 deletions(-) diff --git a/apps/core/src/modules/comment/comment.controller.ts b/apps/core/src/modules/comment/comment.controller.ts index 6530fb68..ceb1c157 100644 --- a/apps/core/src/modules/comment/comment.controller.ts +++ b/apps/core/src/modules/comment/comment.controller.ts @@ -86,7 +86,7 @@ export class CommentController { throw new CannotFindException() } - await this.commentService.replaceMasterAvatarUrl([data]) + await this.commentService.fillAndReplaceAvatarUrl([data]) return data } @@ -153,10 +153,11 @@ export class CommentController { limit: size, page, sort: { pin: -1, created: -1 }, + lean: true, }, ) - await this.commentService.replaceMasterAvatarUrl(comments.docs) + await this.commentService.fillAndReplaceAvatarUrl(comments.docs) this.commentService.cleanDirtyData(comments.docs) return comments } diff --git a/apps/core/src/modules/comment/comment.dto.ts b/apps/core/src/modules/comment/comment.dto.ts index 842d22ff..ae30651e 100644 --- a/apps/core/src/modules/comment/comment.dto.ts +++ b/apps/core/src/modules/comment/comment.dto.ts @@ -38,6 +38,19 @@ export class CommentDto { @IsOptional() @IsBoolean() isWhispers?: boolean + + @IsOptional() + @IsIn(['google', 'github']) + source?: string + + @IsOptional() + @IsUrl( + { require_protocol: true, protocols: ['https'] }, + { + message: '头像必须是合法的 HTTPS URL 哦', + }, + ) + avatar?: string } export class TextOnlyDto { diff --git a/apps/core/src/modules/comment/comment.model.ts b/apps/core/src/modules/comment/comment.model.ts index b71a78c9..aa14e86b 100644 --- a/apps/core/src/modules/comment/comment.model.ts +++ b/apps/core/src/modules/comment/comment.model.ts @@ -5,7 +5,6 @@ import autopopulate from 'mongoose-autopopulate' import { modelOptions, plugin, prop, Ref } from '@typegoose/typegoose' import { BaseModel } from '~/shared/model/base.model' -import { getAvatar } from '~/utils' import { NoteModel } from '../note/note.model' import { PageModel } from '../page/page.model' @@ -124,12 +123,9 @@ export class CommentModel extends BaseModel { @prop({ default: false }) isWhispers?: boolean - _avatar?: string - public get avatar() { - return this._avatar ?? getAvatar(this.mail) - } + @prop() + source?: string - set avatar(value: string) { - this._avatar = value - } + @prop() + avatar?: string } diff --git a/apps/core/src/modules/comment/comment.service.ts b/apps/core/src/modules/comment/comment.service.ts index da9b5a5b..7fd89e9f 100644 --- a/apps/core/src/modules/comment/comment.service.ts +++ b/apps/core/src/modules/comment/comment.service.ts @@ -26,7 +26,7 @@ import { NoContentCanBeModifiedException } from '~/common/exceptions/no-content- import { DatabaseService } from '~/processors/database/database.service' import { EmailService } from '~/processors/helper/helper.email.service' import { InjectModel } from '~/transformers/model.transformer' -import { hasChinese } from '~/utils' +import { getAvatar, hasChinese } from '~/utils' import { ConfigsService } from '../configs/configs.service' import { createMockedContextResponse } from '../serverless/mock-response.util' @@ -273,7 +273,7 @@ export class CommentService implements OnModuleInit { // 过滤脏数据 this.cleanDirtyData(queryList.docs) - await this.replaceMasterAvatarUrl(queryList.docs) + await this.fillAndReplaceAvatarUrl(queryList.docs) return queryList } @@ -437,16 +437,23 @@ export class CommentService implements OnModuleInit { if (location) await this.commentModel.updateOne({ _id: id }, { location }) } - async replaceMasterAvatarUrl(comments: CommentModel[]) { + async fillAndReplaceAvatarUrl(comments: CommentModel[]) { const master = await this.userService.getMaster() comments.forEach(function process(comment) { if (typeof comment == 'string') { return } + // 如果是 author 是站长,就用站长自己设定的头像替换 if (comment.author === master.name) { comment.avatar = master.avatar || comment.avatar } + + // 如果不存在头像就 + if (!comment.avatar) { + comment.avatar = getAvatar(comment.mail) + } + if (comment.children?.length) { comment.children.forEach((child) => { process(child as CommentModel)