fix: hide email public for guest

This commit is contained in:
Innei
2021-09-25 11:36:54 +08:00
parent fbd6fd9d48
commit e319014fb6
5 changed files with 69 additions and 12 deletions

View File

@@ -11,6 +11,7 @@ import { AnalyzeModel } from '~/modules/analyze/analyze.model'
import { OptionModel } from '~/modules/configs/configs.model'
import { CacheService } from '~/processors/cache/cache.service'
import { CronService } from '~/processors/helper/helper.cron.service'
import { TaskQueueService } from '~/processors/helper/helper.tq.service'
import { getIp } from '~/utils/ip.util'
import { getRedisKey } from '~/utils/redis.util'
@@ -25,6 +26,7 @@ export class AnalyzeMiddleware implements NestMiddleware {
private readonly options: ReturnModelType<typeof OptionModel>,
private readonly cronService: CronService,
private readonly cacheService: CacheService,
private readonly taskService: TaskQueueService,
) {
this.init()
}
@@ -32,9 +34,9 @@ export class AnalyzeMiddleware implements NestMiddleware {
init() {
this.parser = new UAParser()
this.botListData = this.getLocalBotList()
this.cronService.updateBotList().then((res) => {
this.botListData = this.pickPattern2Regexp(res)
})
this.taskService.add(this.cronService.updateBotList.name, async () =>
this.cronService.updateBotList(),
)
}
getLocalBotList() {

View File

@@ -10,11 +10,12 @@ import {
Post,
Query,
Req,
UseInterceptors,
} from '@nestjs/common'
import { ApiOperation, ApiParam } from '@nestjs/swagger'
import { DocumentType } from '@typegoose/typegoose'
import { Auth } from '~/common/decorator/auth.decorator'
import { HTTPDecorators, Paginator } from '~/common/decorator/http.decorator'
import { CurrentUser } from '~/common/decorator/current-user.decorator'
import { IpLocation, IpRecord } from '~/common/decorator/ip.decorator'
import { ApiName } from '~/common/decorator/openapi.decorator'
import { IsMaster } from '~/common/decorator/role.decorator'
@@ -24,15 +25,20 @@ import { SharedGateway } from '~/processors/gateway/shared/events.gateway'
import { ReplyMailType } from '~/processors/helper/helper.email.service'
import { MongoIdDto } from '~/shared/dto/id.dto'
import { PagerDto } from '~/shared/dto/pager.dto'
import { transformDataToPaginate } from '~/utils/transfrom.util'
import { UserModel } from '../user/user.model'
import {
CommentDto,
CommentRefTypesDto,
StateDto,
TextOnlyDto,
} from './comment.dto'
import { CommentFilterEmailInterceptor } from './comment.interceptor'
import { CommentModel, CommentState } from './comment.model'
import { CommentService } from './comment.service'
@Controller({ path: 'comments' })
@UseInterceptors(CommentFilterEmailInterceptor)
@ApiName
export class CommentController {
constructor(
@@ -42,10 +48,11 @@ export class CommentController {
@Get('/')
@Auth()
@Paginator
async getRecentlyComments(@Query() query: PagerDto) {
const { size = 10, page = 1, state = 0 } = query
return await this.commentService.getComments({ size, page, state })
return transformDataToPaginate(
await this.commentService.getComments({ size, page, state }),
)
}
@Get('/:id')
@@ -64,7 +71,6 @@ export class CommentController {
}
@Get('/ref/:id')
@HTTPDecorators.Paginator
@ApiParam({
name: 'id',
description: 'refId',
@@ -94,7 +100,7 @@ export class CommentController {
sort: { created: -1 },
},
)
return comments
return transformDataToPaginate(comments)
}
@Post('/:id')
@@ -196,13 +202,13 @@ export class CommentController {
@ApiOperation({ summary: '主人专用评论接口 需要登录' })
@Auth()
async commentByMaster(
@Req() req: any,
@CurrentUser() user: UserModel,
@Param() params: MongoIdDto,
@Body() body: TextOnlyDto,
@IpLocation() ipLocation: IpRecord,
@Query() query: CommentRefTypesDto,
) {
const { name, mail, url } = req.user
const { name, mail, url } = user
const model: CommentDto = {
author: name,
...body,

View File

@@ -0,0 +1,49 @@
import {
CallHandler,
ExecutionContext,
Injectable,
NestInterceptor,
} from '@nestjs/common'
import { isDefined } from 'class-validator'
import { isArrayLike, isObjectLike } from 'lodash'
import { map } from 'rxjs'
import { getNestExecutionContextRequest } from '~/utils/nest.util'
@Injectable()
export class CommentFilterEmailInterceptor implements NestInterceptor {
intercept(context: ExecutionContext, next: CallHandler) {
const request = this.getRequest(context)
// 如果已经登陆
const isMaster = request.user
if (isMaster) {
return next.handle()
}
return next.handle().pipe(
map(function handle(data: any) {
if (!data) {
return data
}
if (isArrayLike(data?.data)) {
;(data?.data).forEach((item: any, i) => {
if (isDefined(item.mail)) {
data.data[i].mail = '*'
}
if (item.children) {
handle({ data: item.children })
}
})
}
if (isObjectLike(data)) {
data = data?.toJSON?.() || data
Reflect.deleteProperty(data, 'mail')
}
return data
}),
)
}
get getRequest() {
return getNestExecutionContextRequest.bind(this)
}
}

View File

@@ -55,7 +55,7 @@ export class CommentModel extends BaseModel {
@prop({ trim: true, required: true })
author!: string
@prop({ trim: true, select: false })
@prop({ trim: true })
mail?: string
@prop({ trim: true })

View File

@@ -177,7 +177,7 @@ export class CommentService {
const queryList = await this.commentModel.paginate(
{ state },
{
select: '+ip +agent +mail -children',
select: '+ip +agent -children',
page,
limit: size,
populate: [