import { createClient, RedisClient } from 'redis'
import { logger } from '../helpers/logger'
import { generateRandomString } from '../helpers/utils'
-import { CONFIG, USER_PASSWORD_RESET_LIFETIME, USER_EMAIL_VERIFY_LIFETIME, VIDEO_VIEW_LIFETIME } from '../initializers'
+import {
+ CONTACT_FORM_LIFETIME,
+ USER_EMAIL_VERIFY_LIFETIME,
+ USER_PASSWORD_RESET_LIFETIME,
+ VIDEO_VIEW_LIFETIME,
+ WEBSERVER
+} from '../initializers/constants'
+import { CONFIG } from '../initializers/config'
type CachedRoute = {
body: string,
if (this.initialized === true) return
this.initialized = true
- this.client = createClient(Redis.getRedisClient())
+ this.client = createClient(Redis.getRedisClientOptions())
this.client.on('error', err => {
logger.error('Error in Redis client.', { err })
this.client.auth(CONFIG.REDIS.AUTH)
}
- this.prefix = 'redis-' + CONFIG.WEBSERVER.HOST + '-'
+ this.prefix = 'redis-' + WEBSERVER.HOST + '-'
}
- static getRedisClient () {
+ static getRedisClientOptions () {
return Object.assign({},
(CONFIG.REDIS.AUTH && CONFIG.REDIS.AUTH != null) ? { password: CONFIG.REDIS.AUTH } : {},
(CONFIG.REDIS.DB) ? { db: CONFIG.REDIS.DB } : {},
)
}
+ getClient () {
+ return this.client
+ }
+
+ getPrefix () {
+ return this.prefix
+ }
+
/************* Forgot password *************/
async setResetPasswordVerificationString (userId: number) {
return this.getValue(this.generateVerifyEmailKey(userId))
}
+ /************* Contact form per IP *************/
+
+ async setContactFormIp (ip: string) {
+ return this.setValue(this.generateContactFormKey(ip), '1', CONTACT_FORM_LIFETIME)
+ }
+
+ async doesContactFormIpExist (ip: string) {
+ return this.exists(this.generateContactFormKey(ip))
+ }
+
/************* Views per IP *************/
setIPVideoView (ip: string, videoUUID: string) {
return this.setValue(this.generateViewKey(ip, videoUUID), '1', VIDEO_VIEW_LIFETIME)
}
- async isVideoIPViewExists (ip: string, videoUUID: string) {
+ async doesVideoIPViewExist (ip: string, videoUUID: string) {
return this.exists(this.generateViewKey(ip, videoUUID))
}
}
private generateViewKey (ip: string, videoUUID: string) {
- return videoUUID + '-' + ip
+ return `views-${videoUUID}-${ip}`
+ }
+
+ private generateContactFormKey (ip: string) {
+ return 'contact-form-' + ip
}
/************* Redis helpers *************/