feat: add comment option for ip record

This commit is contained in:
Innei
2022-06-04 22:17:05 +08:00
parent 92500edd6e
commit 454d5b074f
2 changed files with 48 additions and 25 deletions

View File

@@ -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<CommentModel> = { ...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<CommentModel> =
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<CommentModel> = {
parent,
ref: (parent.ref as DocumentType<any>)._id,
refType: parent.refType,
...body,
...ipLocation,
key,
}
const model: Partial<CommentModel> =
await this.commentService.attachIpLocation(
{
parent,
ref: (parent.ref as DocumentType<any>)._id,
refType: parent.refType,
...body,
...ipLocation,
key,
},
isMaster ? '' : ipLocation.ip,
)
const comment = await this.commentService.model.create(model)

View File

@@ -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<CommentModel>, 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
}
}