Add player mode in watch/embed urls
[oweals/peertube.git] / server / lib / redis.ts
index abd75d5122d7162276fb245f7c2191ab952ad19b..3628c0583e3006dc91f30ba60fb076285cfe5fa1 100644 (file)
@@ -2,7 +2,13 @@ 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 {
+  CONFIG,
+  CONTACT_FORM_LIFETIME,
+  USER_EMAIL_VERIFY_LIFETIME,
+  USER_PASSWORD_RESET_LIFETIME,
+  VIDEO_VIEW_LIFETIME
+} from '../initializers'
 
 type CachedRoute = {
   body: string,
@@ -76,6 +82,16 @@ 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 isContactFormIpExists (ip: string) {
+    return this.exists(this.generateContactFormKey(ip))
+  }
+
   /************* Views per IP *************/
 
   setIPVideoView (ip: string, videoUUID: string) {
@@ -121,7 +137,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 +191,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 *************/