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