X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Flib%2Fredis.ts;h=62641e313af34f63a72da710e815b4bedbc0c771;hb=2539932e16129992a2c0889b4ff527c265a8e2c7;hp=5313c46857a95254df77cc2d4717e763a803db3d;hpb=db48de8597897e5024f8e9ed5acb1a8f40748169;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/lib/redis.ts b/server/lib/redis.ts index 5313c4685..62641e313 100644 --- a/server/lib/redis.ts +++ b/server/lib/redis.ts @@ -7,7 +7,7 @@ import { USER_EMAIL_VERIFY_LIFETIME, USER_PASSWORD_RESET_LIFETIME, USER_PASSWORD_CREATE_LIFETIME, - VIDEO_VIEW_LIFETIME, + VIEW_LIFETIME, WEBSERVER, TRACKER_RATE_LIMITS } from '../initializers/constants' @@ -84,6 +84,10 @@ class Redis { return generatedString } + async removePasswordVerificationString (userId: number) { + return this.removeValue(this.generateResetPasswordKey(userId)) + } + async getResetPasswordLink (userId: number) { return this.getValue(this.generateResetPasswordKey(userId)) } @@ -114,8 +118,12 @@ class Redis { /* ************ Views per IP ************ */ - setIPVideoView (ip: string, videoUUID: string) { - return this.setValue(this.generateViewKey(ip, videoUUID), '1', VIDEO_VIEW_LIFETIME) + setIPVideoView (ip: string, videoUUID: string, isLive: boolean) { + const lifetime = isLive + ? VIEW_LIFETIME.LIVE + : VIEW_LIFETIME.VIDEO + + return this.setValue(this.generateViewKey(ip, videoUUID), '1', lifetime) } async doesVideoIPViewExist (ip: string, videoUUID: string) { @@ -207,7 +215,7 @@ class Redis { } private generateVideoViewKey (videoId: number, hour?: number) { - if (!hour) hour = new Date().getHours() + if (hour === undefined || hour === null) hour = new Date().getHours() return `video-view-${videoId}-h${hour}` } @@ -255,7 +263,7 @@ class Redis { } private addToSet (key: string, value: string) { - return new Promise((res, rej) => { + return new Promise((res, rej) => { this.client.sadd(this.prefix + key, value, err => err ? rej(err) : res()) }) } @@ -290,6 +298,16 @@ class Redis { }) } + private removeValue (key: string) { + return new Promise((res, rej) => { + this.client.del(this.prefix + key, err => { + if (err) return rej(err) + + return res() + }) + }) + } + private setObject (key: string, obj: { [id: string]: string }, expirationMilliseconds: number) { return new Promise((res, rej) => { this.client.hmset(this.prefix + key, obj, (err, ok) => {