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