]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/shared-main/video/video.model.ts
Optimize improve stat card rendering in player
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-main / video / video.model.ts
CommitLineData
3a0fb65c 1import { AuthUser } from '@app/core'
67ed6552 2import { User } from '@app/core/users/user.model'
72493e44 3import { durationToString, getAbsoluteAPIUrl, getAbsoluteEmbedUrl } from '@app/helpers'
69524f6e 4import { Actor } from '@app/shared/shared-main/account/actor.model'
15a7eafb 5import { buildVideoWatchPath } from '@shared/core-utils'
bd45d503 6import { peertubeTranslate } from '@shared/core-utils/i18n'
67ed6552 7import {
f4796856 8 ActorImage,
2989628b 9 HTMLServerConfig,
67ed6552
C
10 UserRight,
11 Video as VideoServerModel,
12 VideoConstant,
3c10840f 13 VideoFile,
67ed6552
C
14 VideoPrivacy,
15 VideoScheduleUpdate,
3c10840f 16 VideoState,
b46cf4b9
C
17 VideoStreamingPlaylist,
18 VideoStreamingPlaylistType
67ed6552 19} from '@shared/models'
92fb909c 20
69f616ab 21export class Video implements VideoServerModel {
22a16e36
C
22 byVideoChannel: string
23 byAccount: string
24
df98563e 25 createdAt: Date
6d33593a 26 updatedAt: Date
2922e048 27 publishedAt: Date
c8034165 28 originallyPublishedAt: Date | string
09700934
C
29 category: VideoConstant<number>
30 licence: VideoConstant<number>
9d3ef9fe 31 language: VideoConstant<string>
2243730c 32 privacy: VideoConstant<VideoPrivacy>
d4a8e7a6 33
df98563e 34 description: string
d4a8e7a6 35
df98563e
C
36 duration: number
37 durationLabel: string
d4a8e7a6 38
0a6658fd
C
39 id: number
40 uuid: string
d4a8e7a6
C
41 shortUUID: string
42
df98563e 43 isLocal: boolean
d4a8e7a6 44
df98563e 45 name: string
60862425 46 serverHost: string
df98563e
C
47 thumbnailPath: string
48 thumbnailUrl: string
5fb2e288 49
c6c0fa6c
C
50 isLive: boolean
51
43f61d26
C
52 previewPath: string
53 previewUrl: string
5fb2e288 54
d8755eed
C
55 embedPath: string
56 embedUrl: string
5fb2e288 57
ab4001aa 58 url: string
5fb2e288 59
df98563e 60 views: number
51353d9a
C
61 // If live
62 viewers?: number
63
df98563e
C
64 likes: number
65 dislikes: number
66 nsfw: boolean
b64c950a 67
c910f667
C
68 originInstanceUrl: string
69 originInstanceHost: string
70
2186386c
C
71 waitTranscoding?: boolean
72 state?: VideoConstant<VideoState>
bbe0f064 73 scheduledUpdate?: VideoScheduleUpdate
2760b454 74
191764f3 75 blacklisted?: boolean
2760b454
C
76 blacklistedReason?: string
77
78 blockedOwner?: boolean
79 blockedServer?: boolean
2186386c 80
b64c950a 81 account: {
03e12d7c 82 id: number
b64c950a
C
83 name: string
84 displayName: string
85 url: string
0f320037 86 host: string
d0800f76 87
88 // TODO: remove, deprecated in 4.2
89 avatar: ActorImage
90
91 avatars: ActorImage[]
0f320037
C
92 }
93
94 channel: {
95 id: number
0f320037
C
96 name: string
97 displayName: string
98 url: string
b64c950a 99 host: string
d0800f76 100
101 // TODO: remove, deprecated in 4.2
102 avatar: ActorImage
103
104 avatars: ActorImage[]
b64c950a 105 }
aff038cd 106
6e46de09
C
107 userHistory?: {
108 currentTime: number
109 }
110
7294aab0
C
111 pluginData?: any
112
3c10840f
C
113 streamingPlaylists?: VideoStreamingPlaylist[]
114 files?: VideoFile[]
115
d4a8e7a6 116 static buildWatchUrl (video: Partial<Pick<Video, 'uuid' | 'shortUUID'>>) {
15a7eafb 117 return buildVideoWatchPath({ shortUUID: video.shortUUID || video.uuid })
d4a8e7a6
C
118 }
119
120 static buildUpdateUrl (video: Pick<Video, 'uuid'>) {
121 return '/videos/update/' + video.uuid
191764f3
C
122 }
123
9df52d66 124 constructor (hash: VideoServerModel, translations: { [ id: string ]: string } = {}) {
c5911fd3 125 const absoluteAPIUrl = getAbsoluteAPIUrl()
c6e0bfbf 126
d592e0a9 127 this.createdAt = new Date(hash.createdAt.toString())
2922e048 128 this.publishedAt = new Date(hash.publishedAt.toString())
df98563e 129 this.category = hash.category
df98563e 130 this.licence = hash.licence
df98563e 131 this.language = hash.language
2243730c 132 this.privacy = hash.privacy
2186386c
C
133 this.waitTranscoding = hash.waitTranscoding
134 this.state = hash.state
df98563e 135 this.description = hash.description
c910f667 136
c6c0fa6c
C
137 this.isLive = hash.isLive
138
df98563e 139 this.duration = hash.duration
11b8762f 140 this.durationLabel = durationToString(hash.duration)
c910f667 141
df98563e 142 this.id = hash.id
0a6658fd 143 this.uuid = hash.uuid
d4a8e7a6 144 this.shortUUID = hash.shortUUID
c910f667 145
df98563e 146 this.isLocal = hash.isLocal
df98563e 147 this.name = hash.name
c910f667 148
df98563e 149 this.thumbnailPath = hash.thumbnailPath
c6c0fa6c
C
150 this.thumbnailUrl = this.thumbnailPath
151 ? hash.thumbnailUrl || (absoluteAPIUrl + hash.thumbnailPath)
152 : null
c910f667 153
43f61d26 154 this.previewPath = hash.previewPath
c6c0fa6c
C
155 this.previewUrl = this.previewPath
156 ? hash.previewUrl || (absoluteAPIUrl + hash.previewPath)
157 : null
c910f667 158
d8755eed 159 this.embedPath = hash.embedPath
72493e44 160 this.embedUrl = hash.embedUrl || (getAbsoluteEmbedUrl() + hash.embedPath)
5fb2e288
C
161
162 this.url = hash.url
c910f667 163
df98563e 164 this.views = hash.views
51353d9a 165 this.viewers = hash.viewers
df98563e
C
166 this.likes = hash.likes
167 this.dislikes = hash.dislikes
c910f667 168
df98563e 169 this.nsfw = hash.nsfw
c910f667 170
b64c950a 171 this.account = hash.account
52d9f792 172 this.channel = hash.channel
4fd8aa32 173
22a16e36
C
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)
7ce44a74
C
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)
2186386c 181
bbe0f064 182 this.scheduledUpdate = hash.scheduledUpdate
830b4faf
C
183 this.originallyPublishedAt = hash.originallyPublishedAt ? new Date(hash.originallyPublishedAt.toString()) : null
184
2186386c 185 if (this.state) this.state.label = peertubeTranslate(this.state.label, translations)
191764f3
C
186
187 this.blacklisted = hash.blacklisted
2760b454
C
188 this.blacklistedReason = hash.blacklistedReason
189
190 this.blockedOwner = hash.blockedOwner
191 this.blockedServer = hash.blockedServer
6e46de09 192
3c10840f
C
193 this.streamingPlaylists = hash.streamingPlaylists
194 this.files = hash.files
195
6e46de09 196 this.userHistory = hash.userHistory
c910f667
C
197
198 this.originInstanceHost = this.account.host
199 this.originInstanceUrl = 'https://' + this.originInstanceHost
7294aab0
C
200
201 this.pluginData = hash.pluginData
501bc6c2
C
202 }
203
2989628b 204 isVideoNSFWForUser (user: User, serverConfig: HTMLServerConfig) {
0883b324
C
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'
92fb909c 213 }
3a0fb65c
C
214
215 isRemovableBy (user: AuthUser) {
216 return user && this.isLocal === true && (this.account.name === user.username || user.hasRight(UserRight.REMOVE_ANY_VIDEO))
217 }
218
5baee5fc 219 isBlockableBy (user: AuthUser) {
3487330d 220 return this.blacklisted !== true && user && user.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) === true
3a0fb65c
C
221 }
222
5baee5fc 223 isUnblockableBy (user: AuthUser) {
3487330d 224 return this.blacklisted === true && user && user.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) === true
3a0fb65c
C
225 }
226
227 isUpdatableBy (user: AuthUser) {
228 return user && this.isLocal === true && (this.account.name === user.username || user.hasRight(UserRight.UPDATE_ANY_VIDEO))
229 }
b764380a 230
ad5db104 231 canRemoveFiles (user: AuthUser) {
7aeb82ea
C
232 return this.isLocal &&
233 user.hasRight(UserRight.MANAGE_VIDEO_FILES) &&
ad5db104
C
234 this.state.id !== VideoState.TO_TRANSCODE &&
235 this.hasHLS() &&
236 this.hasWebTorrent()
237 }
238
239 canRunTranscoding (user: AuthUser) {
7aeb82ea
C
240 return this.isLocal &&
241 user.hasRight(UserRight.RUN_VIDEO_TRANSCODING) &&
ad5db104
C
242 this.state.id !== VideoState.TO_TRANSCODE
243 }
244
b46cf4b9
C
245 hasHLS () {
246 return this.streamingPlaylists?.some(p => p.type === VideoStreamingPlaylistType.HLS)
247 }
248
249 hasWebTorrent () {
250 return this.files && this.files.length !== 0
251 }
252
f8c00564
C
253 isLiveInfoAvailableBy (user: AuthUser) {
254 return this.isLive &&
255 user && this.isLocal === true && (this.account.name === user.username || user.hasRight(UserRight.GET_ANY_LIVE))
256 }
257
b764380a
C
258 canBeDuplicatedBy (user: AuthUser) {
259 return user && this.isLocal === false && user.hasRight(UserRight.MANAGE_VIDEOS_REDUNDANCIES)
260 }
4ec25ae8
C
261
262 getExactNumberOfViews () {
263 if (this.views < 1000) return ''
264
265 if (this.isLive) {
266 return $localize`${this.views} viewers`
267 }
268
269 return $localize`${this.views} views`
270 }
501bc6c2 271}