]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/shared-main/video/video.model.ts
Add video file size info in admin videos list
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-main / video / video.model.ts
1 import { AuthUser } from '@app/core'
2 import { User } from '@app/core/users/user.model'
3 import { durationToString, getAbsoluteAPIUrl, getAbsoluteEmbedUrl } from '@app/helpers'
4 import { Actor } from '@app/shared/shared-main/account/actor.model'
5 import { buildVideoWatchPath } from '@shared/core-utils'
6 import { peertubeTranslate } from '@shared/core-utils/i18n'
7 import {
8 ActorImage,
9 HTMLServerConfig,
10 UserRight,
11 Video as VideoServerModel,
12 VideoConstant,
13 VideoFile,
14 VideoPrivacy,
15 VideoScheduleUpdate,
16 VideoState,
17 VideoStreamingPlaylist
18 } from '@shared/models'
19
20 export class Video implements VideoServerModel {
21 byVideoChannel: string
22 byAccount: string
23
24 createdAt: Date
25 updatedAt: Date
26 publishedAt: Date
27 originallyPublishedAt: Date | string
28 category: VideoConstant<number>
29 licence: VideoConstant<number>
30 language: VideoConstant<string>
31 privacy: VideoConstant<VideoPrivacy>
32
33 description: string
34
35 duration: number
36 durationLabel: string
37
38 id: number
39 uuid: string
40 shortUUID: string
41
42 isLocal: boolean
43
44 name: string
45 serverHost: string
46 thumbnailPath: string
47 thumbnailUrl: string
48
49 isLive: boolean
50
51 previewPath: string
52 previewUrl: string
53
54 embedPath: string
55 embedUrl: string
56
57 url: string
58
59 views: number
60 likes: number
61 dislikes: number
62 nsfw: boolean
63
64 originInstanceUrl: string
65 originInstanceHost: string
66
67 waitTranscoding?: boolean
68 state?: VideoConstant<VideoState>
69 scheduledUpdate?: VideoScheduleUpdate
70
71 blacklisted?: boolean
72 blacklistedReason?: string
73
74 blockedOwner?: boolean
75 blockedServer?: boolean
76
77 account: {
78 id: number
79 name: string
80 displayName: string
81 url: string
82 host: string
83 avatar?: ActorImage
84 }
85
86 channel: {
87 id: number
88 name: string
89 displayName: string
90 url: string
91 host: string
92 avatar?: ActorImage
93 }
94
95 userHistory?: {
96 currentTime: number
97 }
98
99 pluginData?: any
100
101 streamingPlaylists?: VideoStreamingPlaylist[]
102 files?: VideoFile[]
103
104 static buildWatchUrl (video: Partial<Pick<Video, 'uuid' | 'shortUUID'>>) {
105 return buildVideoWatchPath({ shortUUID: video.shortUUID || video.uuid })
106 }
107
108 static buildUpdateUrl (video: Pick<Video, 'uuid'>) {
109 return '/videos/update/' + video.uuid
110 }
111
112 constructor (hash: VideoServerModel, translations: { [ id: string ]: string } = {}) {
113 const absoluteAPIUrl = getAbsoluteAPIUrl()
114
115 this.createdAt = new Date(hash.createdAt.toString())
116 this.publishedAt = new Date(hash.publishedAt.toString())
117 this.category = hash.category
118 this.licence = hash.licence
119 this.language = hash.language
120 this.privacy = hash.privacy
121 this.waitTranscoding = hash.waitTranscoding
122 this.state = hash.state
123 this.description = hash.description
124
125 this.isLive = hash.isLive
126
127 this.duration = hash.duration
128 this.durationLabel = durationToString(hash.duration)
129
130 this.id = hash.id
131 this.uuid = hash.uuid
132 this.shortUUID = hash.shortUUID
133
134 this.isLocal = hash.isLocal
135 this.name = hash.name
136
137 this.thumbnailPath = hash.thumbnailPath
138 this.thumbnailUrl = this.thumbnailPath
139 ? hash.thumbnailUrl || (absoluteAPIUrl + hash.thumbnailPath)
140 : null
141
142 this.previewPath = hash.previewPath
143 this.previewUrl = this.previewPath
144 ? hash.previewUrl || (absoluteAPIUrl + hash.previewPath)
145 : null
146
147 this.embedPath = hash.embedPath
148 this.embedUrl = hash.embedUrl || (getAbsoluteEmbedUrl() + hash.embedPath)
149
150 this.url = hash.url
151
152 this.views = hash.views
153 this.likes = hash.likes
154 this.dislikes = hash.dislikes
155
156 this.nsfw = hash.nsfw
157
158 this.account = hash.account
159 this.channel = hash.channel
160
161 this.byAccount = Actor.CREATE_BY_STRING(hash.account.name, hash.account.host)
162 this.byVideoChannel = Actor.CREATE_BY_STRING(hash.channel.name, hash.channel.host)
163
164 this.category.label = peertubeTranslate(this.category.label, translations)
165 this.licence.label = peertubeTranslate(this.licence.label, translations)
166 this.language.label = peertubeTranslate(this.language.label, translations)
167 this.privacy.label = peertubeTranslate(this.privacy.label, translations)
168
169 this.scheduledUpdate = hash.scheduledUpdate
170 this.originallyPublishedAt = hash.originallyPublishedAt ? new Date(hash.originallyPublishedAt.toString()) : null
171
172 if (this.state) this.state.label = peertubeTranslate(this.state.label, translations)
173
174 this.blacklisted = hash.blacklisted
175 this.blacklistedReason = hash.blacklistedReason
176
177 this.blockedOwner = hash.blockedOwner
178 this.blockedServer = hash.blockedServer
179
180 this.streamingPlaylists = hash.streamingPlaylists
181 this.files = hash.files
182
183 this.userHistory = hash.userHistory
184
185 this.originInstanceHost = this.account.host
186 this.originInstanceUrl = 'https://' + this.originInstanceHost
187
188 this.pluginData = hash.pluginData
189 }
190
191 isVideoNSFWForUser (user: User, serverConfig: HTMLServerConfig) {
192 // Video is not NSFW, skip
193 if (this.nsfw === false) return false
194
195 // Return user setting if logged in
196 if (user) return user.nsfwPolicy !== 'display'
197
198 // Return default instance config
199 return serverConfig.instance.defaultNSFWPolicy !== 'display'
200 }
201
202 isRemovableBy (user: AuthUser) {
203 return user && this.isLocal === true && (this.account.name === user.username || user.hasRight(UserRight.REMOVE_ANY_VIDEO))
204 }
205
206 isBlockableBy (user: AuthUser) {
207 return this.blacklisted !== true && user && user.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) === true
208 }
209
210 isUnblockableBy (user: AuthUser) {
211 return this.blacklisted === true && user && user.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) === true
212 }
213
214 isUpdatableBy (user: AuthUser) {
215 return user && this.isLocal === true && (this.account.name === user.username || user.hasRight(UserRight.UPDATE_ANY_VIDEO))
216 }
217
218 isLiveInfoAvailableBy (user: AuthUser) {
219 return this.isLive &&
220 user && this.isLocal === true && (this.account.name === user.username || user.hasRight(UserRight.GET_ANY_LIVE))
221 }
222
223 canBeDuplicatedBy (user: AuthUser) {
224 return user && this.isLocal === false && user.hasRight(UserRight.MANAGE_VIDEOS_REDUNDANCIES)
225 }
226
227 getExactNumberOfViews () {
228 if (this.views < 1000) return ''
229
230 if (this.isLive) {
231 return $localize`${this.views} viewers`
232 }
233
234 return $localize`${this.views} views`
235 }
236 }