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