]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/shared-main/video/video.model.ts
Add ability to bulk block videos
[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 } from '@shared/models'
19
20 export class Video implements VideoServerModel {
21 byVideoChannel: string
22 byAccount: string
23
24 createdAt: Date
25 updatedAt: Date
26 publishedAt: Date
27 originallyPublishedAt: Date | string
28 category: VideoConstant<number>
29 licence: VideoConstant<number>
30 language: VideoConstant<string>
31 privacy: VideoConstant<VideoPrivacy>
32
33 description: string
34
35 duration: number
36 durationLabel: string
37
38 id: number
39 uuid: string
40 shortUUID: string
41
42 isLocal: boolean
43
44 name: string
45 serverHost: string
46 thumbnailPath: string
47 thumbnailUrl: string
48
49 isLive: boolean
50
51 previewPath: string
52 previewUrl: string
53
54 embedPath: string
55 embedUrl: string
56
57 url: string
58
59 views: number
60 // If live
61 viewers?: number
62
63 likes: number
64 dislikes: number
65 nsfw: boolean
66
67 originInstanceUrl: string
68 originInstanceHost: string
69
70 waitTranscoding?: boolean
71 state?: VideoConstant<VideoState>
72 scheduledUpdate?: VideoScheduleUpdate
73
74 blacklisted?: boolean
75 blacklistedReason?: string
76
77 blockedOwner?: boolean
78 blockedServer?: boolean
79
80 account: {
81 id: number
82 name: string
83 displayName: string
84 url: string
85 host: string
86 avatar?: ActorImage
87 }
88
89 channel: {
90 id: number
91 name: string
92 displayName: string
93 url: string
94 host: string
95 avatar?: ActorImage
96 }
97
98 userHistory?: {
99 currentTime: number
100 }
101
102 pluginData?: any
103
104 streamingPlaylists?: VideoStreamingPlaylist[]
105 files?: VideoFile[]
106
107 static buildWatchUrl (video: Partial<Pick<Video, 'uuid' | 'shortUUID'>>) {
108 return buildVideoWatchPath({ shortUUID: video.shortUUID || video.uuid })
109 }
110
111 static buildUpdateUrl (video: Pick<Video, 'uuid'>) {
112 return '/videos/update/' + video.uuid
113 }
114
115 constructor (hash: VideoServerModel, translations: { [ id: string ]: string } = {}) {
116 const absoluteAPIUrl = getAbsoluteAPIUrl()
117
118 this.createdAt = new Date(hash.createdAt.toString())
119 this.publishedAt = new Date(hash.publishedAt.toString())
120 this.category = hash.category
121 this.licence = hash.licence
122 this.language = hash.language
123 this.privacy = hash.privacy
124 this.waitTranscoding = hash.waitTranscoding
125 this.state = hash.state
126 this.description = hash.description
127
128 this.isLive = hash.isLive
129
130 this.duration = hash.duration
131 this.durationLabel = durationToString(hash.duration)
132
133 this.id = hash.id
134 this.uuid = hash.uuid
135 this.shortUUID = hash.shortUUID
136
137 this.isLocal = hash.isLocal
138 this.name = hash.name
139
140 this.thumbnailPath = hash.thumbnailPath
141 this.thumbnailUrl = this.thumbnailPath
142 ? hash.thumbnailUrl || (absoluteAPIUrl + hash.thumbnailPath)
143 : null
144
145 this.previewPath = hash.previewPath
146 this.previewUrl = this.previewPath
147 ? hash.previewUrl || (absoluteAPIUrl + hash.previewPath)
148 : null
149
150 this.embedPath = hash.embedPath
151 this.embedUrl = hash.embedUrl || (getAbsoluteEmbedUrl() + hash.embedPath)
152
153 this.url = hash.url
154
155 this.views = hash.views
156 this.viewers = hash.viewers
157 this.likes = hash.likes
158 this.dislikes = hash.dislikes
159
160 this.nsfw = hash.nsfw
161
162 this.account = hash.account
163 this.channel = hash.channel
164
165 this.byAccount = Actor.CREATE_BY_STRING(hash.account.name, hash.account.host)
166 this.byVideoChannel = Actor.CREATE_BY_STRING(hash.channel.name, hash.channel.host)
167
168 this.category.label = peertubeTranslate(this.category.label, translations)
169 this.licence.label = peertubeTranslate(this.licence.label, translations)
170 this.language.label = peertubeTranslate(this.language.label, translations)
171 this.privacy.label = peertubeTranslate(this.privacy.label, translations)
172
173 this.scheduledUpdate = hash.scheduledUpdate
174 this.originallyPublishedAt = hash.originallyPublishedAt ? new Date(hash.originallyPublishedAt.toString()) : null
175
176 if (this.state) this.state.label = peertubeTranslate(this.state.label, translations)
177
178 this.blacklisted = hash.blacklisted
179 this.blacklistedReason = hash.blacklistedReason
180
181 this.blockedOwner = hash.blockedOwner
182 this.blockedServer = hash.blockedServer
183
184 this.streamingPlaylists = hash.streamingPlaylists
185 this.files = hash.files
186
187 this.userHistory = hash.userHistory
188
189 this.originInstanceHost = this.account.host
190 this.originInstanceUrl = 'https://' + this.originInstanceHost
191
192 this.pluginData = hash.pluginData
193 }
194
195 isVideoNSFWForUser (user: User, serverConfig: HTMLServerConfig) {
196 // Video is not NSFW, skip
197 if (this.nsfw === false) return false
198
199 // Return user setting if logged in
200 if (user) return user.nsfwPolicy !== 'display'
201
202 // Return default instance config
203 return serverConfig.instance.defaultNSFWPolicy !== 'display'
204 }
205
206 isRemovableBy (user: AuthUser) {
207 return user && this.isLocal === true && (this.account.name === user.username || user.hasRight(UserRight.REMOVE_ANY_VIDEO))
208 }
209
210 isBlockableBy (user: AuthUser) {
211 return this.blacklisted !== true && user && user.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) === true
212 }
213
214 isUnblockableBy (user: AuthUser) {
215 return this.blacklisted === true && user && user.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) === true
216 }
217
218 isUpdatableBy (user: AuthUser) {
219 return user && this.isLocal === true && (this.account.name === user.username || user.hasRight(UserRight.UPDATE_ANY_VIDEO))
220 }
221
222 isLiveInfoAvailableBy (user: AuthUser) {
223 return this.isLive &&
224 user && this.isLocal === true && (this.account.name === user.username || user.hasRight(UserRight.GET_ANY_LIVE))
225 }
226
227 canBeDuplicatedBy (user: AuthUser) {
228 return user && this.isLocal === false && user.hasRight(UserRight.MANAGE_VIDEOS_REDUNDANCIES)
229 }
230
231 getExactNumberOfViews () {
232 if (this.views < 1000) return ''
233
234 if (this.isLive) {
235 return $localize`${this.views} viewers`
236 }
237
238 return $localize`${this.views} views`
239 }
240 }