From 454d5b074ff11bdea46de8093bd0feee1ec1b63c Mon Sep 17 00:00:00 2001 From: Innei Date: Sat, 4 Jun 2022 22:17:05 +0800 Subject: [PATCH] feat: add comment option for ip record --- src/modules/comment/comment.controller.ts | 42 +++++++++-------------- src/modules/comment/comment.service.ts | 31 +++++++++++++++++ 2 files changed, 48 insertions(+), 25 deletions(-) diff --git a/src/modules/comment/comment.controller.ts b/src/modules/comment/comment.controller.ts index 26fcedbc..cf911954 100644 --- a/src/modules/comment/comment.controller.ts +++ b/src/modules/comment/comment.controller.ts @@ -29,7 +29,6 @@ import { MongoIdDto } from '~/shared/dto/id.dto' import { PagerDto } from '~/shared/dto/pager.dto' import { transformDataToPaginate } from '~/transformers/paginate.transformer' -import { ToolService } from '../tool/tool.service' import { UserModel } from '../user/user.model' import { CommentDto, @@ -50,7 +49,6 @@ export class CommentController { constructor( private readonly commentService: CommentService, private readonly eventManager: EventManagerService, - private readonly toolService: ToolService, ) {} @Get('/') @@ -133,21 +131,11 @@ export class CommentController { throw new ForbiddenException('主人禁止了评论') } - const model: Partial = { ...body, ...ipLocation } - - if (ipLocation.ip) { - model.location = await this.toolService - .getIp(ipLocation.ip) - .then( - (res) => - `${ - res.regionName && res.regionName !== res.cityName - ? `${res.regionName}` - : '' - }${res.cityName ? `${res.cityName}` : ''}` || undefined, - ) - .catch(() => undefined) - } + const model: Partial = + await this.commentService.attachIpLocation( + { ...body, ...ipLocation }, + isMaster ? '' : ipLocation.ip, + ) const comment = await this.commentService.createComment(id, model, ref) @@ -202,14 +190,18 @@ export class CommentController { const commentIndex = parent.commentsIndex const key = `${parent.key}#${commentIndex}` - const model: Partial = { - parent, - ref: (parent.ref as DocumentType)._id, - refType: parent.refType, - ...body, - ...ipLocation, - key, - } + const model: Partial = + await this.commentService.attachIpLocation( + { + parent, + ref: (parent.ref as DocumentType)._id, + refType: parent.refType, + ...body, + ...ipLocation, + key, + }, + isMaster ? '' : ipLocation.ip, + ) const comment = await this.commentService.model.create(model) diff --git a/src/modules/comment/comment.service.ts b/src/modules/comment/comment.service.ts index 275cc97b..45120f49 100644 --- a/src/modules/comment/comment.service.ts +++ b/src/modules/comment/comment.service.ts @@ -20,6 +20,7 @@ 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' import { CommentModel, CommentRefTypes } from './comment.model' @@ -35,6 +36,9 @@ export class CommentService { private readonly configs: ConfigsService, private readonly userService: UserService, private readonly mailService: EmailService, + + private readonly toolService: ToolService, + private readonly configsService: ConfigsService, ) {} public get model() { @@ -277,4 +281,31 @@ export class CommentService { } } } + + async attachIpLocation(model: Partial, ip: string) { + if (!ip) { + return model + } + const { recordIpLocation, fetchLocationTimeout = 3000 } = + await this.configsService.get('commentOptions') + + if (!recordIpLocation) { + return model + } + const newModel = { ...model } + + newModel.location = await this.toolService + .getIp(ip, fetchLocationTimeout) + .then( + (res) => + `${ + res.regionName && res.regionName !== res.cityName + ? `${res.regionName}` + : '' + }${res.cityName ? `${res.cityName}` : ''}` || undefined, + ) + .catch(() => undefined) + + return newModel + } }