From b5c0e95544cec5a33cee3df41c1607d2a0cd5403 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 23 Feb 2018 16:39:51 +0100 Subject: Avoids easy cheating on vidoe views --- server/lib/redis.ts | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) (limited to 'server/lib/redis.ts') diff --git a/server/lib/redis.ts b/server/lib/redis.ts index 4240cc162..b284cab8f 100644 --- a/server/lib/redis.ts +++ b/server/lib/redis.ts @@ -1,7 +1,7 @@ import { createClient, RedisClient } from 'redis' import { logger } from '../helpers/logger' import { generateRandomString } from '../helpers/utils' -import { CONFIG, USER_PASSWORD_RESET_LIFETIME } from '../initializers' +import { CONFIG, USER_PASSWORD_RESET_LIFETIME, VIDEO_VIEW_LIFETIME } from '../initializers' class Redis { @@ -46,6 +46,14 @@ class Redis { return this.getValue(this.generateResetPasswordKey(userId)) } + setView (ip: string, videoUUID: string) { + return this.setValue(this.buildViewKey(ip, videoUUID), '1', VIDEO_VIEW_LIFETIME) + } + + async isViewExists (ip: string, videoUUID: string) { + return this.exists(this.buildViewKey(ip, videoUUID)) + } + private getValue (key: string) { return new Promise((res, rej) => { this.client.get(this.prefix + key, (err, value) => { @@ -68,10 +76,24 @@ class Redis { }) } + private exists (key: string) { + return new Promise((res, rej) => { + this.client.exists(this.prefix + key, (err, existsNumber) => { + if (err) return rej(err) + + return res(existsNumber === 1) + }) + }) + } + private generateResetPasswordKey (userId: number) { return 'reset-password-' + userId } + private buildViewKey (ip: string, videoUUID: string) { + return videoUUID + '-' + ip + } + static get Instance () { return this.instance || (this.instance = new this()) } -- cgit v1.2.3