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