BEARKING CHANGE: Update videos API response
[oweals/peertube.git] / client / src / app / shared / video / video-details.model.ts
1 import {
2   UserRight,
3   VideoChannel,
4   VideoDetails as VideoDetailsServerModel,
5   VideoFile,
6   VideoPrivacy,
7   VideoResolution
8 } from '../../../../../shared'
9 import { Account } from '../../../../../shared/models/actors'
10 import { VideoConstant } from '../../../../../shared/models/videos/video.model'
11 import { AuthUser } from '../../core'
12 import { Video } from '../../shared/video/video.model'
13
14 export class VideoDetails extends Video implements VideoDetailsServerModel {
15   privacy: VideoConstant<VideoPrivacy>
16   descriptionPath: string
17   support: string
18   channel: VideoChannel
19   tags: string[]
20   files: VideoFile[]
21   account: Account
22   commentsEnabled: boolean
23
24   likesPercent: number
25   dislikesPercent: number
26
27   constructor (hash: VideoDetailsServerModel) {
28     super(hash)
29
30     this.privacy = hash.privacy
31     this.descriptionPath = hash.descriptionPath
32     this.files = hash.files
33     this.channel = hash.channel
34     this.account = hash.account
35     this.tags = hash.tags
36     this.support = hash.support
37     this.commentsEnabled = hash.commentsEnabled
38
39     this.buildLikeAndDislikePercents()
40   }
41
42   getAppropriateMagnetUri (actualDownloadSpeed = 0) {
43     if (this.files === undefined || this.files.length === 0) return ''
44     if (this.files.length === 1) return this.files[0].magnetUri
45
46     // Find first video that is good for our download speed (remember they are sorted)
47     let betterResolutionFile = this.files.find(f => actualDownloadSpeed > (f.size / this.duration))
48
49     // If the download speed is too bad, return the lowest resolution we have
50     if (betterResolutionFile === undefined) {
51       betterResolutionFile = this.files.find(f => f.resolution.id === VideoResolution.H_240P)
52     }
53
54     return betterResolutionFile.magnetUri
55   }
56
57   isRemovableBy (user: AuthUser) {
58     return user && this.isLocal === true && (this.account.name === user.username || user.hasRight(UserRight.REMOVE_ANY_VIDEO))
59   }
60
61   isBlackistableBy (user: AuthUser) {
62     return user && user.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) === true && this.isLocal === false
63   }
64
65   isUpdatableBy (user: AuthUser) {
66     return user && this.isLocal === true && (this.account.name === user.username || user.hasRight(UserRight.UPDATE_ANY_VIDEO))
67   }
68
69   buildLikeAndDislikePercents () {
70     this.likesPercent = (this.likes / (this.likes + this.dislikes)) * 100
71     this.dislikesPercent = (this.dislikes / (this.likes + this.dislikes)) * 100
72   }
73 }