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