]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/shared-main/video/video.model.ts
147d0817e2b6aba7cf300300ea0b94f55c20ee53
[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 {
5 Avatar,
6 peertubeTranslate,
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 static buildClientUrl (videoUUID: string) {
88 return '/videos/watch/' + videoUUID
89 }
90
91 constructor (hash: VideoServerModel, translations = {}) {
92 const absoluteAPIUrl = getAbsoluteAPIUrl()
93
94 this.createdAt = new Date(hash.createdAt.toString())
95 this.publishedAt = new Date(hash.publishedAt.toString())
96 this.category = hash.category
97 this.licence = hash.licence
98 this.language = hash.language
99 this.privacy = hash.privacy
100 this.waitTranscoding = hash.waitTranscoding
101 this.state = hash.state
102 this.description = hash.description
103
104 this.duration = hash.duration
105 this.durationLabel = durationToString(hash.duration)
106
107 this.id = hash.id
108 this.uuid = hash.uuid
109
110 this.isLocal = hash.isLocal
111 this.name = hash.name
112
113 this.thumbnailPath = hash.thumbnailPath
114 this.thumbnailUrl = hash.thumbnailUrl || (absoluteAPIUrl + hash.thumbnailPath)
115
116 this.previewPath = hash.previewPath
117 this.previewUrl = hash.previewUrl || (absoluteAPIUrl + hash.previewPath)
118
119 this.embedPath = hash.embedPath
120 this.embedUrl = hash.embedUrl || (getAbsoluteEmbedUrl() + hash.embedPath)
121
122 this.url = hash.url
123
124 this.views = hash.views
125 this.likes = hash.likes
126 this.dislikes = hash.dislikes
127
128 this.nsfw = hash.nsfw
129
130 this.account = hash.account
131 this.channel = hash.channel
132
133 this.byAccount = Actor.CREATE_BY_STRING(hash.account.name, hash.account.host)
134 this.byVideoChannel = Actor.CREATE_BY_STRING(hash.channel.name, hash.channel.host)
135 this.accountAvatarUrl = Actor.GET_ACTOR_AVATAR_URL(this.account)
136 this.videoChannelAvatarUrl = Actor.GET_ACTOR_AVATAR_URL(this.channel)
137
138 this.category.label = peertubeTranslate(this.category.label, translations)
139 this.licence.label = peertubeTranslate(this.licence.label, translations)
140 this.language.label = peertubeTranslate(this.language.label, translations)
141 this.privacy.label = peertubeTranslate(this.privacy.label, translations)
142
143 this.scheduledUpdate = hash.scheduledUpdate
144 this.originallyPublishedAt = hash.originallyPublishedAt ? new Date(hash.originallyPublishedAt.toString()) : null
145
146 if (this.state) this.state.label = peertubeTranslate(this.state.label, translations)
147
148 this.blacklisted = hash.blacklisted
149 this.blockedReason = hash.blacklistedReason
150
151 this.userHistory = hash.userHistory
152
153 this.originInstanceHost = this.account.host
154 this.originInstanceUrl = 'https://' + this.originInstanceHost
155 }
156
157 isVideoNSFWForUser (user: User, serverConfig: ServerConfig) {
158 // Video is not NSFW, skip
159 if (this.nsfw === false) return false
160
161 // Return user setting if logged in
162 if (user) return user.nsfwPolicy !== 'display'
163
164 // Return default instance config
165 return serverConfig.instance.defaultNSFWPolicy !== 'display'
166 }
167
168 isRemovableBy (user: AuthUser) {
169 return user && this.isLocal === true && (this.account.name === user.username || user.hasRight(UserRight.REMOVE_ANY_VIDEO))
170 }
171
172 isBlockableBy (user: AuthUser) {
173 return this.blacklisted !== true && user && user.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) === true
174 }
175
176 isUnblockableBy (user: AuthUser) {
177 return this.blacklisted === true && user && user.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) === true
178 }
179
180 isUpdatableBy (user: AuthUser) {
181 return user && this.isLocal === true && (this.account.name === user.username || user.hasRight(UserRight.UPDATE_ANY_VIDEO))
182 }
183
184 canBeDuplicatedBy (user: AuthUser) {
185 return user && this.isLocal === false && user.hasRight(UserRight.MANAGE_VIDEOS_REDUNDANCIES)
186 }
187 }