Add concept of video state, and add ability to wait transcoding before
[oweals/peertube.git] / client / src / app / shared / video / video.model.ts
1 import { User } from '../'
2 import { Video as VideoServerModel, VideoPrivacy, VideoState } from '../../../../../shared'
3 import { Avatar } from '../../../../../shared/models/avatars/avatar.model'
4 import { VideoConstant } from '../../../../../shared/models/videos/video.model'
5 import { getAbsoluteAPIUrl } from '../misc/utils'
6 import { ServerConfig } from '../../../../../shared/models'
7 import { Actor } from '@app/shared/actor/actor.model'
8 import { peertubeTranslate } from '@app/shared/i18n/i18n-utils'
9
10 export class Video implements VideoServerModel {
11   by: string
12   accountAvatarUrl: string
13   createdAt: Date
14   updatedAt: Date
15   publishedAt: Date
16   category: VideoConstant<number>
17   licence: VideoConstant<number>
18   language: VideoConstant<string>
19   privacy: VideoConstant<VideoPrivacy>
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   thumbnailPath: string
29   thumbnailUrl: string
30   previewPath: string
31   previewUrl: string
32   embedPath: string
33   embedUrl: string
34   views: number
35   likes: number
36   dislikes: number
37   nsfw: boolean
38
39   waitTranscoding?: boolean
40   state?: VideoConstant<VideoState>
41
42   account: {
43     id: number
44     uuid: string
45     name: string
46     displayName: string
47     url: string
48     host: string
49     avatar: Avatar
50   }
51
52   channel: {
53     id: number
54     uuid: string
55     name: string
56     displayName: string
57     url: string
58     host: string
59     avatar: Avatar
60   }
61
62   private static createDurationString (duration: number) {
63     const hours = Math.floor(duration / 3600)
64     const minutes = Math.floor((duration % 3600) / 60)
65     const seconds = duration % 60
66
67     const minutesPadding = minutes >= 10 ? '' : '0'
68     const secondsPadding = seconds >= 10 ? '' : '0'
69     const displayedHours = hours > 0 ? hours.toString() + ':' : ''
70
71     return displayedHours + minutesPadding + minutes.toString() + ':' + secondsPadding + seconds.toString()
72   }
73
74   constructor (hash: VideoServerModel, translations = {}) {
75     const absoluteAPIUrl = getAbsoluteAPIUrl()
76
77     this.createdAt = new Date(hash.createdAt.toString())
78     this.publishedAt = new Date(hash.publishedAt.toString())
79     this.category = hash.category
80     this.licence = hash.licence
81     this.language = hash.language
82     this.privacy = hash.privacy
83     this.waitTranscoding = hash.waitTranscoding
84     this.state = hash.state
85     this.description = hash.description
86     this.duration = hash.duration
87     this.durationLabel = Video.createDurationString(hash.duration)
88     this.id = hash.id
89     this.uuid = hash.uuid
90     this.isLocal = hash.isLocal
91     this.name = hash.name
92     this.thumbnailPath = hash.thumbnailPath
93     this.thumbnailUrl = absoluteAPIUrl + hash.thumbnailPath
94     this.previewPath = hash.previewPath
95     this.previewUrl = absoluteAPIUrl + hash.previewPath
96     this.embedPath = hash.embedPath
97     this.embedUrl = absoluteAPIUrl + hash.embedPath
98     this.views = hash.views
99     this.likes = hash.likes
100     this.dislikes = hash.dislikes
101     this.nsfw = hash.nsfw
102     this.account = hash.account
103
104     this.by = Actor.CREATE_BY_STRING(hash.account.name, hash.account.host)
105     this.accountAvatarUrl = Actor.GET_ACTOR_AVATAR_URL(this.account)
106
107     this.category.label = peertubeTranslate(this.category.label, translations)
108     this.licence.label = peertubeTranslate(this.licence.label, translations)
109     this.language.label = peertubeTranslate(this.language.label, translations)
110     this.privacy.label = peertubeTranslate(this.privacy.label, translations)
111
112     if (this.state) this.state.label = peertubeTranslate(this.state.label, translations)
113   }
114
115   isVideoNSFWForUser (user: User, serverConfig: ServerConfig) {
116     // Video is not NSFW, skip
117     if (this.nsfw === false) return false
118
119     // Return user setting if logged in
120     if (user) return user.nsfwPolicy !== 'display'
121
122     // Return default instance config
123     return serverConfig.instance.defaultNSFWPolicy !== 'display'
124   }
125 }