Skip to content

Commit 1fb6298

Browse files
committed
refactor: spam filter test runner
1 parent 59059a9 commit 1fb6298

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

packages/velog-cron/src/routes/posts/v1/PostController.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -66,49 +66,50 @@ export class PostController implements Controller {
6666
return posts.length
6767
}
6868
async spamFilterTestRunner() {
69+
if (ENV.appEnv !== 'development') return
70+
6971
const utils = container.resolve(UtilsService)
7072
const postService = container.resolve(PostService)
7173
try {
72-
if (ENV.appEnv !== 'development') return
73-
7474
const filePath = path.resolve(utils.resolveDir('./src/routes/posts/v1/spam_post.json'))
7575

7676
const fileExits = fs.existsSync(filePath)
77-
if (!fileExits) return
77+
if (!fileExits) {
78+
throw new NotFoundError('Not found spam_post.json')
79+
}
7880

7981
const readFileResult = fs.readFileSync(filePath, { encoding: 'utf-8' })
8082
const data = JSON.parse(readFileResult)
8183
const key = Object.keys(data)[0]
8284
const posts: PostData[] = data[key]
83-
.filter((v: any) => !!v.title)
84-
.map((v: any, index: number) => ({ id: index, ...v }))
85+
.filter((post: any) => post.title)
86+
.map((post: any, index: number) => ({ id: index, ...post }))
8587

86-
const postLength = 5000
88+
const maxPostLength = 5000
8789
const set = new Set()
88-
8990
const bannedUesrnames: string[] = []
9091

91-
for (const post of posts.slice(0, postLength)) {
92+
for (const post of posts.slice(0, maxPostLength)) {
9293
const { id, title, body, username } = post
9394
if (bannedUesrnames.includes(username)) {
9495
set.add(id)
9596
}
9697

97-
const isSpam = await postService.checkIsSpam(title, body, username, '', 'US')
98-
if (isSpam) {
98+
const isForeignSpam = await postService.checkIsSpam(title, body, username, '', 'US')
99+
if (isForeignSpam) {
99100
set.add(id)
100101
continue
101102
}
102103

103-
const isSpam2 = await postService.checkIsSpam(title, body, username, '', 'KR')
104-
if (isSpam2) {
104+
const isInternalSpam = await postService.checkIsSpam(title, body, username, '', 'KR')
105+
if (isInternalSpam) {
105106
set.add(id)
106107
}
107108
}
108109

109110
const isSpamCount = set.size
110111
console.log('isSpamCount: ', isSpamCount)
111-
console.log('ratio: ', isSpamCount / postLength)
112+
console.log('ratio: ', isSpamCount / maxPostLength)
112113

113114
const allowIds: number[] = []
114115
for (const id of allowIds) {

0 commit comments

Comments
 (0)