X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Flib%2Fredis.ts;h=f77d0b62c20ba22d3a92c01da5e097b7b35cf8c1;hb=b6a1dd4d1b3b0032f8b968e72cbd074f646e8827;hp=abd75d5122d7162276fb245f7c2191ab952ad19b;hpb=6d8c8ea73a774c3568e6d28a4cbebcf7979d5c2a;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/lib/redis.ts b/server/lib/redis.ts index abd75d512..f77d0b62c 100644 --- a/server/lib/redis.ts +++ b/server/lib/redis.ts @@ -2,7 +2,14 @@ import * as express from 'express' 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, @@ -24,7 +31,7 @@ class Redis { 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 }) @@ -35,10 +42,10 @@ class Redis { 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 } : {}, @@ -48,6 +55,14 @@ class Redis { ) } + getClient () { + return this.client + } + + getPrefix () { + return this.prefix + } + /************* Forgot password *************/ async setResetPasswordVerificationString (userId: number) { @@ -76,13 +91,23 @@ class Redis { 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)) } @@ -121,7 +146,14 @@ class Redis { const key = this.generateVideoViewKey(videoId, hour) const valueString = await this.getValue(key) - return parseInt(valueString, 10) + const valueInt = parseInt(valueString, 10) + + if (isNaN(valueInt)) { + logger.error('Cannot get videos views of video %d in hour %d: views number is NaN (%s).', videoId, hour, valueString) + return undefined + } + + return valueInt } async getVideosIdViewed (hour: number) { @@ -168,7 +200,11 @@ class Redis { } private generateViewKey (ip: string, videoUUID: string) { - return videoUUID + '-' + ip + return `views-${videoUUID}-${ip}` + } + + private generateContactFormKey (ip: string) { + return 'contact-form-' + ip } /************* Redis helpers *************/