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.ts50
1 files changed, 30 insertions, 20 deletions
diff --git a/server/lib/redis.ts b/server/lib/redis.ts
index f77d0b62c..b4cd6f8e7 100644
--- a/server/lib/redis.ts
+++ b/server/lib/redis.ts
@@ -6,13 +6,14 @@ import {
6 CONTACT_FORM_LIFETIME, 6 CONTACT_FORM_LIFETIME,
7 USER_EMAIL_VERIFY_LIFETIME, 7 USER_EMAIL_VERIFY_LIFETIME,
8 USER_PASSWORD_RESET_LIFETIME, 8 USER_PASSWORD_RESET_LIFETIME,
9 USER_PASSWORD_CREATE_LIFETIME,
9 VIDEO_VIEW_LIFETIME, 10 VIDEO_VIEW_LIFETIME,
10 WEBSERVER 11 WEBSERVER
11} from '../initializers/constants' 12} from '../initializers/constants'
12import { CONFIG } from '../initializers/config' 13import { CONFIG } from '../initializers/config'
13 14
14type CachedRoute = { 15type CachedRoute = {
15 body: string, 16 body: string
16 contentType?: string 17 contentType?: string
17 statusCode?: string 18 statusCode?: string
18} 19}
@@ -24,7 +25,8 @@ class Redis {
24 private client: RedisClient 25 private client: RedisClient
25 private prefix: string 26 private prefix: string
26 27
27 private constructor () {} 28 private constructor () {
29 }
28 30
29 init () { 31 init () {
30 // Already initialized 32 // Already initialized
@@ -49,9 +51,9 @@ class Redis {
49 return Object.assign({}, 51 return Object.assign({},
50 (CONFIG.REDIS.AUTH && CONFIG.REDIS.AUTH != null) ? { password: CONFIG.REDIS.AUTH } : {}, 52 (CONFIG.REDIS.AUTH && CONFIG.REDIS.AUTH != null) ? { password: CONFIG.REDIS.AUTH } : {},
51 (CONFIG.REDIS.DB) ? { db: CONFIG.REDIS.DB } : {}, 53 (CONFIG.REDIS.DB) ? { db: CONFIG.REDIS.DB } : {},
52 (CONFIG.REDIS.HOSTNAME && CONFIG.REDIS.PORT) ? 54 (CONFIG.REDIS.HOSTNAME && CONFIG.REDIS.PORT)
53 { host: CONFIG.REDIS.HOSTNAME, port: CONFIG.REDIS.PORT } : 55 ? { host: CONFIG.REDIS.HOSTNAME, port: CONFIG.REDIS.PORT }
54 { path: CONFIG.REDIS.SOCKET } 56 : { path: CONFIG.REDIS.SOCKET }
55 ) 57 )
56 } 58 }
57 59
@@ -63,7 +65,7 @@ class Redis {
63 return this.prefix 65 return this.prefix
64 } 66 }
65 67
66 /************* Forgot password *************/ 68 /* ************ Forgot password ************ */
67 69
68 async setResetPasswordVerificationString (userId: number) { 70 async setResetPasswordVerificationString (userId: number) {
69 const generatedString = await generateRandomString(32) 71 const generatedString = await generateRandomString(32)
@@ -73,11 +75,19 @@ class Redis {
73 return generatedString 75 return generatedString
74 } 76 }
75 77
78 async setCreatePasswordVerificationString (userId: number) {
79 const generatedString = await generateRandomString(32)
80
81 await this.setValue(this.generateResetPasswordKey(userId), generatedString, USER_PASSWORD_CREATE_LIFETIME)
82
83 return generatedString
84 }
85
76 async getResetPasswordLink (userId: number) { 86 async getResetPasswordLink (userId: number) {
77 return this.getValue(this.generateResetPasswordKey(userId)) 87 return this.getValue(this.generateResetPasswordKey(userId))
78 } 88 }
79 89
80 /************* Email verification *************/ 90 /* ************ Email verification ************ */
81 91
82 async setVerifyEmailVerificationString (userId: number) { 92 async setVerifyEmailVerificationString (userId: number) {
83 const generatedString = await generateRandomString(32) 93 const generatedString = await generateRandomString(32)
@@ -91,7 +101,7 @@ class Redis {
91 return this.getValue(this.generateVerifyEmailKey(userId)) 101 return this.getValue(this.generateVerifyEmailKey(userId))
92 } 102 }
93 103
94 /************* Contact form per IP *************/ 104 /* ************ Contact form per IP ************ */
95 105
96 async setContactFormIp (ip: string) { 106 async setContactFormIp (ip: string) {
97 return this.setValue(this.generateContactFormKey(ip), '1', CONTACT_FORM_LIFETIME) 107 return this.setValue(this.generateContactFormKey(ip), '1', CONTACT_FORM_LIFETIME)
@@ -101,7 +111,7 @@ class Redis {
101 return this.exists(this.generateContactFormKey(ip)) 111 return this.exists(this.generateContactFormKey(ip))
102 } 112 }
103 113
104 /************* Views per IP *************/ 114 /* ************ Views per IP ************ */
105 115
106 setIPVideoView (ip: string, videoUUID: string) { 116 setIPVideoView (ip: string, videoUUID: string) {
107 return this.setValue(this.generateViewKey(ip, videoUUID), '1', VIDEO_VIEW_LIFETIME) 117 return this.setValue(this.generateViewKey(ip, videoUUID), '1', VIDEO_VIEW_LIFETIME)
@@ -111,7 +121,7 @@ class Redis {
111 return this.exists(this.generateViewKey(ip, videoUUID)) 121 return this.exists(this.generateViewKey(ip, videoUUID))
112 } 122 }
113 123
114 /************* API cache *************/ 124 /* ************ API cache ************ */
115 125
116 async getCachedRoute (req: express.Request) { 126 async getCachedRoute (req: express.Request) {
117 const cached = await this.getObject(this.generateCachedRouteKey(req)) 127 const cached = await this.getObject(this.generateCachedRouteKey(req))
@@ -120,17 +130,17 @@ class Redis {
120 } 130 }
121 131
122 setCachedRoute (req: express.Request, body: any, lifetime: number, contentType?: string, statusCode?: number) { 132 setCachedRoute (req: express.Request, body: any, lifetime: number, contentType?: string, statusCode?: number) {
123 const cached: CachedRoute = Object.assign({}, { 133 const cached: CachedRoute = Object.assign(
124 body: body.toString() 134 {},
125 }, 135 { body: body.toString() },
126 (contentType) ? { contentType } : null, 136 (contentType) ? { contentType } : null,
127 (statusCode) ? { statusCode: statusCode.toString() } : null 137 (statusCode) ? { statusCode: statusCode.toString() } : null
128 ) 138 )
129 139
130 return this.setObject(this.generateCachedRouteKey(req), cached, lifetime) 140 return this.setObject(this.generateCachedRouteKey(req), cached, lifetime)
131 } 141 }
132 142
133 /************* Video views *************/ 143 /* ************ Video views ************ */
134 144
135 addVideoView (videoId: number) { 145 addVideoView (videoId: number) {
136 const keyIncr = this.generateVideoViewKey(videoId) 146 const keyIncr = this.generateVideoViewKey(videoId)
@@ -173,7 +183,7 @@ class Redis {
173 ]) 183 ])
174 } 184 }
175 185
176 /************* Keys generation *************/ 186 /* ************ Keys generation ************ */
177 187
178 generateCachedRouteKey (req: express.Request) { 188 generateCachedRouteKey (req: express.Request) {
179 return req.method + '-' + req.originalUrl 189 return req.method + '-' + req.originalUrl
@@ -207,7 +217,7 @@ class Redis {
207 return 'contact-form-' + ip 217 return 'contact-form-' + ip
208 } 218 }
209 219
210 /************* Redis helpers *************/ 220 /* ************ Redis helpers ************ */
211 221
212 private getValue (key: string) { 222 private getValue (key: string) {
213 return new Promise<string>((res, rej) => { 223 return new Promise<string>((res, rej) => {
@@ -265,7 +275,7 @@ class Redis {
265 }) 275 })
266 } 276 }
267 277
268 private setObject (key: string, obj: { [ id: string ]: string }, expirationMilliseconds: number) { 278 private setObject (key: string, obj: { [id: string]: string }, expirationMilliseconds: number) {
269 return new Promise<void>((res, rej) => { 279 return new Promise<void>((res, rej) => {
270 this.client.hmset(this.prefix + key, obj, (err, ok) => { 280 this.client.hmset(this.prefix + key, obj, (err, ok) => {
271 if (err) return rej(err) 281 if (err) return rej(err)
@@ -282,7 +292,7 @@ class Redis {
282 } 292 }
283 293
284 private getObject (key: string) { 294 private getObject (key: string) {
285 return new Promise<{ [ id: string ]: string }>((res, rej) => { 295 return new Promise<{ [id: string]: string }>((res, rej) => {
286 this.client.hgetall(this.prefix + key, (err, value) => { 296 this.client.hgetall(this.prefix + key, (err, value) => {
287 if (err) return rej(err) 297 if (err) return rej(err)
288 298