diff options
Diffstat (limited to 'server/lib')
-rw-r--r-- | server/lib/redis.ts | 54 |
1 files changed, 42 insertions, 12 deletions
diff --git a/server/lib/redis.ts b/server/lib/redis.ts index e4e435659..abd75d512 100644 --- a/server/lib/redis.ts +++ b/server/lib/redis.ts | |||
@@ -48,6 +48,8 @@ class Redis { | |||
48 | ) | 48 | ) |
49 | } | 49 | } |
50 | 50 | ||
51 | /************* Forgot password *************/ | ||
52 | |||
51 | async setResetPasswordVerificationString (userId: number) { | 53 | async setResetPasswordVerificationString (userId: number) { |
52 | const generatedString = await generateRandomString(32) | 54 | const generatedString = await generateRandomString(32) |
53 | 55 | ||
@@ -60,6 +62,8 @@ class Redis { | |||
60 | return this.getValue(this.generateResetPasswordKey(userId)) | 62 | return this.getValue(this.generateResetPasswordKey(userId)) |
61 | } | 63 | } |
62 | 64 | ||
65 | /************* Email verification *************/ | ||
66 | |||
63 | async setVerifyEmailVerificationString (userId: number) { | 67 | async setVerifyEmailVerificationString (userId: number) { |
64 | const generatedString = await generateRandomString(32) | 68 | const generatedString = await generateRandomString(32) |
65 | 69 | ||
@@ -72,16 +76,20 @@ class Redis { | |||
72 | return this.getValue(this.generateVerifyEmailKey(userId)) | 76 | return this.getValue(this.generateVerifyEmailKey(userId)) |
73 | } | 77 | } |
74 | 78 | ||
79 | /************* Views per IP *************/ | ||
80 | |||
75 | setIPVideoView (ip: string, videoUUID: string) { | 81 | setIPVideoView (ip: string, videoUUID: string) { |
76 | return this.setValue(this.buildViewKey(ip, videoUUID), '1', VIDEO_VIEW_LIFETIME) | 82 | return this.setValue(this.generateViewKey(ip, videoUUID), '1', VIDEO_VIEW_LIFETIME) |
77 | } | 83 | } |
78 | 84 | ||
79 | async isVideoIPViewExists (ip: string, videoUUID: string) { | 85 | async isVideoIPViewExists (ip: string, videoUUID: string) { |
80 | return this.exists(this.buildViewKey(ip, videoUUID)) | 86 | return this.exists(this.generateViewKey(ip, videoUUID)) |
81 | } | 87 | } |
82 | 88 | ||
89 | /************* API cache *************/ | ||
90 | |||
83 | async getCachedRoute (req: express.Request) { | 91 | async getCachedRoute (req: express.Request) { |
84 | const cached = await this.getObject(this.buildCachedRouteKey(req)) | 92 | const cached = await this.getObject(this.generateCachedRouteKey(req)) |
85 | 93 | ||
86 | return cached as CachedRoute | 94 | return cached as CachedRoute |
87 | } | 95 | } |
@@ -94,9 +102,11 @@ class Redis { | |||
94 | (statusCode) ? { statusCode: statusCode.toString() } : null | 102 | (statusCode) ? { statusCode: statusCode.toString() } : null |
95 | ) | 103 | ) |
96 | 104 | ||
97 | return this.setObject(this.buildCachedRouteKey(req), cached, lifetime) | 105 | return this.setObject(this.generateCachedRouteKey(req), cached, lifetime) |
98 | } | 106 | } |
99 | 107 | ||
108 | /************* Video views *************/ | ||
109 | |||
100 | addVideoView (videoId: number) { | 110 | addVideoView (videoId: number) { |
101 | const keyIncr = this.generateVideoViewKey(videoId) | 111 | const keyIncr = this.generateVideoViewKey(videoId) |
102 | const keySet = this.generateVideosViewKey() | 112 | const keySet = this.generateVideosViewKey() |
@@ -131,33 +141,37 @@ class Redis { | |||
131 | ]) | 141 | ]) |
132 | } | 142 | } |
133 | 143 | ||
134 | generateVideosViewKey (hour?: number) { | 144 | /************* Keys generation *************/ |
145 | |||
146 | generateCachedRouteKey (req: express.Request) { | ||
147 | return req.method + '-' + req.originalUrl | ||
148 | } | ||
149 | |||
150 | private generateVideosViewKey (hour?: number) { | ||
135 | if (!hour) hour = new Date().getHours() | 151 | if (!hour) hour = new Date().getHours() |
136 | 152 | ||
137 | return `videos-view-h${hour}` | 153 | return `videos-view-h${hour}` |
138 | } | 154 | } |
139 | 155 | ||
140 | generateVideoViewKey (videoId: number, hour?: number) { | 156 | private generateVideoViewKey (videoId: number, hour?: number) { |
141 | if (!hour) hour = new Date().getHours() | 157 | if (!hour) hour = new Date().getHours() |
142 | 158 | ||
143 | return `video-view-${videoId}-h${hour}` | 159 | return `video-view-${videoId}-h${hour}` |
144 | } | 160 | } |
145 | 161 | ||
146 | generateResetPasswordKey (userId: number) { | 162 | private generateResetPasswordKey (userId: number) { |
147 | return 'reset-password-' + userId | 163 | return 'reset-password-' + userId |
148 | } | 164 | } |
149 | 165 | ||
150 | generateVerifyEmailKey (userId: number) { | 166 | private generateVerifyEmailKey (userId: number) { |
151 | return 'verify-email-' + userId | 167 | return 'verify-email-' + userId |
152 | } | 168 | } |
153 | 169 | ||
154 | buildViewKey (ip: string, videoUUID: string) { | 170 | private generateViewKey (ip: string, videoUUID: string) { |
155 | return videoUUID + '-' + ip | 171 | return videoUUID + '-' + ip |
156 | } | 172 | } |
157 | 173 | ||
158 | buildCachedRouteKey (req: express.Request) { | 174 | /************* Redis helpers *************/ |
159 | return req.method + '-' + req.originalUrl | ||
160 | } | ||
161 | 175 | ||
162 | private getValue (key: string) { | 176 | private getValue (key: string) { |
163 | return new Promise<string>((res, rej) => { | 177 | return new Promise<string>((res, rej) => { |
@@ -197,6 +211,12 @@ class Redis { | |||
197 | }) | 211 | }) |
198 | } | 212 | } |
199 | 213 | ||
214 | private deleteFieldInHash (key: string, field: string) { | ||
215 | return new Promise<void>((res, rej) => { | ||
216 | this.client.hdel(this.prefix + key, field, err => err ? rej(err) : res()) | ||
217 | }) | ||
218 | } | ||
219 | |||
200 | private setValue (key: string, value: string, expirationMilliseconds: number) { | 220 | private setValue (key: string, value: string, expirationMilliseconds: number) { |
201 | return new Promise<void>((res, rej) => { | 221 | return new Promise<void>((res, rej) => { |
202 | this.client.set(this.prefix + key, value, 'PX', expirationMilliseconds, (err, ok) => { | 222 | this.client.set(this.prefix + key, value, 'PX', expirationMilliseconds, (err, ok) => { |
@@ -235,6 +255,16 @@ class Redis { | |||
235 | }) | 255 | }) |
236 | } | 256 | } |
237 | 257 | ||
258 | private setValueInHash (key: string, field: string, value: string) { | ||
259 | return new Promise<void>((res, rej) => { | ||
260 | this.client.hset(this.prefix + key, field, value, (err) => { | ||
261 | if (err) return rej(err) | ||
262 | |||
263 | return res() | ||
264 | }) | ||
265 | }) | ||
266 | } | ||
267 | |||
238 | private increment (key: string) { | 268 | private increment (key: string) { |
239 | return new Promise<number>((res, rej) => { | 269 | return new Promise<number>((res, rej) => { |
240 | this.client.incr(this.prefix + key, (err, value) => { | 270 | this.client.incr(this.prefix + key, (err, value) => { |