-
-
Notifications
You must be signed in to change notification settings - Fork 304
/
Copy pathhelper.service.akismet.ts
executable file
·117 lines (104 loc) · 3.62 KB
/
helper.service.akismet.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/**
* @file Akismet service
* @module processor/helper/akismet.service
* @author Surmon <https://github.com/surmon-china>
*/
import { AkismetClient } from 'akismet-api'
import { Injectable } from '@nestjs/common'
import { UNDEFINED } from '@app/constants/value.constant'
import { getMessageFromNormalError } from '@app/transformers/error.transformer'
import { createLogger } from '@app/utils/logger'
import { isDevEnv } from '@app/app.environment'
import * as APP_CONFIG from '@app/app.config'
const logger = createLogger({ scope: 'AkismetService', time: isDevEnv })
// keyof typeof AkismetClient
export enum AkismetAction {
CheckSpam = 'checkSpam',
SubmitSpam = 'submitSpam',
SubmitHam = 'submitHam'
}
// https://github.com/chrisfosterelli/akismet-api/blob/master/docs/comments.md
export interface AkismetPayload {
user_ip: string
user_agent: string
referrer: string
permalink?: string | null
comment_type?: 'comment' | 'reply'
comment_author?: string | null
comment_author_email?: string | null
comment_author_url?: string | null
comment_content?: string | null
}
@Injectable()
export class AkismetService {
private client: AkismetClient
private clientIsValid = false
constructor() {
this.initClient()
this.initVerify()
}
private initClient(): void {
// https://github.com/chrisfosterelli/akismet-api
this.client = new AkismetClient({
key: APP_CONFIG.AKISMET.key as string,
blog: APP_CONFIG.AKISMET.blog as string
})
}
private initVerify(): void {
this.client
.verifyKey()
.then((valid) => (valid ? Promise.resolve(valid) : Promise.reject('Invalid Akismet key')))
.then(() => {
this.clientIsValid = true
logger.success('client initialized.')
})
.catch((error) => {
this.clientIsValid = false
logger.failure('client initialization failed!', '|', getMessageFromNormalError(error))
})
}
private makeInterceptor(handleType: AkismetAction) {
return (content: AkismetPayload): Promise<any> => {
return new Promise((resolve, reject) => {
// continue operation only when initialization successful
if (!this.clientIsValid) {
const message = `${handleType} failed! reason: init failed`
logger.warn(message)
return resolve(message)
}
logger.log(`${handleType}...`, new Date())
this.client[handleType]({
...content,
permalink: content.permalink || UNDEFINED,
comment_author: content.comment_author || UNDEFINED,
comment_author_email: content.comment_author_email || UNDEFINED,
comment_author_url: content.comment_author_url || UNDEFINED,
comment_content: content.comment_content || UNDEFINED
})
.then((result) => {
if (handleType === AkismetAction.CheckSpam && result) {
logger.info(`${handleType} found SPAM!`, new Date(), content)
reject('SPAM!')
} else {
logger.info(`${handleType} succeeded.`)
resolve(result)
}
})
.catch((error) => {
const message = `${handleType} failed!`
logger.warn(message, error)
reject(message)
})
})
}
}
public checkSpam(payload: AkismetPayload): Promise<any> {
return this.makeInterceptor(AkismetAction.CheckSpam)(payload)
}
public submitSpam(payload: AkismetPayload): Promise<any> {
return this.makeInterceptor(AkismetAction.SubmitSpam)(payload)
}
public submitHam(payload: AkismetPayload): Promise<any> {
return this.makeInterceptor(AkismetAction.SubmitHam)(payload)
}
}