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