feat: comment modal add avatar and source
Signed-off-by: Innei <i@innei.in>
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user