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