diff options
author | Chocobozzz <me@florianbigard.com> | 2018-02-23 16:39:51 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2018-02-23 16:44:37 +0100 |
commit | b5c0e95544cec5a33cee3df41c1607d2a0cd5403 (patch) | |
tree | 38a5db1faed107f7b75583c32125152812594821 /server/lib/redis.ts | |
parent | e3bb78a2134a5e5755b6dbd8987894572ca31269 (diff) | |
download | PeerTube-b5c0e95544cec5a33cee3df41c1607d2a0cd5403.tar.gz PeerTube-b5c0e95544cec5a33cee3df41c1607d2a0cd5403.tar.zst PeerTube-b5c0e95544cec5a33cee3df41c1607d2a0cd5403.zip |
Avoids easy cheating on vidoe views
Diffstat (limited to 'server/lib/redis.ts')
-rw-r--r-- | server/lib/redis.ts | 24 |
1 files changed, 23 insertions, 1 deletions
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 @@ | |||
1 | import { createClient, RedisClient } from 'redis' | 1 | import { createClient, RedisClient } from 'redis' |
2 | import { logger } from '../helpers/logger' | 2 | import { logger } from '../helpers/logger' |
3 | import { generateRandomString } from '../helpers/utils' | 3 | import { generateRandomString } from '../helpers/utils' |
4 | import { CONFIG, USER_PASSWORD_RESET_LIFETIME } from '../initializers' | 4 | import { CONFIG, USER_PASSWORD_RESET_LIFETIME, VIDEO_VIEW_LIFETIME } from '../initializers' |
5 | 5 | ||
6 | class Redis { | 6 | class Redis { |
7 | 7 | ||
@@ -46,6 +46,14 @@ class Redis { | |||
46 | return this.getValue(this.generateResetPasswordKey(userId)) | 46 | return this.getValue(this.generateResetPasswordKey(userId)) |
47 | } | 47 | } |
48 | 48 | ||
49 | setView (ip: string, videoUUID: string) { | ||
50 | return this.setValue(this.buildViewKey(ip, videoUUID), '1', VIDEO_VIEW_LIFETIME) | ||
51 | } | ||
52 | |||
53 | async isViewExists (ip: string, videoUUID: string) { | ||
54 | return this.exists(this.buildViewKey(ip, videoUUID)) | ||
55 | } | ||
56 | |||
49 | private getValue (key: string) { | 57 | private getValue (key: string) { |
50 | return new Promise<string>((res, rej) => { | 58 | return new Promise<string>((res, rej) => { |
51 | this.client.get(this.prefix + key, (err, value) => { | 59 | this.client.get(this.prefix + key, (err, value) => { |
@@ -68,10 +76,24 @@ class Redis { | |||
68 | }) | 76 | }) |
69 | } | 77 | } |
70 | 78 | ||
79 | private exists (key: string) { | ||
80 | return new Promise<boolean>((res, rej) => { | ||
81 | this.client.exists(this.prefix + key, (err, existsNumber) => { | ||
82 | if (err) return rej(err) | ||
83 | |||
84 | return res(existsNumber === 1) | ||
85 | }) | ||
86 | }) | ||
87 | } | ||
88 | |||
71 | private generateResetPasswordKey (userId: number) { | 89 | private generateResetPasswordKey (userId: number) { |
72 | return 'reset-password-' + userId | 90 | return 'reset-password-' + userId |
73 | } | 91 | } |
74 | 92 | ||
93 | private buildViewKey (ip: string, videoUUID: string) { | ||
94 | return videoUUID + '-' + ip | ||
95 | } | ||
96 | |||
75 | static get Instance () { | 97 | static get Instance () { |
76 | return this.instance || (this.instance = new this()) | 98 | return this.instance || (this.instance = new this()) |
77 | } | 99 | } |