Merge branch 'develop' into pr/1285
[oweals/peertube.git] / client / src / app / shared / video / video-details.model.ts
1 import { UserRight, VideoConstant, VideoDetails as VideoDetailsServerModel, VideoFile, VideoState } from '../../../../../shared'
2 import { AuthUser } from '../../core'
3 import { Video } from '../../shared/video/video.model'
4 import { Account } from '@app/shared/account/account.model'
5 import { VideoChannel } from '@app/shared/video-channel/video-channel.model'
6 import { VideoStreamingPlaylist } from '../../../../../shared/models/videos/video-streaming-playlist.model'
7 import { VideoStreamingPlaylistType } from '../../../../../shared/models/videos/video-streaming-playlist.type'
8
9 export class VideoDetails extends Video implements VideoDetailsServerModel {
10   descriptionPath: string
11   support: string
12   channel: VideoChannel
13   tags: string[]
14   files: VideoFile[]
15   account: Account
16   commentsEnabled: boolean
17   downloadEnabled: boolean
18
19   waitTranscoding: boolean
20   state: VideoConstant<VideoState>
21
22   likesPercent: number
23   dislikesPercent: number
24
25   trackerUrls: string[]
26
27   streamingPlaylists: VideoStreamingPlaylist[]
28
29   constructor (hash: VideoDetailsServerModel, translations = {}) {
30     super(hash, translations)
31
32     this.descriptionPath = hash.descriptionPath
33     this.files = hash.files
34     this.channel = new VideoChannel(hash.channel)
35     this.account = new Account(hash.account)
36     this.tags = hash.tags
37     this.support = hash.support
38     this.commentsEnabled = hash.commentsEnabled
39     this.downloadEnabled = hash.downloadEnabled
40
41     this.trackerUrls = hash.trackerUrls
42     this.streamingPlaylists = hash.streamingPlaylists
43
44     this.buildLikeAndDislikePercents()
45   }
46
47   isRemovableBy (user: AuthUser) {
48     return user && this.isLocal === true && (this.account.name === user.username || user.hasRight(UserRight.REMOVE_ANY_VIDEO))
49   }
50
51   isBlackistableBy (user: AuthUser) {
52     return this.blacklisted !== true && user && user.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) === true
53   }
54
55   isUnblacklistableBy (user: AuthUser) {
56     return this.blacklisted === true && user && user.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) === true
57   }
58
59   isUpdatableBy (user: AuthUser) {
60     return user && this.isLocal === true && (this.account.name === user.username || user.hasRight(UserRight.UPDATE_ANY_VIDEO))
61   }
62
63   buildLikeAndDislikePercents () {
64     this.likesPercent = (this.likes / (this.likes + this.dislikes)) * 100
65     this.dislikesPercent = (this.dislikes / (this.likes + this.dislikes)) * 100
66   }
67
68   getHlsPlaylist () {
69     return this.streamingPlaylists.find(p => p.type === VideoStreamingPlaylistType.HLS)
70   }
71 }