]>
Commit | Line | Data |
---|---|---|
3a0fb65c | 1 | import { AuthUser } from '@app/core' |
67ed6552 | 2 | import { User } from '@app/core/users/user.model' |
367a9dc6 | 3 | import { durationToString, getAbsoluteAPIUrl, getAbsoluteEmbedUrl, prepareIcu } from '@app/helpers' |
69524f6e | 4 | import { Actor } from '@app/shared/shared-main/account/actor.model' |
367a9dc6 | 5 | import { buildVideoWatchPath, getAllFiles } from '@shared/core-utils' |
bd45d503 | 6 | import { peertubeTranslate } from '@shared/core-utils/i18n' |
67ed6552 | 7 | import { |
f4796856 | 8 | ActorImage, |
2989628b | 9 | HTMLServerConfig, |
67ed6552 C |
10 | UserRight, |
11 | Video as VideoServerModel, | |
12 | VideoConstant, | |
3c10840f | 13 | VideoFile, |
67ed6552 C |
14 | VideoPrivacy, |
15 | VideoScheduleUpdate, | |
3c10840f | 16 | VideoState, |
b46cf4b9 C |
17 | VideoStreamingPlaylist, |
18 | VideoStreamingPlaylistType | |
67ed6552 | 19 | } from '@shared/models' |
92fb909c | 20 | |
69f616ab | 21 | export class Video implements VideoServerModel { |
eaa52952 C |
22 | private static readonly viewsICU = prepareIcu($localize`{views, plural, =0 {No view} =1 {1 view} other {{views} views}}`) |
23 | private static readonly viewersICU = prepareIcu($localize`{viewers, plural, =0 {No viewers} =1 {1 viewer} other {{viewers} viewers}}`) | |
24 | ||
22a16e36 C |
25 | byVideoChannel: string |
26 | byAccount: string | |
27 | ||
df98563e | 28 | createdAt: Date |
6d33593a | 29 | updatedAt: Date |
2922e048 | 30 | publishedAt: Date |
c8034165 | 31 | originallyPublishedAt: Date | string |
09700934 C |
32 | category: VideoConstant<number> |
33 | licence: VideoConstant<number> | |
9d3ef9fe | 34 | language: VideoConstant<string> |
2243730c | 35 | privacy: VideoConstant<VideoPrivacy> |
d4a8e7a6 | 36 | |
f713f36b | 37 | truncatedDescription: string |
df98563e | 38 | description: string |
d4a8e7a6 | 39 | |
df98563e C |
40 | duration: number |
41 | durationLabel: string | |
d4a8e7a6 | 42 | |
0a6658fd C |
43 | id: number |
44 | uuid: string | |
d4a8e7a6 C |
45 | shortUUID: string |
46 | ||
df98563e | 47 | isLocal: boolean |
d4a8e7a6 | 48 | |
df98563e | 49 | name: string |
60862425 | 50 | serverHost: string |
df98563e C |
51 | thumbnailPath: string |
52 | thumbnailUrl: string | |
5fb2e288 | 53 | |
c6c0fa6c C |
54 | isLive: boolean |
55 | ||
43f61d26 C |
56 | previewPath: string |
57 | previewUrl: string | |
5fb2e288 | 58 | |
d8755eed C |
59 | embedPath: string |
60 | embedUrl: string | |
5fb2e288 | 61 | |
ab4001aa | 62 | url: string |
5fb2e288 | 63 | |
df98563e | 64 | views: number |
384ba8b7 | 65 | viewers: number |
51353d9a | 66 | |
df98563e C |
67 | likes: number |
68 | dislikes: number | |
69 | nsfw: boolean | |
b64c950a | 70 | |
c910f667 C |
71 | originInstanceUrl: string |
72 | originInstanceHost: string | |
73 | ||
2186386c C |
74 | waitTranscoding?: boolean |
75 | state?: VideoConstant<VideoState> | |
bbe0f064 | 76 | scheduledUpdate?: VideoScheduleUpdate |
2760b454 | 77 | |
191764f3 | 78 | blacklisted?: boolean |
2760b454 C |
79 | blacklistedReason?: string |
80 | ||
81 | blockedOwner?: boolean | |
82 | blockedServer?: boolean | |
2186386c | 83 | |
b64c950a | 84 | account: { |
03e12d7c | 85 | id: number |
b64c950a C |
86 | name: string |
87 | displayName: string | |
88 | url: string | |
0f320037 | 89 | host: string |
d0800f76 | 90 | |
91 | // TODO: remove, deprecated in 4.2 | |
92 | avatar: ActorImage | |
93 | ||
94 | avatars: ActorImage[] | |
0f320037 C |
95 | } |
96 | ||
97 | channel: { | |
98 | id: number | |
0f320037 C |
99 | name: string |
100 | displayName: string | |
101 | url: string | |
b64c950a | 102 | host: string |
d0800f76 | 103 | |
104 | // TODO: remove, deprecated in 4.2 | |
105 | avatar: ActorImage | |
106 | ||
107 | avatars: ActorImage[] | |
b64c950a | 108 | } |
aff038cd | 109 | |
6e46de09 C |
110 | userHistory?: { |
111 | currentTime: number | |
112 | } | |
113 | ||
7294aab0 C |
114 | pluginData?: any |
115 | ||
3c10840f C |
116 | streamingPlaylists?: VideoStreamingPlaylist[] |
117 | files?: VideoFile[] | |
118 | ||
d4a8e7a6 | 119 | static buildWatchUrl (video: Partial<Pick<Video, 'uuid' | 'shortUUID'>>) { |
15a7eafb | 120 | return buildVideoWatchPath({ shortUUID: video.shortUUID || video.uuid }) |
d4a8e7a6 C |
121 | } |
122 | ||
123 | static buildUpdateUrl (video: Pick<Video, 'uuid'>) { | |
124 | return '/videos/update/' + video.uuid | |
191764f3 C |
125 | } |
126 | ||
9df52d66 | 127 | constructor (hash: VideoServerModel, translations: { [ id: string ]: string } = {}) { |
c5911fd3 | 128 | const absoluteAPIUrl = getAbsoluteAPIUrl() |
c6e0bfbf | 129 | |
d592e0a9 | 130 | this.createdAt = new Date(hash.createdAt.toString()) |
2922e048 | 131 | this.publishedAt = new Date(hash.publishedAt.toString()) |
df98563e | 132 | this.category = hash.category |
df98563e | 133 | this.licence = hash.licence |
df98563e | 134 | this.language = hash.language |
2243730c | 135 | this.privacy = hash.privacy |
2186386c C |
136 | this.waitTranscoding = hash.waitTranscoding |
137 | this.state = hash.state | |
f713f36b C |
138 | |
139 | this.truncatedDescription = hash.truncatedDescription | |
df98563e | 140 | this.description = hash.description |
c910f667 | 141 | |
c6c0fa6c C |
142 | this.isLive = hash.isLive |
143 | ||
df98563e | 144 | this.duration = hash.duration |
11b8762f | 145 | this.durationLabel = durationToString(hash.duration) |
c910f667 | 146 | |
df98563e | 147 | this.id = hash.id |
0a6658fd | 148 | this.uuid = hash.uuid |
d4a8e7a6 | 149 | this.shortUUID = hash.shortUUID |
c910f667 | 150 | |
df98563e | 151 | this.isLocal = hash.isLocal |
df98563e | 152 | this.name = hash.name |
c910f667 | 153 | |
df98563e | 154 | this.thumbnailPath = hash.thumbnailPath |
c6c0fa6c C |
155 | this.thumbnailUrl = this.thumbnailPath |
156 | ? hash.thumbnailUrl || (absoluteAPIUrl + hash.thumbnailPath) | |
157 | : null | |
c910f667 | 158 | |
43f61d26 | 159 | this.previewPath = hash.previewPath |
c6c0fa6c C |
160 | this.previewUrl = this.previewPath |
161 | ? hash.previewUrl || (absoluteAPIUrl + hash.previewPath) | |
162 | : null | |
c910f667 | 163 | |
d8755eed | 164 | this.embedPath = hash.embedPath |
72493e44 | 165 | this.embedUrl = hash.embedUrl || (getAbsoluteEmbedUrl() + hash.embedPath) |
5fb2e288 C |
166 | |
167 | this.url = hash.url | |
c910f667 | 168 | |
df98563e | 169 | this.views = hash.views |
51353d9a | 170 | this.viewers = hash.viewers |
df98563e C |
171 | this.likes = hash.likes |
172 | this.dislikes = hash.dislikes | |
c910f667 | 173 | |
df98563e | 174 | this.nsfw = hash.nsfw |
c910f667 | 175 | |
b64c950a | 176 | this.account = hash.account |
52d9f792 | 177 | this.channel = hash.channel |
4fd8aa32 | 178 | |
22a16e36 C |
179 | this.byAccount = Actor.CREATE_BY_STRING(hash.account.name, hash.account.host) |
180 | this.byVideoChannel = Actor.CREATE_BY_STRING(hash.channel.name, hash.channel.host) | |
7ce44a74 C |
181 | |
182 | this.category.label = peertubeTranslate(this.category.label, translations) | |
183 | this.licence.label = peertubeTranslate(this.licence.label, translations) | |
184 | this.language.label = peertubeTranslate(this.language.label, translations) | |
185 | this.privacy.label = peertubeTranslate(this.privacy.label, translations) | |
2186386c | 186 | |
bbe0f064 | 187 | this.scheduledUpdate = hash.scheduledUpdate |
830b4faf C |
188 | this.originallyPublishedAt = hash.originallyPublishedAt ? new Date(hash.originallyPublishedAt.toString()) : null |
189 | ||
2186386c | 190 | if (this.state) this.state.label = peertubeTranslate(this.state.label, translations) |
191764f3 C |
191 | |
192 | this.blacklisted = hash.blacklisted | |
2760b454 C |
193 | this.blacklistedReason = hash.blacklistedReason |
194 | ||
195 | this.blockedOwner = hash.blockedOwner | |
196 | this.blockedServer = hash.blockedServer | |
6e46de09 | 197 | |
3c10840f C |
198 | this.streamingPlaylists = hash.streamingPlaylists |
199 | this.files = hash.files | |
200 | ||
6e46de09 | 201 | this.userHistory = hash.userHistory |
c910f667 C |
202 | |
203 | this.originInstanceHost = this.account.host | |
204 | this.originInstanceUrl = 'https://' + this.originInstanceHost | |
7294aab0 C |
205 | |
206 | this.pluginData = hash.pluginData | |
501bc6c2 C |
207 | } |
208 | ||
2989628b | 209 | isVideoNSFWForUser (user: User, serverConfig: HTMLServerConfig) { |
0883b324 C |
210 | // Video is not NSFW, skip |
211 | if (this.nsfw === false) return false | |
212 | ||
213 | // Return user setting if logged in | |
214 | if (user) return user.nsfwPolicy !== 'display' | |
215 | ||
216 | // Return default instance config | |
217 | return serverConfig.instance.defaultNSFWPolicy !== 'display' | |
92fb909c | 218 | } |
3a0fb65c C |
219 | |
220 | isRemovableBy (user: AuthUser) { | |
221 | return user && this.isLocal === true && (this.account.name === user.username || user.hasRight(UserRight.REMOVE_ANY_VIDEO)) | |
222 | } | |
223 | ||
5baee5fc | 224 | isBlockableBy (user: AuthUser) { |
3487330d | 225 | return this.blacklisted !== true && user && user.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) === true |
3a0fb65c C |
226 | } |
227 | ||
5baee5fc | 228 | isUnblockableBy (user: AuthUser) { |
3487330d | 229 | return this.blacklisted === true && user && user.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) === true |
3a0fb65c C |
230 | } |
231 | ||
232 | isUpdatableBy (user: AuthUser) { | |
233 | return user && this.isLocal === true && (this.account.name === user.username || user.hasRight(UserRight.UPDATE_ANY_VIDEO)) | |
234 | } | |
b764380a | 235 | |
92e66e04 C |
236 | isEditableBy (user: AuthUser, videoStudioEnabled: boolean) { |
237 | return videoStudioEnabled && | |
22e90922 C |
238 | this.state?.id === VideoState.PUBLISHED && |
239 | this.isUpdatableBy(user) | |
240 | } | |
241 | ||
384ba8b7 C |
242 | canSeeStats (user: AuthUser) { |
243 | return user && this.isLocal === true && (this.account.name === user.username || user.hasRight(UserRight.SEE_ALL_VIDEOS)) | |
244 | } | |
245 | ||
367a9dc6 C |
246 | canRemoveOneFile (user: AuthUser) { |
247 | return this.isLocal && | |
248 | user && user.hasRight(UserRight.MANAGE_VIDEO_FILES) && | |
249 | this.state.id !== VideoState.TO_TRANSCODE && | |
250 | getAllFiles(this).length > 1 | |
251 | } | |
252 | ||
ad5db104 | 253 | canRemoveFiles (user: AuthUser) { |
7aeb82ea | 254 | return this.isLocal && |
384ba8b7 | 255 | user && user.hasRight(UserRight.MANAGE_VIDEO_FILES) && |
ad5db104 C |
256 | this.state.id !== VideoState.TO_TRANSCODE && |
257 | this.hasHLS() && | |
258 | this.hasWebTorrent() | |
259 | } | |
260 | ||
261 | canRunTranscoding (user: AuthUser) { | |
7aeb82ea | 262 | return this.isLocal && |
384ba8b7 | 263 | user && user.hasRight(UserRight.RUN_VIDEO_TRANSCODING) && |
ad5db104 C |
264 | this.state.id !== VideoState.TO_TRANSCODE |
265 | } | |
266 | ||
b46cf4b9 C |
267 | hasHLS () { |
268 | return this.streamingPlaylists?.some(p => p.type === VideoStreamingPlaylistType.HLS) | |
269 | } | |
270 | ||
271 | hasWebTorrent () { | |
272 | return this.files && this.files.length !== 0 | |
273 | } | |
274 | ||
f8c00564 C |
275 | isLiveInfoAvailableBy (user: AuthUser) { |
276 | return this.isLive && | |
277 | user && this.isLocal === true && (this.account.name === user.username || user.hasRight(UserRight.GET_ANY_LIVE)) | |
278 | } | |
279 | ||
b764380a C |
280 | canBeDuplicatedBy (user: AuthUser) { |
281 | return user && this.isLocal === false && user.hasRight(UserRight.MANAGE_VIDEOS_REDUNDANCIES) | |
282 | } | |
4ec25ae8 C |
283 | |
284 | getExactNumberOfViews () { | |
4ec25ae8 | 285 | if (this.isLive) { |
eaa52952 | 286 | return Video.viewersICU({ viewers: this.viewers }, $localize`${this.viewers} viewer(s)`) |
4ec25ae8 C |
287 | } |
288 | ||
eaa52952 | 289 | return Video.viewsICU({ views: this.views }, $localize`{${this.views} view(s)}`) |
4ec25ae8 | 290 | } |
501bc6c2 | 291 | } |