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