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