+ async doesContactFormIpExist (ip: string) {
+ return this.exists(this.generateContactFormKey(ip))
+ }
+
+ /* ************ Views per IP ************ */
+
+ setIPVideoView (ip: string, videoUUID: string) {
+ return this.setValue(this.generateIPViewKey(ip, videoUUID), '1', VIEW_LIFETIME.VIEW)
+ }
+
+ async doesVideoIPViewExist (ip: string, videoUUID: string) {
+ return this.exists(this.generateIPViewKey(ip, videoUUID))
+ }
+
+ /* ************ Tracker IP block ************ */
+
+ setTrackerBlockIP (ip: string) {
+ return this.setValue(this.generateTrackerBlockIPKey(ip), '1', TRACKER_RATE_LIMITS.BLOCK_IP_LIFETIME)
+ }
+
+ async doesTrackerBlockIPExist (ip: string) {
+ return this.exists(this.generateTrackerBlockIPKey(ip))
+ }
+
+ /* ************ Video views stats ************ */
+
+ addVideoViewStats (videoId: number) {
+ const { videoKey, setKey } = this.generateVideoViewStatsKeys({ videoId })
+
+ return Promise.all([
+ this.addToSet(setKey, videoId.toString()),
+ this.increment(videoKey)
+ ])
+ }
+
+ async getVideoViewsStats (videoId: number, hour: number) {
+ const { videoKey } = this.generateVideoViewStatsKeys({ videoId, hour })
+
+ const valueString = await this.getValue(videoKey)
+ const valueInt = parseInt(valueString, 10)
+
+ if (isNaN(valueInt)) {
+ logger.error('Cannot get videos views stats of video %d in hour %d: views number is NaN (%s).', videoId, hour, valueString)
+ return undefined
+ }
+
+ return valueInt
+ }
+
+ async listVideosViewedForStats (hour: number) {
+ const { setKey } = this.generateVideoViewStatsKeys({ hour })
+
+ const stringIds = await this.getSet(setKey)
+ return stringIds.map(s => parseInt(s, 10))
+ }
+
+ deleteVideoViewsStats (videoId: number, hour: number) {
+ const { setKey, videoKey } = this.generateVideoViewStatsKeys({ videoId, hour })
+
+ return Promise.all([
+ this.deleteFromSet(setKey, videoId.toString()),
+ this.deleteKey(videoKey)
+ ])
+ }
+
+ /* ************ Local video views buffer ************ */
+
+ addLocalVideoView (videoId: number) {
+ const { videoKey, setKey } = this.generateLocalVideoViewsKeys(videoId)
+
+ return Promise.all([
+ this.addToSet(setKey, videoId.toString()),
+ this.increment(videoKey)
+ ])
+ }
+
+ async getLocalVideoViews (videoId: number) {
+ const { videoKey } = this.generateLocalVideoViewsKeys(videoId)
+
+ const valueString = await this.getValue(videoKey)
+ const valueInt = parseInt(valueString, 10)
+
+ if (isNaN(valueInt)) {
+ logger.error('Cannot get videos views of video %d: views number is NaN (%s).', videoId, valueString)
+ return undefined