Implement support field in video and video channel
[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   support: string
22   duration: number
23   durationLabel: string
24   id: number
25   uuid: string
26   isLocal: boolean
27   name: string
28   serverHost: string
29   tags: string[]
30   thumbnailPath: string
31   thumbnailUrl: string
32   previewPath: string
33   previewUrl: string
34   embedPath: string
35   embedUrl: string
36   views: number
37   likes: number
38   dislikes: number
39   nsfw: boolean
40   descriptionPath: string
41   files: VideoFile[]
42   channel: VideoChannel
43   privacy: VideoPrivacy
44   privacyLabel: string
45   account: Account
46   likesPercent: number
47   dislikesPercent: number
48   commentsEnabled: boolean
49
50   constructor (hash: VideoDetailsServerModel) {
51     super(hash)
52
53     this.privacy = hash.privacy
54     this.privacyLabel = hash.privacyLabel
55     this.descriptionPath = hash.descriptionPath
56     this.files = hash.files
57     this.channel = hash.channel
58     this.account = hash.account
59     this.tags = hash.tags
60     this.commentsEnabled = hash.commentsEnabled
61
62     this.likesPercent = (this.likes / (this.likes + this.dislikes)) * 100
63     this.dislikesPercent = (this.dislikes / (this.likes + this.dislikes)) * 100
64   }
65
66   getAppropriateMagnetUri (actualDownloadSpeed = 0) {
67     if (this.files === undefined || this.files.length === 0) return ''
68     if (this.files.length === 1) return this.files[0].magnetUri
69
70     // Find first video that is good for our download speed (remember they are sorted)
71     let betterResolutionFile = this.files.find(f => actualDownloadSpeed > (f.size / this.duration))
72
73     // If the download speed is too bad, return the lowest resolution we have
74     if (betterResolutionFile === undefined) {
75       betterResolutionFile = this.files.find(f => f.resolution === VideoResolution.H_240P)
76     }
77
78     return betterResolutionFile.magnetUri
79   }
80
81   isRemovableBy (user: AuthUser) {
82     return user && this.isLocal === true && (this.accountName === user.username || user.hasRight(UserRight.REMOVE_ANY_VIDEO))
83   }
84
85   isBlackistableBy (user: AuthUser) {
86     return user && user.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) === true && this.isLocal === false
87   }
88
89   isUpdatableBy (user: AuthUser) {
90     return user && this.isLocal === true && user.username === this.accountName
91   }
92 }