fix(link): hide email if not master
Signed-off-by: Innei <tukon479@gmail.com>
This commit is contained in:
@@ -8,9 +8,10 @@ import { RolesGuard } from './common/guard/roles.guard'
|
||||
import { AnalyzeInterceptor } from './common/interceptors/analyze.interceptor'
|
||||
import { HttpCacheInterceptor } from './common/interceptors/cache.interceptor'
|
||||
import { CountingInterceptor } from './common/interceptors/counting.interceptor'
|
||||
import { DbQueryInterceptor } from './common/interceptors/db-query.interceptor'
|
||||
import { IdempotenceInterceptor } from './common/interceptors/idempotence.interceptor'
|
||||
import { JSONTransformInterceptor } from './common/interceptors/json-transform.interceptor'
|
||||
import { QueryInterceptor } from './common/interceptors/query.interceptor'
|
||||
import { ResponseFilterInterceptor } from './common/interceptors/response-filter.interceptor'
|
||||
import { ResponseInterceptor } from './common/interceptors/response.interceptor'
|
||||
import { AggregateModule } from './modules/aggregate/aggregate.module'
|
||||
import { AnalyzeModule } from './modules/analyze/analyze.module'
|
||||
@@ -102,12 +103,12 @@ import { RedisModule } from './processors/redis/redis.module'
|
||||
providers: [
|
||||
{
|
||||
provide: APP_INTERCEPTOR,
|
||||
useClass: QueryInterceptor,
|
||||
useClass: DbQueryInterceptor,
|
||||
},
|
||||
|
||||
{
|
||||
provide: APP_INTERCEPTOR,
|
||||
useClass: HttpCacheInterceptor, // 4
|
||||
useClass: HttpCacheInterceptor, // 5
|
||||
},
|
||||
{
|
||||
provide: APP_INTERCEPTOR,
|
||||
@@ -115,11 +116,15 @@ import { RedisModule } from './processors/redis/redis.module'
|
||||
},
|
||||
{
|
||||
provide: APP_INTERCEPTOR,
|
||||
useClass: CountingInterceptor, // 3
|
||||
useClass: CountingInterceptor, // 4
|
||||
},
|
||||
{
|
||||
provide: APP_INTERCEPTOR,
|
||||
useClass: JSONTransformInterceptor, // 2
|
||||
useClass: JSONTransformInterceptor, // 3
|
||||
},
|
||||
{
|
||||
provide: APP_INTERCEPTOR,
|
||||
useClass: ResponseFilterInterceptor, // 2
|
||||
},
|
||||
{
|
||||
provide: APP_INTERCEPTOR,
|
||||
|
||||
13
src/common/decorator/response-filter.decorator.ts
Normal file
13
src/common/decorator/response-filter.decorator.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { FastifyRequest } from 'fastify'
|
||||
|
||||
import { SetMetadata } from '@nestjs/common'
|
||||
|
||||
import { HTTP_RESPONSE_FILTER } from '~/constants/meta.constant'
|
||||
|
||||
export const ResponseFilter: (
|
||||
fn: (data: any, request: FastifyRequest & KV, handler: Function) => any,
|
||||
) => MethodDecorator = (type) => {
|
||||
return (_, __, descriptor: any) => {
|
||||
SetMetadata(HTTP_RESPONSE_FILTER, type)(descriptor.value)
|
||||
}
|
||||
}
|
||||
@@ -12,7 +12,7 @@ import { getNestExecutionContextRequest } from '~/transformers/get-req.transform
|
||||
|
||||
/** 此拦截器用于转换 req.query.query -> js object,用于直接数据库查询,需要鉴权 */
|
||||
@Injectable()
|
||||
export class QueryInterceptor implements NestInterceptor {
|
||||
export class DbQueryInterceptor implements NestInterceptor {
|
||||
intercept(
|
||||
context: ExecutionContext,
|
||||
next: CallHandler<any>,
|
||||
47
src/common/interceptors/response-filter.interceptor.ts
Normal file
47
src/common/interceptors/response-filter.interceptor.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
/**
|
||||
* 对响应体进行数据处理
|
||||
* @author Innei
|
||||
*/
|
||||
import { isFunction } from 'lodash'
|
||||
import { Observable, map } from 'rxjs'
|
||||
|
||||
import {
|
||||
CallHandler,
|
||||
ExecutionContext,
|
||||
Injectable,
|
||||
NestInterceptor,
|
||||
} from '@nestjs/common'
|
||||
import { Reflector } from '@nestjs/core'
|
||||
|
||||
import { HTTP_RESPONSE_FILTER } from '~/constants/meta.constant'
|
||||
|
||||
@Injectable()
|
||||
export class ResponseFilterInterceptor implements NestInterceptor {
|
||||
constructor(private readonly reflector: Reflector) {}
|
||||
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
|
||||
const handler = context.getHandler()
|
||||
|
||||
// 请求数据过滤函数
|
||||
const responseFilterFn = this.reflector.getAllAndOverride<boolean>(
|
||||
HTTP_RESPONSE_FILTER,
|
||||
[handler],
|
||||
)
|
||||
|
||||
if (!isFunction(responseFilterFn)) {
|
||||
return next.handle()
|
||||
}
|
||||
|
||||
const http = context.switchToHttp()
|
||||
const request = http.getRequest()
|
||||
|
||||
if (!http.getRequest()) {
|
||||
return next.handle()
|
||||
}
|
||||
|
||||
return next.handle().pipe(
|
||||
map((data) => {
|
||||
return responseFilterFn(data, request, handler)
|
||||
}),
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -12,3 +12,5 @@ export const CRON_DESCRIPTION = '__cron:description__'
|
||||
|
||||
export const HTTP_IDEMPOTENCE_OPTIONS = '__idempotence_options__'
|
||||
export const HTTP_IDEMPOTENCE_KEY = '__idempotence_key__'
|
||||
|
||||
export const HTTP_RESPONSE_FILTER = '__response_filter__'
|
||||
|
||||
@@ -26,6 +26,7 @@ import { LinkModel, LinkState } from './link.model'
|
||||
import { LinkService } from './link.service'
|
||||
|
||||
const paths = ['links', 'friends']
|
||||
|
||||
@ApiController(paths)
|
||||
@ApiName
|
||||
export class LinkControllerCrud extends BaseCrudFactory({
|
||||
@@ -49,14 +50,22 @@ export class LinkControllerCrud extends BaseCrudFactory({
|
||||
}
|
||||
|
||||
@Get('/all')
|
||||
async getAll(this: BaseCrudModuleType<LinkModel>) {
|
||||
async getAll(
|
||||
this: BaseCrudModuleType<LinkModel>,
|
||||
@IsMaster() isMaster: boolean,
|
||||
) {
|
||||
// 过滤未通过审核的
|
||||
const condition: mongoose.FilterQuery<LinkModel> = {
|
||||
state: LinkState.Pass,
|
||||
}
|
||||
return await this._model.find(condition).sort({ created: -1 }).lean()
|
||||
return await this._model
|
||||
.find(condition)
|
||||
.sort({ created: -1 })
|
||||
.select(isMaster ? '' : '-email')
|
||||
.lean()
|
||||
}
|
||||
}
|
||||
|
||||
@ApiController(paths)
|
||||
@ApiName
|
||||
export class LinkController {
|
||||
|
||||
Reference in New Issue
Block a user