]>
Commit | Line | Data |
---|---|---|
b2111066 | 1 | import { logger, loggerTagsFactory } from '@server/helpers/logger' |
ac907dc7 C |
2 | import { sendView } from '@server/lib/activitypub/send/send-view' |
3 | import { getServerActor } from '@server/models/application/application' | |
b2111066 | 4 | import { MVideo } from '@server/types/models' |
ac907dc7 | 5 | import { buildUUID } from '@shared/extra-utils' |
b2111066 C |
6 | import { Redis } from '../../redis' |
7 | ||
8 | const lTags = loggerTagsFactory('views') | |
9 | ||
10 | export class VideoViews { | |
11 | ||
12 | async addLocalView (options: { | |
13 | video: MVideo | |
14 | ip: string | |
15 | watchTime: number | |
16 | }) { | |
17 | const { video, ip, watchTime } = options | |
18 | ||
19 | logger.debug('Adding local view to video %s.', video.uuid, { watchTime, ...lTags(video.uuid) }) | |
20 | ||
21 | if (!this.hasEnoughWatchTime(video, watchTime)) return false | |
22 | ||
23 | const viewExists = await Redis.Instance.doesVideoIPViewExist(ip, video.uuid) | |
24 | if (viewExists) return false | |
25 | ||
26 | await Redis.Instance.setIPVideoView(ip, video.uuid) | |
27 | ||
28 | await this.addView(video) | |
29 | ||
ac907dc7 C |
30 | await sendView({ byActor: await getServerActor(), video, type: 'view', viewerIdentifier: buildUUID() }) |
31 | ||
b2111066 C |
32 | return true |
33 | } | |
34 | ||
35 | async addRemoteView (options: { | |
36 | video: MVideo | |
37 | }) { | |
38 | const { video } = options | |
39 | ||
40 | logger.debug('Adding remote view to video %s.', video.uuid, { ...lTags(video.uuid) }) | |
41 | ||
42 | await this.addView(video) | |
43 | ||
44 | return true | |
45 | } | |
46 | ||
ac907dc7 C |
47 | // --------------------------------------------------------------------------- |
48 | ||
b2111066 C |
49 | private async addView (video: MVideo) { |
50 | const promises: Promise<any>[] = [] | |
51 | ||
52 | if (video.isOwned()) { | |
53 | promises.push(Redis.Instance.addLocalVideoView(video.id)) | |
54 | } | |
55 | ||
56 | promises.push(Redis.Instance.addVideoViewStats(video.id)) | |
57 | ||
58 | await Promise.all(promises) | |
59 | } | |
60 | ||
61 | private hasEnoughWatchTime (video: MVideo, watchTime: number) { | |
62 | if (video.isLive || video.duration >= 30) return watchTime >= 30 | |
63 | ||
64 | // Check more than 50% of the video is watched | |
65 | return video.duration / watchTime < 2 | |
66 | } | |
67 | } |