1 import * as express from 'express'
2 import { createClient, RedisClient } from 'redis'
3 import { logger } from '../helpers/logger'
4 import { generateRandomString } from '../helpers/utils'
7 USER_EMAIL_VERIFY_LIFETIME,
8 USER_PASSWORD_RESET_LIFETIME,
11 } from '../initializers/constants'
12 import { CONFIG } from '../initializers/config'
22 private static instance: Redis
23 private initialized = false
24 private client: RedisClient
25 private prefix: string
27 private constructor () {
31 // Already initialized
32 if (this.initialized === true) return
33 this.initialized = true
35 this.client = createClient(Redis.getRedisClientOptions())
37 this.client.on('error', err => {
38 logger.error('Error in Redis client.', { err })
42 if (CONFIG.REDIS.AUTH) {
43 this.client.auth(CONFIG.REDIS.AUTH)
46 this.prefix = 'redis-' + WEBSERVER.HOST + '-'
49 static getRedisClientOptions () {
50 return Object.assign({},
51 (CONFIG.REDIS.AUTH && CONFIG.REDIS.AUTH != null) ? { password: CONFIG.REDIS.AUTH } : {},
52 (CONFIG.REDIS.DB) ? { db: CONFIG.REDIS.DB } : {},
53 (CONFIG.REDIS.HOSTNAME && CONFIG.REDIS.PORT)
54 ? { host: CONFIG.REDIS.HOSTNAME, port: CONFIG.REDIS.PORT }
55 : { path: CONFIG.REDIS.SOCKET }
67 /* ************ Forgot password ************ */
69 async setResetPasswordVerificationString (userId: number) {
70 const generatedString = await generateRandomString(32)
72 await this.setValue(this.generateResetPasswordKey(userId), generatedString, USER_PASSWORD_RESET_LIFETIME)
74 return generatedString
77 async getResetPasswordLink (userId: number) {
78 return this.getValue(this.generateResetPasswordKey(userId))
81 /* ************ Email verification ************ */
83 async setVerifyEmailVerificationString (userId: number) {
84 const generatedString = await generateRandomString(32)
86 await this.setValue(this.generateVerifyEmailKey(userId), generatedString, USER_EMAIL_VERIFY_LIFETIME)
88 return generatedString
91 async getVerifyEmailLink (userId: number) {
92 return this.getValue(this.generateVerifyEmailKey(userId))
95 /* ************ Contact form per IP ************ */
97 async setContactFormIp (ip: string) {
98 return this.setValue(this.generateContactFormKey(ip), '1', CONTACT_FORM_LIFETIME)
101 async doesContactFormIpExist (ip: string) {
102 return this.exists(this.generateContactFormKey(ip))
105 /* ************ Views per IP ************ */
107 setIPVideoView (ip: string, videoUUID: string) {
108 return this.setValue(this.generateViewKey(ip, videoUUID), '1', VIDEO_VIEW_LIFETIME)
111 async doesVideoIPViewExist (ip: string, videoUUID: string) {
112 return this.exists(this.generateViewKey(ip, videoUUID))
115 /* ************ API cache ************ */
117 async getCachedRoute (req: express.Request) {
118 const cached = await this.getObject(this.generateCachedRouteKey(req))
120 return cached as CachedRoute
123 setCachedRoute (req: express.Request, body: any, lifetime: number, contentType?: string, statusCode?: number) {
124 const cached: CachedRoute = Object.assign(
126 { body: body.toString() },
127 (contentType) ? { contentType } : null,
128 (statusCode) ? { statusCode: statusCode.toString() } : null
131 return this.setObject(this.generateCachedRouteKey(req), cached, lifetime)
134 /* ************ Video views ************ */
136 addVideoView (videoId: number) {
137 const keyIncr = this.generateVideoViewKey(videoId)
138 const keySet = this.generateVideosViewKey()
141 this.addToSet(keySet, videoId.toString()),
142 this.increment(keyIncr)
146 async getVideoViews (videoId: number, hour: number) {
147 const key = this.generateVideoViewKey(videoId, hour)
149 const valueString = await this.getValue(key)
150 const valueInt = parseInt(valueString, 10)
152 if (isNaN(valueInt)) {
153 logger.error('Cannot get videos views of video %d in hour %d: views number is NaN (%s).', videoId, hour, valueString)
160 async getVideosIdViewed (hour: number) {
161 const key = this.generateVideosViewKey(hour)
163 const stringIds = await this.getSet(key)
164 return stringIds.map(s => parseInt(s, 10))
167 deleteVideoViews (videoId: number, hour: number) {
168 const keySet = this.generateVideosViewKey(hour)
169 const keyIncr = this.generateVideoViewKey(videoId, hour)
172 this.deleteFromSet(keySet, videoId.toString()),
173 this.deleteKey(keyIncr)
177 /* ************ Keys generation ************ */
179 generateCachedRouteKey (req: express.Request) {
180 return req.method + '-' + req.originalUrl
183 private generateVideosViewKey (hour?: number) {
184 if (!hour) hour = new Date().getHours()
186 return `videos-view-h${hour}`
189 private generateVideoViewKey (videoId: number, hour?: number) {
190 if (!hour) hour = new Date().getHours()
192 return `video-view-${videoId}-h${hour}`
195 private generateResetPasswordKey (userId: number) {
196 return 'reset-password-' + userId
199 private generateVerifyEmailKey (userId: number) {
200 return 'verify-email-' + userId
203 private generateViewKey (ip: string, videoUUID: string) {
204 return `views-${videoUUID}-${ip}`
207 private generateContactFormKey (ip: string) {
208 return 'contact-form-' + ip
211 /* ************ Redis helpers ************ */
213 private getValue (key: string) {
214 return new Promise<string>((res, rej) => {
215 this.client.get(this.prefix + key, (err, value) => {
216 if (err) return rej(err)
223 private getSet (key: string) {
224 return new Promise<string[]>((res, rej) => {
225 this.client.smembers(this.prefix + key, (err, value) => {
226 if (err) return rej(err)
233 private addToSet (key: string, value: string) {
234 return new Promise<string[]>((res, rej) => {
235 this.client.sadd(this.prefix + key, value, err => err ? rej(err) : res())
239 private deleteFromSet (key: string, value: string) {
240 return new Promise<void>((res, rej) => {
241 this.client.srem(this.prefix + key, value, err => err ? rej(err) : res())
245 private deleteKey (key: string) {
246 return new Promise<void>((res, rej) => {
247 this.client.del(this.prefix + key, err => err ? rej(err) : res())
251 private deleteFieldInHash (key: string, field: string) {
252 return new Promise<void>((res, rej) => {
253 this.client.hdel(this.prefix + key, field, err => err ? rej(err) : res())
257 private setValue (key: string, value: string, expirationMilliseconds: number) {
258 return new Promise<void>((res, rej) => {
259 this.client.set(this.prefix + key, value, 'PX', expirationMilliseconds, (err, ok) => {
260 if (err) return rej(err)
262 if (ok !== 'OK') return rej(new Error('Redis set result is not OK.'))
269 private setObject (key: string, obj: { [id: string]: string }, expirationMilliseconds: number) {
270 return new Promise<void>((res, rej) => {
271 this.client.hmset(this.prefix + key, obj, (err, ok) => {
272 if (err) return rej(err)
273 if (!ok) return rej(new Error('Redis mset result is not OK.'))
275 this.client.pexpire(this.prefix + key, expirationMilliseconds, (err, ok) => {
276 if (err) return rej(err)
277 if (!ok) return rej(new Error('Redis expiration result is not OK.'))
285 private getObject (key: string) {
286 return new Promise<{ [id: string]: string }>((res, rej) => {
287 this.client.hgetall(this.prefix + key, (err, value) => {
288 if (err) return rej(err)
295 private setValueInHash (key: string, field: string, value: string) {
296 return new Promise<void>((res, rej) => {
297 this.client.hset(this.prefix + key, field, value, (err) => {
298 if (err) return rej(err)
305 private increment (key: string) {
306 return new Promise<number>((res, rej) => {
307 this.client.incr(this.prefix + key, (err, value) => {
308 if (err) return rej(err)
315 private exists (key: string) {
316 return new Promise<boolean>((res, rej) => {
317 this.client.exists(this.prefix + key, (err, existsNumber) => {
318 if (err) return rej(err)
320 return res(existsNumber === 1)
325 static get Instance () {
326 return this.instance || (this.instance = new this())
330 // ---------------------------------------------------------------------------