]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/redis.ts
Reduce AP context size on specific activities
[github/Chocobozzz/PeerTube.git] / server / lib / redis.ts
1 import * as express from 'express'
2 import { createClient, RedisClient } from 'redis'
3 import { logger } from '../helpers/logger'
4 import { generateRandomString } from '../helpers/utils'
5 import {
6 CONTACT_FORM_LIFETIME,
7 USER_EMAIL_VERIFY_LIFETIME,
8 USER_PASSWORD_RESET_LIFETIME,
9 VIDEO_VIEW_LIFETIME,
10 WEBSERVER
11 } from '../initializers/constants'
12 import { CONFIG } from '../initializers/config'
13
14 type CachedRoute = {
15 body: string
16 contentType?: string
17 statusCode?: string
18 }
19
20 class Redis {
21
22 private static instance: Redis
23 private initialized = false
24 private client: RedisClient
25 private prefix: string
26
27 private constructor () {
28 }
29
30 init () {
31 // Already initialized
32 if (this.initialized === true) return
33 this.initialized = true
34
35 this.client = createClient(Redis.getRedisClientOptions())
36
37 this.client.on('error', err => {
38 logger.error('Error in Redis client.', { err })
39 process.exit(-1)
40 })
41
42 if (CONFIG.REDIS.AUTH) {
43 this.client.auth(CONFIG.REDIS.AUTH)
44 }
45
46 this.prefix = 'redis-' + WEBSERVER.HOST + '-'
47 }
48
49 static getRedisClientOptions () {
50 return Object.assign({},
51 (CONFIG.REDIS.AUTH && CONFIG.REDIS.AUTH != null) ? { password: CONFIG.REDIS.AUTH } : {},
52 (CONFIG.REDIS.DB) ? { db: CONFIG.REDIS.DB } : {},
53 (CONFIG.REDIS.HOSTNAME && CONFIG.REDIS.PORT)
54 ? { host: CONFIG.REDIS.HOSTNAME, port: CONFIG.REDIS.PORT }
55 : { path: CONFIG.REDIS.SOCKET }
56 )
57 }
58
59 getClient () {
60 return this.client
61 }
62
63 getPrefix () {
64 return this.prefix
65 }
66
67 /* ************ Forgot password ************ */
68
69 async setResetPasswordVerificationString (userId: number) {
70 const generatedString = await generateRandomString(32)
71
72 await this.setValue(this.generateResetPasswordKey(userId), generatedString, USER_PASSWORD_RESET_LIFETIME)
73
74 return generatedString
75 }
76
77 async getResetPasswordLink (userId: number) {
78 return this.getValue(this.generateResetPasswordKey(userId))
79 }
80
81 /* ************ Email verification ************ */
82
83 async setVerifyEmailVerificationString (userId: number) {
84 const generatedString = await generateRandomString(32)
85
86 await this.setValue(this.generateVerifyEmailKey(userId), generatedString, USER_EMAIL_VERIFY_LIFETIME)
87
88 return generatedString
89 }
90
91 async getVerifyEmailLink (userId: number) {
92 return this.getValue(this.generateVerifyEmailKey(userId))
93 }
94
95 /* ************ Contact form per IP ************ */
96
97 async setContactFormIp (ip: string) {
98 return this.setValue(this.generateContactFormKey(ip), '1', CONTACT_FORM_LIFETIME)
99 }
100
101 async doesContactFormIpExist (ip: string) {
102 return this.exists(this.generateContactFormKey(ip))
103 }
104
105 /* ************ Views per IP ************ */
106
107 setIPVideoView (ip: string, videoUUID: string) {
108 return this.setValue(this.generateViewKey(ip, videoUUID), '1', VIDEO_VIEW_LIFETIME)
109 }
110
111 async doesVideoIPViewExist (ip: string, videoUUID: string) {
112 return this.exists(this.generateViewKey(ip, videoUUID))
113 }
114
115 /* ************ API cache ************ */
116
117 async getCachedRoute (req: express.Request) {
118 const cached = await this.getObject(this.generateCachedRouteKey(req))
119
120 return cached as CachedRoute
121 }
122
123 setCachedRoute (req: express.Request, body: any, lifetime: number, contentType?: string, statusCode?: number) {
124 const cached: CachedRoute = Object.assign(
125 {},
126 { body: body.toString() },
127 (contentType) ? { contentType } : null,
128 (statusCode) ? { statusCode: statusCode.toString() } : null
129 )
130
131 return this.setObject(this.generateCachedRouteKey(req), cached, lifetime)
132 }
133
134 /* ************ Video views ************ */
135
136 addVideoView (videoId: number) {
137 const keyIncr = this.generateVideoViewKey(videoId)
138 const keySet = this.generateVideosViewKey()
139
140 return Promise.all([
141 this.addToSet(keySet, videoId.toString()),
142 this.increment(keyIncr)
143 ])
144 }
145
146 async getVideoViews (videoId: number, hour: number) {
147 const key = this.generateVideoViewKey(videoId, hour)
148
149 const valueString = await this.getValue(key)
150 const valueInt = parseInt(valueString, 10)
151
152 if (isNaN(valueInt)) {
153 logger.error('Cannot get videos views of video %d in hour %d: views number is NaN (%s).', videoId, hour, valueString)
154 return undefined
155 }
156
157 return valueInt
158 }
159
160 async getVideosIdViewed (hour: number) {
161 const key = this.generateVideosViewKey(hour)
162
163 const stringIds = await this.getSet(key)
164 return stringIds.map(s => parseInt(s, 10))
165 }
166
167 deleteVideoViews (videoId: number, hour: number) {
168 const keySet = this.generateVideosViewKey(hour)
169 const keyIncr = this.generateVideoViewKey(videoId, hour)
170
171 return Promise.all([
172 this.deleteFromSet(keySet, videoId.toString()),
173 this.deleteKey(keyIncr)
174 ])
175 }
176
177 /* ************ Keys generation ************ */
178
179 generateCachedRouteKey (req: express.Request) {
180 return req.method + '-' + req.originalUrl
181 }
182
183 private generateVideosViewKey (hour?: number) {
184 if (!hour) hour = new Date().getHours()
185
186 return `videos-view-h${hour}`
187 }
188
189 private generateVideoViewKey (videoId: number, hour?: number) {
190 if (!hour) hour = new Date().getHours()
191
192 return `video-view-${videoId}-h${hour}`
193 }
194
195 private generateResetPasswordKey (userId: number) {
196 return 'reset-password-' + userId
197 }
198
199 private generateVerifyEmailKey (userId: number) {
200 return 'verify-email-' + userId
201 }
202
203 private generateViewKey (ip: string, videoUUID: string) {
204 return `views-${videoUUID}-${ip}`
205 }
206
207 private generateContactFormKey (ip: string) {
208 return 'contact-form-' + ip
209 }
210
211 /* ************ Redis helpers ************ */
212
213 private getValue (key: string) {
214 return new Promise<string>((res, rej) => {
215 this.client.get(this.prefix + key, (err, value) => {
216 if (err) return rej(err)
217
218 return res(value)
219 })
220 })
221 }
222
223 private getSet (key: string) {
224 return new Promise<string[]>((res, rej) => {
225 this.client.smembers(this.prefix + key, (err, value) => {
226 if (err) return rej(err)
227
228 return res(value)
229 })
230 })
231 }
232
233 private addToSet (key: string, value: string) {
234 return new Promise<string[]>((res, rej) => {
235 this.client.sadd(this.prefix + key, value, err => err ? rej(err) : res())
236 })
237 }
238
239 private deleteFromSet (key: string, value: string) {
240 return new Promise<void>((res, rej) => {
241 this.client.srem(this.prefix + key, value, err => err ? rej(err) : res())
242 })
243 }
244
245 private deleteKey (key: string) {
246 return new Promise<void>((res, rej) => {
247 this.client.del(this.prefix + key, err => err ? rej(err) : res())
248 })
249 }
250
251 private deleteFieldInHash (key: string, field: string) {
252 return new Promise<void>((res, rej) => {
253 this.client.hdel(this.prefix + key, field, err => err ? rej(err) : res())
254 })
255 }
256
257 private setValue (key: string, value: string, expirationMilliseconds: number) {
258 return new Promise<void>((res, rej) => {
259 this.client.set(this.prefix + key, value, 'PX', expirationMilliseconds, (err, ok) => {
260 if (err) return rej(err)
261
262 if (ok !== 'OK') return rej(new Error('Redis set result is not OK.'))
263
264 return res()
265 })
266 })
267 }
268
269 private setObject (key: string, obj: { [id: string]: string }, expirationMilliseconds: number) {
270 return new Promise<void>((res, rej) => {
271 this.client.hmset(this.prefix + key, obj, (err, ok) => {
272 if (err) return rej(err)
273 if (!ok) return rej(new Error('Redis mset result is not OK.'))
274
275 this.client.pexpire(this.prefix + key, expirationMilliseconds, (err, ok) => {
276 if (err) return rej(err)
277 if (!ok) return rej(new Error('Redis expiration result is not OK.'))
278
279 return res()
280 })
281 })
282 })
283 }
284
285 private getObject (key: string) {
286 return new Promise<{ [id: string]: string }>((res, rej) => {
287 this.client.hgetall(this.prefix + key, (err, value) => {
288 if (err) return rej(err)
289
290 return res(value)
291 })
292 })
293 }
294
295 private setValueInHash (key: string, field: string, value: string) {
296 return new Promise<void>((res, rej) => {
297 this.client.hset(this.prefix + key, field, value, (err) => {
298 if (err) return rej(err)
299
300 return res()
301 })
302 })
303 }
304
305 private increment (key: string) {
306 return new Promise<number>((res, rej) => {
307 this.client.incr(this.prefix + key, (err, value) => {
308 if (err) return rej(err)
309
310 return res(value)
311 })
312 })
313 }
314
315 private exists (key: string) {
316 return new Promise<boolean>((res, rej) => {
317 this.client.exists(this.prefix + key, (err, existsNumber) => {
318 if (err) return rej(err)
319
320 return res(existsNumber === 1)
321 })
322 })
323 }
324
325 static get Instance () {
326 return this.instance || (this.instance = new this())
327 }
328 }
329
330 // ---------------------------------------------------------------------------
331
332 export {
333 Redis
334 }