feat: comment service init

This commit is contained in:
Innei
2021-09-03 17:03:21 +08:00
parent deca66e471
commit 4fcb0dfb5d
6 changed files with 86 additions and 11 deletions

View File

@@ -1,9 +1,11 @@
import { Module } from '@nestjs/common'
import { UserModule } from '../user/user.module'
import { CommentService } from './comment.service'
@Module({
controllers: [],
providers: [CommentService],
exports: [CommentService],
imports: [UserModule],
})
export class CommentModule {}

View File

@@ -1,13 +1,17 @@
import { Injectable } from '@nestjs/common'
import { Injectable, Logger } from '@nestjs/common'
import { ReturnModelType } from '@typegoose/typegoose'
import { InjectModel } from 'nestjs-typegoose'
import { hasChinese } from '~/utils/index.util'
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 { CommentModel } from './comment.model'
import { UserService } from '../user/user.service'
import BlockedKeywords from './block-keywords.json'
import { CommentModel, CommentRefTypes } from './comment.model'
@Injectable()
export class CommentService {
private readonly logger: Logger = new Logger(CommentService.name)
constructor(
@InjectModel(CommentModel)
private readonly commentModel: MongooseModel<CommentModel>,
@@ -19,5 +23,62 @@ export class CommentService {
@InjectModel(PageModel)
private readonly pageModel: ReturnModelType<typeof PageModel>,
private readonly configs: ConfigsService,
private readonly userService: UserService,
) {}
private getModelByRefType(type: CommentRefTypes) {
const map = new Map(
Object.entries({
Post: this.postModel,
Note: this.noteModel,
Page: this.pageModel,
}),
)
return map.get(type) as any as ReturnModelType<
typeof NoteModel | typeof PostModel | typeof PageModel
>
}
async checkSpam(doc: Partial<CommentModel>) {
const res = await (async () => {
const commentOptions = this.configs.get('commentOptions')
if (!commentOptions.antiSpam) {
return false
}
const master = await this.userService.getMaster()
if (doc.author === master.username) {
return false
}
if (commentOptions.blockIps) {
const isBlock = commentOptions.blockIps.some((ip) =>
new RegExp(ip, 'ig').test(doc.ip),
)
if (isBlock) {
return true
}
}
const customKeywords = commentOptions.spamKeywords || []
const isBlock = [...customKeywords, ...BlockedKeywords].some((keyword) =>
new RegExp(keyword, 'ig').test(doc.text),
)
if (isBlock) {
return true
}
if (commentOptions.disableNoChinese && !hasChinese(doc.text)) {
return true
}
return false
})()
if (res) {
this.logger.warn(
'--> 检测到一条垃圾评论: ' +
`作者: ${doc.author}, IP: ${doc.ip}, 内容为: ${doc.text}`,
)
}
return res
}
}

View File

@@ -1,12 +1,3 @@
/*
* @Author: Innei
* @Date: 2020-05-08 17:02:08
* @LastEditTime: 2020-09-09 13:36:59
* @LastEditors: Innei
* @FilePath: /mx-server/src/common/global/configs/configs.module.ts
* @Copyright
*/
import { Global, Module } from '@nestjs/common'
import { ConfigsService } from './configs.service'

View File

@@ -55,6 +55,10 @@ export class UserService {
async hasMaster() {
return !!(await this.userModel.countDocuments())
}
getMaster() {
return this.userModel.findOne().lean()
}
async createMaster(
model: Pick<UserModel, 'username' | 'name' | 'password'> &
Partial<Pick<UserModel, 'introduce' | 'avatar' | 'url'>>,