aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/redis.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/lib/redis.ts')
-rw-r--r--server/lib/redis.ts41
1 files changed, 21 insertions, 20 deletions
diff --git a/server/lib/redis.ts b/server/lib/redis.ts
index f77d0b62c..0c5dbdd3e 100644
--- a/server/lib/redis.ts
+++ b/server/lib/redis.ts
@@ -12,7 +12,7 @@ import {
12import { CONFIG } from '../initializers/config' 12import { CONFIG } from '../initializers/config'
13 13
14type CachedRoute = { 14type CachedRoute = {
15 body: string, 15 body: string
16 contentType?: string 16 contentType?: string
17 statusCode?: string 17 statusCode?: string
18} 18}
@@ -24,7 +24,8 @@ class Redis {
24 private client: RedisClient 24 private client: RedisClient
25 private prefix: string 25 private prefix: string
26 26
27 private constructor () {} 27 private constructor () {
28 }
28 29
29 init () { 30 init () {
30 // Already initialized 31 // Already initialized
@@ -49,9 +50,9 @@ class Redis {
49 return Object.assign({}, 50 return Object.assign({},
50 (CONFIG.REDIS.AUTH && CONFIG.REDIS.AUTH != null) ? { password: CONFIG.REDIS.AUTH } : {}, 51 (CONFIG.REDIS.AUTH && CONFIG.REDIS.AUTH != null) ? { password: CONFIG.REDIS.AUTH } : {},
51 (CONFIG.REDIS.DB) ? { db: CONFIG.REDIS.DB } : {}, 52 (CONFIG.REDIS.DB) ? { db: CONFIG.REDIS.DB } : {},
52 (CONFIG.REDIS.HOSTNAME && CONFIG.REDIS.PORT) ? 53 (CONFIG.REDIS.HOSTNAME && CONFIG.REDIS.PORT)
53 { host: CONFIG.REDIS.HOSTNAME, port: CONFIG.REDIS.PORT } : 54 ? { host: CONFIG.REDIS.HOSTNAME, port: CONFIG.REDIS.PORT }
54 { path: CONFIG.REDIS.SOCKET } 55 : { path: CONFIG.REDIS.SOCKET }
55 ) 56 )
56 } 57 }
57 58
@@ -63,7 +64,7 @@ class Redis {
63 return this.prefix 64 return this.prefix
64 } 65 }
65 66
66 /************* Forgot password *************/ 67 /* ************ Forgot password ************ */
67 68
68 async setResetPasswordVerificationString (userId: number) { 69 async setResetPasswordVerificationString (userId: number) {
69 const generatedString = await generateRandomString(32) 70 const generatedString = await generateRandomString(32)
@@ -77,7 +78,7 @@ class Redis {
77 return this.getValue(this.generateResetPasswordKey(userId)) 78 return this.getValue(this.generateResetPasswordKey(userId))
78 } 79 }
79 80
80 /************* Email verification *************/ 81 /* ************ Email verification ************ */
81 82
82 async setVerifyEmailVerificationString (userId: number) { 83 async setVerifyEmailVerificationString (userId: number) {
83 const generatedString = await generateRandomString(32) 84 const generatedString = await generateRandomString(32)
@@ -91,7 +92,7 @@ class Redis {
91 return this.getValue(this.generateVerifyEmailKey(userId)) 92 return this.getValue(this.generateVerifyEmailKey(userId))
92 } 93 }
93 94
94 /************* Contact form per IP *************/ 95 /* ************ Contact form per IP ************ */
95 96
96 async setContactFormIp (ip: string) { 97 async setContactFormIp (ip: string) {
97 return this.setValue(this.generateContactFormKey(ip), '1', CONTACT_FORM_LIFETIME) 98 return this.setValue(this.generateContactFormKey(ip), '1', CONTACT_FORM_LIFETIME)
@@ -101,7 +102,7 @@ class Redis {
101 return this.exists(this.generateContactFormKey(ip)) 102 return this.exists(this.generateContactFormKey(ip))
102 } 103 }
103 104
104 /************* Views per IP *************/ 105 /* ************ Views per IP ************ */
105 106
106 setIPVideoView (ip: string, videoUUID: string) { 107 setIPVideoView (ip: string, videoUUID: string) {
107 return this.setValue(this.generateViewKey(ip, videoUUID), '1', VIDEO_VIEW_LIFETIME) 108 return this.setValue(this.generateViewKey(ip, videoUUID), '1', VIDEO_VIEW_LIFETIME)
@@ -111,7 +112,7 @@ class Redis {
111 return this.exists(this.generateViewKey(ip, videoUUID)) 112 return this.exists(this.generateViewKey(ip, videoUUID))
112 } 113 }
113 114
114 /************* API cache *************/ 115 /* ************ API cache ************ */
115 116
116 async getCachedRoute (req: express.Request) { 117 async getCachedRoute (req: express.Request) {
117 const cached = await this.getObject(this.generateCachedRouteKey(req)) 118 const cached = await this.getObject(this.generateCachedRouteKey(req))
@@ -120,17 +121,17 @@ class Redis {
120 } 121 }
121 122
122 setCachedRoute (req: express.Request, body: any, lifetime: number, contentType?: string, statusCode?: number) { 123 setCachedRoute (req: express.Request, body: any, lifetime: number, contentType?: string, statusCode?: number) {
123 const cached: CachedRoute = Object.assign({}, { 124 const cached: CachedRoute = Object.assign(
124 body: body.toString() 125 {},
125 }, 126 { body: body.toString() },
126 (contentType) ? { contentType } : null, 127 (contentType) ? { contentType } : null,
127 (statusCode) ? { statusCode: statusCode.toString() } : null 128 (statusCode) ? { statusCode: statusCode.toString() } : null
128 ) 129 )
129 130
130 return this.setObject(this.generateCachedRouteKey(req), cached, lifetime) 131 return this.setObject(this.generateCachedRouteKey(req), cached, lifetime)
131 } 132 }
132 133
133 /************* Video views *************/ 134 /* ************ Video views ************ */
134 135
135 addVideoView (videoId: number) { 136 addVideoView (videoId: number) {
136 const keyIncr = this.generateVideoViewKey(videoId) 137 const keyIncr = this.generateVideoViewKey(videoId)
@@ -173,7 +174,7 @@ class Redis {
173 ]) 174 ])
174 } 175 }
175 176
176 /************* Keys generation *************/ 177 /* ************ Keys generation ************ */
177 178
178 generateCachedRouteKey (req: express.Request) { 179 generateCachedRouteKey (req: express.Request) {
179 return req.method + '-' + req.originalUrl 180 return req.method + '-' + req.originalUrl
@@ -207,7 +208,7 @@ class Redis {
207 return 'contact-form-' + ip 208 return 'contact-form-' + ip
208 } 209 }
209 210
210 /************* Redis helpers *************/ 211 /* ************ Redis helpers ************ */
211 212
212 private getValue (key: string) { 213 private getValue (key: string) {
213 return new Promise<string>((res, rej) => { 214 return new Promise<string>((res, rej) => {
@@ -265,7 +266,7 @@ class Redis {
265 }) 266 })
266 } 267 }
267 268
268 private setObject (key: string, obj: { [ id: string ]: string }, expirationMilliseconds: number) { 269 private setObject (key: string, obj: { [id: string]: string }, expirationMilliseconds: number) {
269 return new Promise<void>((res, rej) => { 270 return new Promise<void>((res, rej) => {
270 this.client.hmset(this.prefix + key, obj, (err, ok) => { 271 this.client.hmset(this.prefix + key, obj, (err, ok) => {
271 if (err) return rej(err) 272 if (err) return rej(err)
@@ -282,7 +283,7 @@ class Redis {
282 } 283 }
283 284
284 private getObject (key: string) { 285 private getObject (key: string) {
285 return new Promise<{ [ id: string ]: string }>((res, rej) => { 286 return new Promise<{ [id: string]: string }>((res, rej) => {
286 this.client.hgetall(this.prefix + key, (err, value) => { 287 this.client.hgetall(this.prefix + key, (err, value) => {
287 if (err) return rej(err) 288 if (err) return rej(err)
288 289