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