]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/shared-main/video/video.model.ts
Live streaming implementation first step
[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 { peertubeTranslate } from '@shared/core-utils/i18n'
5 import {
6 Avatar,
7 ServerConfig,
8 UserRight,
9 Video as VideoServerModel,
10 VideoConstant,
11 VideoPrivacy,
12 VideoScheduleUpdate,
13 VideoState
14 } from '@shared/models'
15 import { Actor } from '../account/actor.model'
16
17 export class Video implements VideoServerModel {
18 byVideoChannel: string
19 byAccount: string
20
21 accountAvatarUrl: string
22 videoChannelAvatarUrl: 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 description: string
33 duration: number
34 durationLabel: string
35 id: number
36 uuid: string
37 isLocal: boolean
38 name: string
39 serverHost: string
40 thumbnailPath: string
41 thumbnailUrl: string
42
43 isLive: boolean
44
45 previewPath: string
46 previewUrl: string
47
48 embedPath: string
49 embedUrl: string
50
51 url?: string
52
53 views: number
54 likes: number
55 dislikes: number
56 nsfw: boolean
57
58 originInstanceUrl: string
59 originInstanceHost: string
60
61 waitTranscoding?: boolean
62 state?: VideoConstant<VideoState>
63 scheduledUpdate?: VideoScheduleUpdate
64 blacklisted?: boolean
65 blockedReason?: string
66
67 account: {
68 id: number
69 name: string
70 displayName: string
71 url: string
72 host: string
73 avatar?: Avatar
74 }
75
76 channel: {
77 id: number
78 name: string
79 displayName: string
80 url: string
81 host: string
82 avatar?: Avatar
83 }
84
85 userHistory?: {
86 currentTime: number
87 }
88
89 pluginData?: any
90
91 static buildClientUrl (videoUUID: string) {
92 return '/videos/watch/' + videoUUID
93 }
94
95 constructor (hash: VideoServerModel, translations = {}) {
96 const absoluteAPIUrl = getAbsoluteAPIUrl()
97
98 this.createdAt = new Date(hash.createdAt.toString())
99 this.publishedAt = new Date(hash.publishedAt.toString())
100 this.category = hash.category
101 this.licence = hash.licence
102 this.language = hash.language
103 this.privacy = hash.privacy
104 this.waitTranscoding = hash.waitTranscoding
105 this.state = hash.state
106 this.description = hash.description
107
108 this.isLive = hash.isLive
109
110 this.duration = hash.duration
111 this.durationLabel = durationToString(hash.duration)
112
113 this.id = hash.id
114 this.uuid = hash.uuid
115
116 this.isLocal = hash.isLocal
117 this.name = hash.name
118
119 this.thumbnailPath = hash.thumbnailPath
120 this.thumbnailUrl = this.thumbnailPath
121 ? hash.thumbnailUrl || (absoluteAPIUrl + hash.thumbnailPath)
122 : null
123
124 this.previewPath = hash.previewPath
125 this.previewUrl = this.previewPath
126 ? hash.previewUrl || (absoluteAPIUrl + hash.previewPath)
127 : null
128
129 this.embedPath = hash.embedPath
130 this.embedUrl = hash.embedUrl || (getAbsoluteEmbedUrl() + hash.embedPath)
131
132 this.url = hash.url
133
134 this.views = hash.views
135 this.likes = hash.likes
136 this.dislikes = hash.dislikes
137
138 this.nsfw = hash.nsfw
139
140 this.account = hash.account
141 this.channel = hash.channel
142
143 this.byAccount = Actor.CREATE_BY_STRING(hash.account.name, hash.account.host)
144 this.byVideoChannel = Actor.CREATE_BY_STRING(hash.channel.name, hash.channel.host)
145 this.accountAvatarUrl = Actor.GET_ACTOR_AVATAR_URL(this.account)
146 this.videoChannelAvatarUrl = Actor.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 canBeDuplicatedBy (user: AuthUser) {
197 return user && this.isLocal === false && user.hasRight(UserRight.MANAGE_VIDEOS_REDUNDANCIES)
198 }
199 }