]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/redis.ts
Don't display remove file icon in some cases
[github/Chocobozzz/PeerTube.git] / server / lib / redis.ts
CommitLineData
a91e9beb 1import { createClient, RedisClientOptions, RedisModules } from 'redis'
e5d91a9b 2import { exists } from '@server/helpers/custom-validators/misc'
f1569117 3import { sha256 } from '@shared/extra-utils'
ecb4e35f
C
4import { logger } from '../helpers/logger'
5import { generateRandomString } from '../helpers/utils'
e5d91a9b 6import { CONFIG } from '../initializers/config'
a4101923 7import {
f1569117 8 AP_CLEANER,
a4101923 9 CONTACT_FORM_LIFETIME,
e5d91a9b
C
10 RESUMABLE_UPLOAD_SESSION_LIFETIME,
11 TRACKER_RATE_LIMITS,
a4101923 12 USER_EMAIL_VERIFY_LIFETIME,
45f1bd72 13 USER_PASSWORD_CREATE_LIFETIME,
e5d91a9b 14 USER_PASSWORD_RESET_LIFETIME,
e4bf7856 15 VIEW_LIFETIME,
e5d91a9b 16 WEBSERVER
74dc3bca 17} from '../initializers/constants'
4195cd2b 18
ecb4e35f
C
19class Redis {
20
21 private static instance: Redis
22 private initialized = false
e5d91a9b 23 private connected = false
a91e9beb 24 private client: ReturnType<typeof createClient>
ecb4e35f
C
25 private prefix: string
26
a1587156
C
27 private constructor () {
28 }
ecb4e35f
C
29
30 init () {
31 // Already initialized
32 if (this.initialized === true) return
33 this.initialized = true
34
47f6409b 35 this.client = createClient(Redis.getRedisClientOptions())
ab08ab4e 36 this.client.on('error', err => logger.error('Redis Client Error', { err }))
ecb4e35f 37
8f5a1f36
C
38 logger.info('Connecting to redis...')
39
e5d91a9b 40 this.client.connect()
8f5a1f36
C
41 .then(() => {
42 logger.info('Connected to redis.')
43
44 this.connected = true
45 }).catch(err => {
e5d91a9b
C
46 logger.error('Cannot connect to redis', { err })
47 process.exit(-1)
48 })
49
6dd9de95 50 this.prefix = 'redis-' + WEBSERVER.HOST + '-'
ecb4e35f
C
51 }
52
47f6409b 53 static getRedisClientOptions () {
8f5a1f36
C
54 let config: RedisClientOptions<RedisModules, {}> = {
55 socket: {
56 connectTimeout: 20000 // Could be slow since node use sync call to compile PeerTube
57 }
58 }
59
60 if (CONFIG.REDIS.AUTH) {
61 config = { ...config, password: CONFIG.REDIS.AUTH }
62 }
63
64 if (CONFIG.REDIS.DB) {
65 config = { ...config, database: CONFIG.REDIS.DB }
66 }
67
68 if (CONFIG.REDIS.HOSTNAME && CONFIG.REDIS.PORT) {
69 config.socket = { ...config.socket, host: CONFIG.REDIS.HOSTNAME, port: CONFIG.REDIS.PORT }
70 } else {
71 config.socket = { ...config.socket, path: CONFIG.REDIS.SOCKET }
72 }
73
74 return config
19f7b248
RK
75 }
76
47f6409b
C
77 getClient () {
78 return this.client
79 }
80
81 getPrefix () {
82 return this.prefix
83 }
84
e5d91a9b
C
85 isConnected () {
86 return this.connected
87 }
88
a1587156 89 /* ************ Forgot password ************ */
6e46de09 90
ecb4e35f
C
91 async setResetPasswordVerificationString (userId: number) {
92 const generatedString = await generateRandomString(32)
93
94 await this.setValue(this.generateResetPasswordKey(userId), generatedString, USER_PASSWORD_RESET_LIFETIME)
95
45f1bd72
JL
96 return generatedString
97 }
98
99 async setCreatePasswordVerificationString (userId: number) {
100 const generatedString = await generateRandomString(32)
101
102 await this.setValue(this.generateResetPasswordKey(userId), generatedString, USER_PASSWORD_CREATE_LIFETIME)
103
ecb4e35f
C
104 return generatedString
105 }
106
e9c5f123
C
107 async removePasswordVerificationString (userId: number) {
108 return this.removeValue(this.generateResetPasswordKey(userId))
109 }
110
ecb4e35f
C
111 async getResetPasswordLink (userId: number) {
112 return this.getValue(this.generateResetPasswordKey(userId))
113 }
114
a1587156 115 /* ************ Email verification ************ */
6e46de09 116
d9eaee39
JM
117 async setVerifyEmailVerificationString (userId: number) {
118 const generatedString = await generateRandomString(32)
119
120 await this.setValue(this.generateVerifyEmailKey(userId), generatedString, USER_EMAIL_VERIFY_LIFETIME)
121
122 return generatedString
123 }
124
125 async getVerifyEmailLink (userId: number) {
126 return this.getValue(this.generateVerifyEmailKey(userId))
127 }
128
a1587156 129 /* ************ Contact form per IP ************ */
a4101923
C
130
131 async setContactFormIp (ip: string) {
132 return this.setValue(this.generateContactFormKey(ip), '1', CONTACT_FORM_LIFETIME)
133 }
134
0f6acda1 135 async doesContactFormIpExist (ip: string) {
a4101923
C
136 return this.exists(this.generateContactFormKey(ip))
137 }
138
a1587156 139 /* ************ Views per IP ************ */
6e46de09 140
51353d9a
C
141 setIPVideoView (ip: string, videoUUID: string) {
142 return this.setValue(this.generateIPViewKey(ip, videoUUID), '1', VIEW_LIFETIME.VIEW)
143 }
e4bf7856 144
0f6acda1 145 async doesVideoIPViewExist (ip: string, videoUUID: string) {
51353d9a
C
146 return this.exists(this.generateIPViewKey(ip, videoUUID))
147 }
148
db48de85
C
149 /* ************ Tracker IP block ************ */
150
151 setTrackerBlockIP (ip: string) {
152 return this.setValue(this.generateTrackerBlockIPKey(ip), '1', TRACKER_RATE_LIMITS.BLOCK_IP_LIFETIME)
153 }
154
155 async doesTrackerBlockIPExist (ip: string) {
156 return this.exists(this.generateTrackerBlockIPKey(ip))
157 }
158
51353d9a 159 /* ************ Video views stats ************ */
6e46de09 160
51353d9a
C
161 addVideoViewStats (videoId: number) {
162 const { videoKey, setKey } = this.generateVideoViewStatsKeys({ videoId })
6b616860
C
163
164 return Promise.all([
51353d9a
C
165 this.addToSet(setKey, videoId.toString()),
166 this.increment(videoKey)
6b616860
C
167 ])
168 }
169
51353d9a
C
170 async getVideoViewsStats (videoId: number, hour: number) {
171 const { videoKey } = this.generateVideoViewStatsKeys({ videoId, hour })
6b616860 172
51353d9a 173 const valueString = await this.getValue(videoKey)
6040f87d
C
174 const valueInt = parseInt(valueString, 10)
175
176 if (isNaN(valueInt)) {
51353d9a 177 logger.error('Cannot get videos views stats of video %d in hour %d: views number is NaN (%s).', videoId, hour, valueString)
6040f87d
C
178 return undefined
179 }
180
181 return valueInt
6b616860
C
182 }
183
51353d9a
C
184 async listVideosViewedForStats (hour: number) {
185 const { setKey } = this.generateVideoViewStatsKeys({ hour })
6b616860 186
51353d9a 187 const stringIds = await this.getSet(setKey)
6b616860
C
188 return stringIds.map(s => parseInt(s, 10))
189 }
190
51353d9a
C
191 deleteVideoViewsStats (videoId: number, hour: number) {
192 const { setKey, videoKey } = this.generateVideoViewStatsKeys({ videoId, hour })
193
194 return Promise.all([
195 this.deleteFromSet(setKey, videoId.toString()),
196 this.deleteKey(videoKey)
197 ])
198 }
199
200 /* ************ Local video views buffer ************ */
201
202 addLocalVideoView (videoId: number) {
203 const { videoKey, setKey } = this.generateLocalVideoViewsKeys(videoId)
6b616860
C
204
205 return Promise.all([
51353d9a
C
206 this.addToSet(setKey, videoId.toString()),
207 this.increment(videoKey)
208 ])
209 }
210
211 async getLocalVideoViews (videoId: number) {
212 const { videoKey } = this.generateLocalVideoViewsKeys(videoId)
213
214 const valueString = await this.getValue(videoKey)
215 const valueInt = parseInt(valueString, 10)
216
217 if (isNaN(valueInt)) {
218 logger.error('Cannot get videos views of video %d: views number is NaN (%s).', videoId, valueString)
219 return undefined
220 }
221
222 return valueInt
223 }
224
225 async listLocalVideosViewed () {
226 const { setKey } = this.generateLocalVideoViewsKeys()
227
228 const stringIds = await this.getSet(setKey)
229 return stringIds.map(s => parseInt(s, 10))
230 }
231
232 deleteLocalVideoViews (videoId: number) {
233 const { setKey, videoKey } = this.generateLocalVideoViewsKeys(videoId)
234
235 return Promise.all([
236 this.deleteFromSet(setKey, videoId.toString()),
237 this.deleteKey(videoKey)
6b616860
C
238 ])
239 }
240
b2111066
C
241 /* ************ Video viewers stats ************ */
242
243 getLocalVideoViewer (options: {
244 key?: string
245 // Or
246 ip?: string
247 videoId?: number
248 }) {
249 if (options.key) return this.getObject(options.key)
250
251 const { viewerKey } = this.generateLocalVideoViewerKeys(options.ip, options.videoId)
252
253 return this.getObject(viewerKey)
254 }
255
256 setLocalVideoViewer (ip: string, videoId: number, object: any) {
257 const { setKey, viewerKey } = this.generateLocalVideoViewerKeys(ip, videoId)
258
259 return Promise.all([
260 this.addToSet(setKey, viewerKey),
261 this.setObject(viewerKey, object)
262 ])
263 }
264
265 listLocalVideoViewerKeys () {
266 const { setKey } = this.generateLocalVideoViewerKeys()
267
268 return this.getSet(setKey)
269 }
270
271 deleteLocalVideoViewersKeys (key: string) {
272 const { setKey } = this.generateLocalVideoViewerKeys()
273
274 return Promise.all([
275 this.deleteFromSet(setKey, key),
276 this.deleteKey(key)
277 ])
278 }
279
276250f0
RK
280 /* ************ Resumable uploads final responses ************ */
281
282 setUploadSession (uploadId: string, response?: { video: { id: number, shortUUID: string, uuid: string } }) {
283 return this.setValue(
284 'resumable-upload-' + uploadId,
285 response
286 ? JSON.stringify(response)
287 : '',
288 RESUMABLE_UPLOAD_SESSION_LIFETIME
289 )
290 }
291
292 doesUploadSessionExist (uploadId: string) {
293 return this.exists('resumable-upload-' + uploadId)
294 }
295
296 async getUploadSession (uploadId: string) {
297 const value = await this.getValue('resumable-upload-' + uploadId)
298
299 return value
300 ? JSON.parse(value)
301 : ''
302 }
303
020d3d3d
C
304 deleteUploadSession (uploadId: string) {
305 return this.deleteKey('resumable-upload-' + uploadId)
306 }
307
7a4fd56c 308 /* ************ AP resource unavailability ************ */
f1569117
C
309
310 async addAPUnavailability (url: string) {
311 const key = this.generateAPUnavailabilityKey(url)
312
313 const value = await this.increment(key)
314 await this.setExpiration(key, AP_CLEANER.PERIOD * 2)
315
316 return value
317 }
318
a1587156 319 /* ************ Keys generation ************ */
6e46de09 320
b2111066
C
321 private generateLocalVideoViewsKeys (videoId: number): { setKey: string, videoKey: string }
322 private generateLocalVideoViewsKeys (): { setKey: string }
323 private generateLocalVideoViewsKeys (videoId?: number) {
51353d9a 324 return { setKey: `local-video-views-buffer`, videoKey: `local-video-views-buffer-${videoId}` }
6b616860
C
325 }
326
b2111066
C
327 private generateLocalVideoViewerKeys (ip: string, videoId: number): { setKey: string, viewerKey: string }
328 private generateLocalVideoViewerKeys (): { setKey: string }
329 private generateLocalVideoViewerKeys (ip?: string, videoId?: number) {
330 return { setKey: `local-video-viewer-stats-keys`, viewerKey: `local-video-viewer-stats-${ip}-${videoId}` }
331 }
332
51353d9a
C
333 private generateVideoViewStatsKeys (options: { videoId?: number, hour?: number }) {
334 const hour = exists(options.hour)
335 ? options.hour
336 : new Date().getHours()
6b616860 337
51353d9a 338 return { setKey: `videos-view-h${hour}`, videoKey: `video-view-${options.videoId}-h${hour}` }
6b616860
C
339 }
340
6e46de09 341 private generateResetPasswordKey (userId: number) {
b40f0575
C
342 return 'reset-password-' + userId
343 }
344
6e46de09 345 private generateVerifyEmailKey (userId: number) {
d9eaee39
JM
346 return 'verify-email-' + userId
347 }
348
51353d9a 349 private generateIPViewKey (ip: string, videoUUID: string) {
a4101923
C
350 return `views-${videoUUID}-${ip}`
351 }
352
db48de85
C
353 private generateTrackerBlockIPKey (ip: string) {
354 return `tracker-block-ip-${ip}`
355 }
356
a4101923
C
357 private generateContactFormKey (ip: string) {
358 return 'contact-form-' + ip
b40f0575
C
359 }
360
f1569117
C
361 private generateAPUnavailabilityKey (url: string) {
362 return 'ap-unavailability-' + sha256(url)
363 }
364
a1587156 365 /* ************ Redis helpers ************ */
b40f0575 366
ecb4e35f 367 private getValue (key: string) {
e5d91a9b 368 return this.client.get(this.prefix + key)
ecb4e35f
C
369 }
370
6b616860 371 private getSet (key: string) {
e5d91a9b 372 return this.client.sMembers(this.prefix + key)
6b616860
C
373 }
374
375 private addToSet (key: string, value: string) {
e5d91a9b 376 return this.client.sAdd(this.prefix + key, value)
6b616860
C
377 }
378
379 private deleteFromSet (key: string, value: string) {
e5d91a9b 380 return this.client.sRem(this.prefix + key, value)
6b616860
C
381 }
382
383 private deleteKey (key: string) {
e5d91a9b 384 return this.client.del(this.prefix + key)
6e46de09
C
385 }
386
b2111066
C
387 private async getObject (key: string) {
388 const value = await this.getValue(key)
389 if (!value) return null
390
391 return JSON.parse(value)
392 }
393
394 private setObject (key: string, value: { [ id: string ]: number | string }) {
395 return this.setValue(key, JSON.stringify(value))
396 }
397
398 private async setValue (key: string, value: string, expirationMilliseconds?: number) {
399 const options = expirationMilliseconds
400 ? { PX: expirationMilliseconds }
401 : {}
402
403 const result = await this.client.set(this.prefix + key, value, options)
ecb4e35f 404
e5d91a9b 405 if (result !== 'OK') throw new Error('Redis set result is not OK.')
ecb4e35f
C
406 }
407
e9c5f123 408 private removeValue (key: string) {
e5d91a9b 409 return this.client.del(this.prefix + key)
4195cd2b
C
410 }
411
6b616860 412 private increment (key: string) {
e5d91a9b 413 return this.client.incr(this.prefix + key)
6b616860
C
414 }
415
c0d2eac3
C
416 private async exists (key: string) {
417 const result = await this.client.exists(this.prefix + key)
418
419 return result !== 0
b5c0e955
C
420 }
421
f1569117
C
422 private setExpiration (key: string, ms: number) {
423 return this.client.expire(this.prefix + key, ms / 1000)
424 }
425
ecb4e35f
C
426 static get Instance () {
427 return this.instance || (this.instance = new this())
428 }
429}
430
431// ---------------------------------------------------------------------------
432
433export {
434 Redis
435}