feat: add comment location expose
This commit is contained in:
@@ -28,6 +28,7 @@ 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,
|
||||
@@ -46,6 +47,7 @@ export class CommentController {
|
||||
constructor(
|
||||
private readonly commentService: CommentService,
|
||||
private readonly eventManager: EventManagerService,
|
||||
private readonly toolService: ToolService,
|
||||
) {}
|
||||
|
||||
@Get('/')
|
||||
@@ -91,15 +93,16 @@ export class CommentController {
|
||||
ref: id,
|
||||
$or: [
|
||||
{
|
||||
state: 0,
|
||||
state: CommentState.Read,
|
||||
},
|
||||
{ state: 1 },
|
||||
{ state: CommentState.Unread },
|
||||
],
|
||||
},
|
||||
{
|
||||
limit: size,
|
||||
page,
|
||||
sort: { created: -1 },
|
||||
lean: true,
|
||||
},
|
||||
)
|
||||
return transformDataToPaginate(comments)
|
||||
@@ -124,7 +127,19 @@ export class CommentController {
|
||||
throw new ForbiddenException('主人禁止了评论')
|
||||
}
|
||||
|
||||
const model = { ...body, ...ipLocation }
|
||||
const model: Partial<CommentModel> = { ...body, ...ipLocation }
|
||||
|
||||
if (ipLocation.ip) {
|
||||
model.location = await this.toolService
|
||||
.getIp(ipLocation.ip)
|
||||
.then(
|
||||
(res) =>
|
||||
`${res.cityName ? `${res.cityName}` : ''}${
|
||||
res.regionName ? `-${res.regionName}` : ''
|
||||
}` || undefined,
|
||||
)
|
||||
.catch(() => undefined)
|
||||
}
|
||||
|
||||
const comment = await this.commentService.createComment(id, model, ref)
|
||||
|
||||
|
||||
@@ -119,6 +119,10 @@ export class CommentModel extends BaseModel {
|
||||
})
|
||||
public page: Ref<PageModel>
|
||||
|
||||
// IP 归属记录值
|
||||
@prop()
|
||||
public location?: string
|
||||
|
||||
public get avatar() {
|
||||
return getAvatar(this.mail)
|
||||
}
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
import { Module } from '@nestjs/common'
|
||||
import { Global, Module } from '@nestjs/common'
|
||||
|
||||
import { ToolController } from './tool.controller'
|
||||
import { ToolService } from './tool.service'
|
||||
|
||||
@Global()
|
||||
@Module({
|
||||
providers: [ToolService],
|
||||
controllers: [ToolController],
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import camelcaseKeys from 'camelcase-keys'
|
||||
import { isIPv4, isIPv6 } from 'net'
|
||||
import { URLSearchParams } from 'url'
|
||||
|
||||
@@ -31,20 +32,23 @@ export class ToolService {
|
||||
const { data } = await this.httpService.axiosRef.get(
|
||||
`https://api.i-meto.com/ip/v1/qqwry/${ip}`,
|
||||
)
|
||||
return data as IP
|
||||
|
||||
return camelcaseKeys(data, { deep: true }) as IP
|
||||
} else {
|
||||
const { data } = (await this.httpService.axiosRef.get(
|
||||
`http://ip-api.com/json/${ip}`,
|
||||
)) as any
|
||||
|
||||
return {
|
||||
const res = {
|
||||
cityName: data.city,
|
||||
countryName: data.country,
|
||||
ip: data.query,
|
||||
ispDomain: data.as,
|
||||
ownerDomain: data.org,
|
||||
regionName: data.region_name,
|
||||
}
|
||||
} as const
|
||||
|
||||
return res
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user