]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/redis.ts
Update README.md (#378)
[github/Chocobozzz/PeerTube.git] / server / lib / redis.ts
index 4240cc1628c300a51d3b9def9a58c86dcb612fbc..1fd366401c31445d455e6027983c90e2c5542e01 100644 (file)
@@ -1,7 +1,7 @@
 import { createClient, RedisClient } from 'redis'
 import { logger } from '../helpers/logger'
 import { generateRandomString } from '../helpers/utils'
-import { CONFIG, USER_PASSWORD_RESET_LIFETIME } from '../initializers'
+import { CONFIG, USER_PASSWORD_RESET_LIFETIME, VIDEO_VIEW_LIFETIME } from '../initializers'
 
 class Redis {
 
@@ -46,6 +46,24 @@ class Redis {
     return this.getValue(this.generateResetPasswordKey(userId))
   }
 
+  setView (ip: string, videoUUID: string) {
+    return this.setValue(this.buildViewKey(ip, videoUUID), '1', VIDEO_VIEW_LIFETIME)
+  }
+
+  async isViewExists (ip: string, videoUUID: string) {
+    return this.exists(this.buildViewKey(ip, videoUUID))
+  }
+
+  listJobs (jobsPrefix: string, state: string, mode: 'alpha', order: 'ASC' | 'DESC', offset: number, count: number) {
+    return new Promise<string[]>((res, rej) => {
+      this.client.sort(jobsPrefix + ':jobs:' + state, 'by', mode, order, 'LIMIT', offset.toString(), count.toString(), (err, values) => {
+        if (err) return rej(err)
+
+        return res(values)
+      })
+    })
+  }
+
   private getValue (key: string) {
     return new Promise<string>((res, rej) => {
       this.client.get(this.prefix + key, (err, value) => {
@@ -68,10 +86,24 @@ class Redis {
     })
   }
 
+  private exists (key: string) {
+    return new Promise<boolean>((res, rej) => {
+      this.client.exists(this.prefix + key, (err, existsNumber) => {
+        if (err) return rej(err)
+
+        return res(existsNumber === 1)
+      })
+    })
+  }
+
   private generateResetPasswordKey (userId: number) {
     return 'reset-password-' + userId
   }
 
+  private buildViewKey (ip: string, videoUUID: string) {
+    return videoUUID + '-' + ip
+  }
+
   static get Instance () {
     return this.instance || (this.instance = new this())
   }