]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/shared-main/video/video.model.ts
Add reset button for file inputs in studio page
[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 VideoStreamingPlaylistType
19 } from '@shared/models'
20
21 export class Video implements VideoServerModel {
22 byVideoChannel: string
23 byAccount: string
24
25 createdAt: Date
26 updatedAt: Date
27 publishedAt: Date
28 originallyPublishedAt: Date | string
29 category: VideoConstant<number>
30 licence: VideoConstant<number>
31 language: VideoConstant<string>
32 privacy: VideoConstant<VideoPrivacy>
33
34 description: string
35
36 duration: number
37 durationLabel: string
38
39 id: number
40 uuid: string
41 shortUUID: string
42
43 isLocal: boolean
44
45 name: string
46 serverHost: string
47 thumbnailPath: string
48 thumbnailUrl: string
49
50 isLive: boolean
51
52 previewPath: string
53 previewUrl: string
54
55 embedPath: string
56 embedUrl: string
57
58 url: string
59
60 views: number
61 // If live
62 viewers?: number
63
64 likes: number
65 dislikes: number
66 nsfw: boolean
67
68 originInstanceUrl: string
69 originInstanceHost: string
70
71 waitTranscoding?: boolean
72 state?: VideoConstant<VideoState>
73 scheduledUpdate?: VideoScheduleUpdate
74
75 blacklisted?: boolean
76 blacklistedReason?: string
77
78 blockedOwner?: boolean
79 blockedServer?: boolean
80
81 account: {
82 id: number
83 name: string
84 displayName: string
85 url: string
86 host: string
87
88 // TODO: remove, deprecated in 4.2
89 avatar: ActorImage
90
91 avatars: ActorImage[]
92 }
93
94 channel: {
95 id: number
96 name: string
97 displayName: string
98 url: string
99 host: string
100
101 // TODO: remove, deprecated in 4.2
102 avatar: ActorImage
103
104 avatars: ActorImage[]
105 }
106
107 userHistory?: {
108 currentTime: number
109 }
110
111 pluginData?: any
112
113 streamingPlaylists?: VideoStreamingPlaylist[]
114 files?: VideoFile[]
115
116 static buildWatchUrl (video: Partial<Pick<Video, 'uuid' | 'shortUUID'>>) {
117 return buildVideoWatchPath({ shortUUID: video.shortUUID || video.uuid })
118 }
119
120 static buildUpdateUrl (video: Pick<Video, 'uuid'>) {
121 return '/videos/update/' + video.uuid
122 }
123
124 constructor (hash: VideoServerModel, translations: { [ id: string ]: string } = {}) {
125 const absoluteAPIUrl = getAbsoluteAPIUrl()
126
127 this.createdAt = new Date(hash.createdAt.toString())
128 this.publishedAt = new Date(hash.publishedAt.toString())
129 this.category = hash.category
130 this.licence = hash.licence
131 this.language = hash.language
132 this.privacy = hash.privacy
133 this.waitTranscoding = hash.waitTranscoding
134 this.state = hash.state
135 this.description = hash.description
136
137 this.isLive = hash.isLive
138
139 this.duration = hash.duration
140 this.durationLabel = durationToString(hash.duration)
141
142 this.id = hash.id
143 this.uuid = hash.uuid
144 this.shortUUID = hash.shortUUID
145
146 this.isLocal = hash.isLocal
147 this.name = hash.name
148
149 this.thumbnailPath = hash.thumbnailPath
150 this.thumbnailUrl = this.thumbnailPath
151 ? hash.thumbnailUrl || (absoluteAPIUrl + hash.thumbnailPath)
152 : null
153
154 this.previewPath = hash.previewPath
155 this.previewUrl = this.previewPath
156 ? hash.previewUrl || (absoluteAPIUrl + hash.previewPath)
157 : null
158
159 this.embedPath = hash.embedPath
160 this.embedUrl = hash.embedUrl || (getAbsoluteEmbedUrl() + hash.embedPath)
161
162 this.url = hash.url
163
164 this.views = hash.views
165 this.viewers = hash.viewers
166 this.likes = hash.likes
167 this.dislikes = hash.dislikes
168
169 this.nsfw = hash.nsfw
170
171 this.account = hash.account
172 this.channel = hash.channel
173
174 this.byAccount = Actor.CREATE_BY_STRING(hash.account.name, hash.account.host)
175 this.byVideoChannel = Actor.CREATE_BY_STRING(hash.channel.name, hash.channel.host)
176
177 this.category.label = peertubeTranslate(this.category.label, translations)
178 this.licence.label = peertubeTranslate(this.licence.label, translations)
179 this.language.label = peertubeTranslate(this.language.label, translations)
180 this.privacy.label = peertubeTranslate(this.privacy.label, translations)
181
182 this.scheduledUpdate = hash.scheduledUpdate
183 this.originallyPublishedAt = hash.originallyPublishedAt ? new Date(hash.originallyPublishedAt.toString()) : null
184
185 if (this.state) this.state.label = peertubeTranslate(this.state.label, translations)
186
187 this.blacklisted = hash.blacklisted
188 this.blacklistedReason = hash.blacklistedReason
189
190 this.blockedOwner = hash.blockedOwner
191 this.blockedServer = hash.blockedServer
192
193 this.streamingPlaylists = hash.streamingPlaylists
194 this.files = hash.files
195
196 this.userHistory = hash.userHistory
197
198 this.originInstanceHost = this.account.host
199 this.originInstanceUrl = 'https://' + this.originInstanceHost
200
201 this.pluginData = hash.pluginData
202 }
203
204 isVideoNSFWForUser (user: User, serverConfig: HTMLServerConfig) {
205 // Video is not NSFW, skip
206 if (this.nsfw === false) return false
207
208 // Return user setting if logged in
209 if (user) return user.nsfwPolicy !== 'display'
210
211 // Return default instance config
212 return serverConfig.instance.defaultNSFWPolicy !== 'display'
213 }
214
215 isRemovableBy (user: AuthUser) {
216 return user && this.isLocal === true && (this.account.name === user.username || user.hasRight(UserRight.REMOVE_ANY_VIDEO))
217 }
218
219 isBlockableBy (user: AuthUser) {
220 return this.blacklisted !== true && user && user.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) === true
221 }
222
223 isUnblockableBy (user: AuthUser) {
224 return this.blacklisted === true && user && user.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) === true
225 }
226
227 isUpdatableBy (user: AuthUser) {
228 return user && this.isLocal === true && (this.account.name === user.username || user.hasRight(UserRight.UPDATE_ANY_VIDEO))
229 }
230
231 isEditableBy (user: AuthUser, videoStudioEnabled: boolean) {
232 return videoStudioEnabled &&
233 this.state?.id === VideoState.PUBLISHED &&
234 this.isUpdatableBy(user)
235 }
236
237 canRemoveFiles (user: AuthUser) {
238 return this.isLocal &&
239 user.hasRight(UserRight.MANAGE_VIDEO_FILES) &&
240 this.state.id !== VideoState.TO_TRANSCODE &&
241 this.hasHLS() &&
242 this.hasWebTorrent()
243 }
244
245 canRunTranscoding (user: AuthUser) {
246 return this.isLocal &&
247 user.hasRight(UserRight.RUN_VIDEO_TRANSCODING) &&
248 this.state.id !== VideoState.TO_TRANSCODE
249 }
250
251 hasHLS () {
252 return this.streamingPlaylists?.some(p => p.type === VideoStreamingPlaylistType.HLS)
253 }
254
255 hasWebTorrent () {
256 return this.files && this.files.length !== 0
257 }
258
259 isLiveInfoAvailableBy (user: AuthUser) {
260 return this.isLive &&
261 user && this.isLocal === true && (this.account.name === user.username || user.hasRight(UserRight.GET_ANY_LIVE))
262 }
263
264 canBeDuplicatedBy (user: AuthUser) {
265 return user && this.isLocal === false && user.hasRight(UserRight.MANAGE_VIDEOS_REDUNDANCIES)
266 }
267
268 getExactNumberOfViews () {
269 if (this.views < 1000) return ''
270
271 if (this.isLive) {
272 return $localize`${this.views} viewers`
273 }
274
275 return $localize`${this.views} views`
276 }
277 }