Fix video play promise error on non supported browsers
[oweals/peertube.git] / client / src / app / shared / video / video-details.model.ts
1 import {
2   UserRight, VideoChannel, VideoDetails as VideoDetailsServerModel, VideoFile, VideoPrivacy,
3   VideoResolution
4 } from '../../../../../shared'
5 import { Account } from '../../../../../shared/models/actors'
6 import { AuthUser } from '../../core'
7 import { Video } from '../../shared/video/video.model'
8
9 export class VideoDetails extends Video implements VideoDetailsServerModel {
10   accountName: string
11   by: string
12   createdAt: Date
13   updatedAt: Date
14   categoryLabel: string
15   category: number
16   licenceLabel: string
17   licence: number
18   languageLabel: string
19   language: number
20   description: string
21   duration: number
22   durationLabel: string
23   id: number
24   uuid: string
25   isLocal: boolean
26   name: string
27   serverHost: string
28   tags: string[]
29   thumbnailPath: string
30   thumbnailUrl: string
31   previewPath: string
32   previewUrl: string
33   embedPath: string
34   embedUrl: string
35   views: number
36   likes: number
37   dislikes: number
38   nsfw: boolean
39   descriptionPath: string
40   files: VideoFile[]
41   channel: VideoChannel
42   privacy: VideoPrivacy
43   privacyLabel: string
44   account: Account
45   likesPercent: number
46   dislikesPercent: number
47   commentsEnabled: boolean
48
49   constructor (hash: VideoDetailsServerModel) {
50     super(hash)
51
52     this.privacy = hash.privacy
53     this.privacyLabel = hash.privacyLabel
54     this.descriptionPath = hash.descriptionPath
55     this.files = hash.files
56     this.channel = hash.channel
57     this.account = hash.account
58     this.tags = hash.tags
59     this.commentsEnabled = hash.commentsEnabled
60
61     this.likesPercent = (this.likes / (this.likes + this.dislikes)) * 100
62     this.dislikesPercent = (this.dislikes / (this.likes + this.dislikes)) * 100
63   }
64
65   getAppropriateMagnetUri (actualDownloadSpeed = 0) {
66     if (this.files === undefined || this.files.length === 0) return ''
67     if (this.files.length === 1) return this.files[0].magnetUri
68
69     // Find first video that is good for our download speed (remember they are sorted)
70     let betterResolutionFile = this.files.find(f => actualDownloadSpeed > (f.size / this.duration))
71
72     // If the download speed is too bad, return the lowest resolution we have
73     if (betterResolutionFile === undefined) {
74       betterResolutionFile = this.files.find(f => f.resolution === VideoResolution.H_240P)
75     }
76
77     return betterResolutionFile.magnetUri
78   }
79
80   isRemovableBy (user: AuthUser) {
81     return user && this.isLocal === true && (this.accountName === user.username || user.hasRight(UserRight.REMOVE_ANY_VIDEO))
82   }
83
84   isBlackistableBy (user: AuthUser) {
85     return user && user.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) === true && this.isLocal === false
86   }
87
88   isUpdatableBy (user: AuthUser) {
89     return user && this.isLocal === true && user.username === this.accountName
90   }
91 }