]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/shared-video-miniature/video-miniature.component.ts
Reorder playlists when adding an element
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-video-miniature / video-miniature.component.ts
1 import { switchMap } from 'rxjs/operators'
2 import {
3 ChangeDetectionStrategy,
4 ChangeDetectorRef,
5 Component,
6 EventEmitter,
7 Inject,
8 Input,
9 LOCALE_ID,
10 OnInit,
11 Output
12 } from '@angular/core'
13 import { AuthService, ScreenService, ServerService, User } from '@app/core'
14 import { HTMLServerConfig, VideoExistInPlaylist, VideoPlaylistType, VideoPrivacy, VideoState } from '@shared/models'
15 import { LinkType } from '../../../types/link.type'
16 import { ActorAvatarSize } from '../shared-actor-image/actor-avatar.component'
17 import { Video } from '../shared-main'
18 import { VideoPlaylistService } from '../shared-video-playlist'
19 import { VideoActionsDisplayType } from './video-actions-dropdown.component'
20
21 export type MiniatureDisplayOptions = {
22 date?: boolean
23 views?: boolean
24 avatar?: boolean
25 privacyLabel?: boolean
26 privacyText?: boolean
27 state?: boolean
28 blacklistInfo?: boolean
29 nsfw?: boolean
30
31 by?: boolean
32 forceChannelInBy?: boolean
33 }
34 @Component({
35 selector: 'my-video-miniature',
36 styleUrls: [ './video-miniature.component.scss' ],
37 templateUrl: './video-miniature.component.html',
38 changeDetection: ChangeDetectionStrategy.OnPush
39 })
40 export class VideoMiniatureComponent implements OnInit {
41 @Input() user: User
42 @Input() video: Video
43 @Input() containedInPlaylists: VideoExistInPlaylist[]
44
45 @Input() displayOptions: MiniatureDisplayOptions = {
46 date: true,
47 views: true,
48 by: true,
49 avatar: false,
50 privacyLabel: false,
51 privacyText: false,
52 state: false,
53 blacklistInfo: false,
54 forceChannelInBy: false
55 }
56
57 @Input() displayVideoActions = true
58 @Input() videoActionsDisplayOptions: VideoActionsDisplayType = {
59 playlist: true,
60 download: false,
61 update: true,
62 blacklist: true,
63 delete: true,
64 report: true,
65 duplicate: true,
66 mute: true,
67 studio: false,
68 stats: false
69 }
70
71 @Input() actorImageSize: ActorAvatarSize = '40'
72
73 @Input() displayAsRow = false
74
75 @Input() videoLinkType: LinkType = 'internal'
76
77 @Output() videoBlocked = new EventEmitter()
78 @Output() videoUnblocked = new EventEmitter()
79 @Output() videoRemoved = new EventEmitter()
80 @Output() videoAccountMuted = new EventEmitter()
81
82 showActions = false
83 serverConfig: HTMLServerConfig
84
85 addToWatchLaterText: string
86 addedToWatchLaterText: string
87 inWatchLaterPlaylist: boolean
88 channelLinkTitle = ''
89
90 watchLaterPlaylist: {
91 id: number
92 playlistElementId?: number
93 }
94
95 videoRouterLink: string | any[] = []
96 videoHref: string
97 videoTarget: string
98
99 private ownerDisplayType: 'account' | 'videoChannel'
100
101 constructor (
102 private screenService: ScreenService,
103 private serverService: ServerService,
104 private authService: AuthService,
105 private videoPlaylistService: VideoPlaylistService,
106 private cd: ChangeDetectorRef,
107 @Inject(LOCALE_ID) private localeId: string
108 ) {}
109
110 get authorAccount () {
111 return this.serverConfig.client.videos.miniature.preferAuthorDisplayName
112 ? this.video.account.displayName
113 : this.video.byAccount
114 }
115
116 get authorChannel () {
117 return this.serverConfig.client.videos.miniature.preferAuthorDisplayName
118 ? this.video.channel.displayName
119 : this.video.byVideoChannel
120 }
121
122 get isVideoBlur () {
123 return this.video.isVideoNSFWForUser(this.user, this.serverConfig)
124 }
125
126 ngOnInit () {
127 this.serverConfig = this.serverService.getHTMLConfig()
128 this.buildVideoLink()
129
130 this.setUpBy()
131
132 this.channelLinkTitle = $localize`${this.video.channel.name} (channel page)`
133
134 // We rely on mouseenter to lazy load actions
135 if (this.screenService.isInTouchScreen()) {
136 this.loadActions()
137 }
138 }
139
140 buildVideoLink () {
141 if (this.videoLinkType === 'internal' || !this.video.url) {
142 this.videoRouterLink = Video.buildWatchUrl(this.video)
143 return
144 }
145
146 if (this.videoLinkType === 'external') {
147 this.videoRouterLink = null
148 this.videoHref = this.video.url
149 this.videoTarget = '_blank'
150 return
151 }
152
153 // Lazy load
154 this.videoRouterLink = [ '/search/lazy-load-video', { url: this.video.url } ]
155 }
156
157 displayOwnerAccount () {
158 return this.ownerDisplayType === 'account'
159 }
160
161 displayOwnerVideoChannel () {
162 return this.ownerDisplayType === 'videoChannel'
163 }
164
165 isUnlistedVideo () {
166 return this.video.privacy.id === VideoPrivacy.UNLISTED
167 }
168
169 isPrivateVideo () {
170 return this.video.privacy.id === VideoPrivacy.PRIVATE
171 }
172
173 getStateLabel (video: Video) {
174 if (!video.state) return ''
175
176 if (video.privacy.id !== VideoPrivacy.PRIVATE && video.state.id === VideoState.PUBLISHED) {
177 return $localize`Published`
178 }
179
180 if (video.scheduledUpdate) {
181 const updateAt = new Date(video.scheduledUpdate.updateAt.toString()).toLocaleString(this.localeId)
182 return $localize`Publication scheduled on ${updateAt}`
183 }
184
185 if (video.state.id === VideoState.TRANSCODING_FAILED) {
186 return $localize`Transcoding failed`
187 }
188
189 if (video.state.id === VideoState.TO_MOVE_TO_EXTERNAL_STORAGE_FAILED) {
190 return $localize`Move to external storage failed`
191 }
192
193 if (video.state.id === VideoState.TO_TRANSCODE && video.waitTranscoding === true) {
194 return $localize`Waiting transcoding`
195 }
196
197 if (video.state.id === VideoState.TO_TRANSCODE) {
198 return $localize`To transcode`
199 }
200
201 if (video.state.id === VideoState.TO_IMPORT) {
202 return $localize`To import`
203 }
204
205 if (video.state.id === VideoState.TO_EDIT) {
206 return $localize`To edit`
207 }
208
209 return ''
210 }
211
212 loadActions () {
213 if (this.displayVideoActions) this.showActions = true
214
215 this.loadWatchLater()
216 }
217
218 onVideoBlocked () {
219 this.videoBlocked.emit()
220 }
221
222 onVideoUnblocked () {
223 this.videoUnblocked.emit()
224 }
225
226 onVideoRemoved () {
227 this.videoRemoved.emit()
228 }
229
230 onVideoAccountMuted () {
231 this.videoAccountMuted.emit()
232 }
233
234 isUserLoggedIn () {
235 return this.authService.isLoggedIn()
236 }
237
238 onWatchLaterClick (currentState: boolean) {
239 if (currentState === true) this.removeFromWatchLater()
240 else this.addToWatchLater()
241
242 this.inWatchLaterPlaylist = !currentState
243 }
244
245 addToWatchLater () {
246 const body = { videoId: this.video.id }
247
248 this.videoPlaylistService.addVideoInPlaylist(this.watchLaterPlaylist.id, body)
249 .subscribe(
250 res => {
251 this.watchLaterPlaylist.playlistElementId = res.videoPlaylistElement.id
252 }
253 )
254 }
255
256 removeFromWatchLater () {
257 this.videoPlaylistService.removeVideoFromPlaylist(this.watchLaterPlaylist.id, this.watchLaterPlaylist.playlistElementId, this.video.id)
258 .subscribe(
259 _ => { /* empty */ }
260 )
261 }
262
263 isWatchLaterPlaylistDisplayed () {
264 return this.displayVideoActions && this.isUserLoggedIn() && this.inWatchLaterPlaylist !== undefined
265 }
266
267 getClasses () {
268 return {
269 'display-as-row': this.displayAsRow
270 }
271 }
272
273 private setUpBy () {
274 if (this.displayOptions.forceChannelInBy) {
275 this.ownerDisplayType = 'videoChannel'
276 return
277 }
278
279 const accountName = this.video.account.name
280
281 // If the video channel name is an UUID (not really displayable, we changed this behaviour in v1.0.0-beta.12)
282 // Or has not been customized (default created channel display name)
283 // -> Use the account name
284 if (
285 this.video.channel.displayName === `Default ${accountName} channel` ||
286 this.video.channel.displayName === `Main ${accountName} channel` ||
287 this.video.channel.name.match(/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/)
288 ) {
289 this.ownerDisplayType = 'account'
290 } else {
291 this.ownerDisplayType = 'videoChannel'
292 }
293 }
294
295 private loadWatchLater () {
296 if (!this.isUserLoggedIn() || this.inWatchLaterPlaylist !== undefined) return
297
298 this.authService.userInformationLoaded
299 .pipe(switchMap(() => this.videoPlaylistService.listenToVideoPlaylistChange(this.video.id)))
300 .subscribe(existResult => {
301 const watchLaterPlaylist = this.authService.getUser().specialPlaylists.find(p => p.type === VideoPlaylistType.WATCH_LATER)
302 const existsInWatchLater = existResult.find(r => r.playlistId === watchLaterPlaylist.id)
303 this.inWatchLaterPlaylist = false
304
305 this.watchLaterPlaylist = {
306 id: watchLaterPlaylist.id
307 }
308
309 if (existsInWatchLater) {
310 this.inWatchLaterPlaylist = true
311 this.watchLaterPlaylist.playlistElementId = existsInWatchLater.playlistElementId
312 }
313
314 this.cd.markForCheck()
315 })
316
317 this.videoPlaylistService.runVideoExistsInPlaylistCheck(this.video.id)
318 }
319 }