Skip to content

Commit 18d4144

Browse files
committed
feat: get block list from database
1 parent 4cbcb1a commit 18d4144

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed

packages/velog-server/src/env.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { existsSync } from 'fs'
33
import { z } from 'zod'
44
import { fileURLToPath } from 'url'
55
import { dirname, join } from 'path'
6+
import { container } from 'tsyringe'
7+
import { DbService } from '@lib/db/DbService.js'
68

79
type DockerEnv = 'development' | 'stage' | 'production'
810
type AppEnvironment = 'development' | 'production'
@@ -88,6 +90,8 @@ const env = z.object({
8890
graphcdnToken: z.string(),
8991
})
9092

93+
const { bannedKeywords, bannedAltKeywords } = await readEnvFromDatabase()
94+
9195
export const ENV = env.parse({
9296
dockerEnv,
9397
appEnv,
@@ -127,7 +131,17 @@ export const ENV = env.parse({
127131
discordErrorChannel: process.env.DISCORD_ERROR_CHANNEL,
128132
discordSpamChannel: process.env.DISCORD_SPAM_CHANNEL,
129133
turnstileSecretKey: process.env.TURNSTILE_SECRET_KEY,
130-
bannedKeywords: process.env.BANNED_KEYWORDS?.split(','),
131-
bannedAltKeywords: process.env.BANNED_ALT_KEYWORDS?.split(','),
134+
bannedKeywords: bannedKeywords,
135+
bannedAltKeywords: bannedAltKeywords,
132136
graphcdnToken: process.env.GRAPHCDN_TOKEN,
133137
})
138+
139+
async function readEnvFromDatabase() {
140+
const db = container.resolve(DbService)
141+
const items = await db.dynamicConfigItem.findMany()
142+
143+
return {
144+
bannedKeywords: items.filter((item) => item.type === 'banned').map((item) => item.value),
145+
bannedAltKeywords: items.filter((item) => item.type === 'bannedAlt').map((item) => item.value),
146+
}
147+
}
Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
1-
import { RedisService } from '@lib/redis/RedisService.js'
1+
import { DbService } from '@lib/db/DbService.js'
22
import { injectable, singleton } from 'tsyringe'
33

44
interface Service {
5-
checkBlockedUser(username: string): Promise<boolean>
5+
isBlockedUser(username: string): Promise<boolean>
66
}
77

88
@injectable()
99
@singleton()
1010
export class DynamicConfigService implements Service {
11-
constructor(private readonly redis: RedisService) {}
12-
public async checkBlockedUser(username: string = ''): Promise<boolean> {
11+
constructor(private readonly db: DbService) {}
12+
public async isBlockedUser(username: string = ''): Promise<boolean> {
1313
const list = await this.readBlockUserList()
1414
const isBlocked = list.includes(username)
1515
return isBlocked
1616
}
1717
private async readBlockUserList(): Promise<string[]> {
18-
const keyname = this.redis.setName.blockList
19-
const list = await this.redis.smembers(keyname)
20-
return list
18+
const list = await this.db.dynamicConfigItem.findMany({
19+
where: {
20+
type: 'username',
21+
},
22+
})
23+
return list.map((item) => item.value)
2124
}
2225
}

packages/velog-server/src/services/PostApiService/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ export class PostApiService implements Service {
168168
const country = geoip.lookup(ip)?.country ?? ''
169169
const isSpam = this.isIncludeSpamKeyword({ input, user, country })
170170
const isLimit = await this.isPostLimitReached(signedUserId)
171-
const isBlock = await this.dynamicConfigService.checkBlockedUser(user.username)
171+
const isBlock = await this.dynamicConfigService.isBlockedUser(user.username)
172172

173173
const checks = [
174174
{ type: 'spam', value: isSpam },

0 commit comments

Comments
 (0)