1 import { switchMap } from 'rxjs/operators'
3 ChangeDetectionStrategy,
12 } from '@angular/core'
13 import { AuthService, ServerService } from '@app/core'
14 import { ScreenService } from '@app/shared/misc/screen.service'
15 import { VideoPlaylistService } from '@app/shared/video-playlist/video-playlist.service'
16 import { VideoActionsDisplayType } from '@app/shared/video/video-actions-dropdown.component'
17 import { I18n } from '@ngx-translate/i18n-polyfill'
18 import { ServerConfig, VideoPlaylistType, VideoPrivacy, VideoState } from '../../../../../shared'
19 import { User } from '../users'
20 import { Video } from './video.model'
22 export type OwnerDisplayType = 'account' | 'videoChannel' | 'auto'
23 export type MiniatureDisplayOptions = {
28 privacyLabel?: boolean
31 blacklistInfo?: boolean
36 selector: 'my-video-miniature',
37 styleUrls: [ './video-miniature.component.scss' ],
38 templateUrl: './video-miniature.component.html',
39 changeDetection: ChangeDetectionStrategy.OnPush
41 export class VideoMiniatureComponent implements OnInit {
45 @Input() ownerDisplayType: OwnerDisplayType = 'account'
46 @Input() displayOptions: MiniatureDisplayOptions = {
56 @Input() displayAsRow = false
57 @Input() displayVideoActions = true
58 @Input() fitWidth = false
60 @Input() useLazyLoadUrl = false
62 @Output() videoBlocked = new EventEmitter()
63 @Output() videoUnblocked = new EventEmitter()
64 @Output() videoRemoved = new EventEmitter()
66 videoActionsDisplayOptions: VideoActionsDisplayType = {
76 serverConfig: ServerConfig
78 addToWatchLaterText: string
79 addedToWatchLaterText: string
80 inWatchLaterPlaylist: boolean
84 playlistElementId?: number
89 private ownerDisplayTypeChosen: 'account' | 'videoChannel'
92 private screenService: ScreenService,
93 private serverService: ServerService,
95 private authService: AuthService,
96 private videoPlaylistService: VideoPlaylistService,
97 private cd: ChangeDetectorRef,
98 @Inject(LOCALE_ID) private localeId: string
104 return this.video.isVideoNSFWForUser(this.user, this.serverConfig)
108 this.serverConfig = this.serverService.getTmpConfig()
109 this.serverService.getConfig()
110 .subscribe(config => {
111 this.serverConfig = config
112 this.buildVideoLink()
117 // We rely on mouseenter to lazy load actions
118 if (this.screenService.isInTouchScreen()) {
124 if (this.useLazyLoadUrl && this.video.url) {
125 const remoteUriConfig = this.serverConfig.search.remoteUri
127 // Redirect on the external instance if not allowed to fetch remote data
128 const externalRedirect = (!this.authService.isLoggedIn() && !remoteUriConfig.anonymous) || !remoteUriConfig.users
129 const fromPath = window.location.pathname + window.location.search
131 this.videoLink = [ '/search/lazy-load-video', { url: this.video.url, externalRedirect, fromPath } ]
135 this.videoLink = [ '/videos/watch', this.video.uuid ]
138 displayOwnerAccount () {
139 return this.ownerDisplayTypeChosen === 'account'
142 displayOwnerVideoChannel () {
143 return this.ownerDisplayTypeChosen === 'videoChannel'
147 return this.video.privacy.id === VideoPrivacy.UNLISTED
151 return this.video.privacy.id === VideoPrivacy.PRIVATE
154 getStateLabel (video: Video) {
155 if (!video.state) return ''
157 if (video.privacy.id !== VideoPrivacy.PRIVATE && video.state.id === VideoState.PUBLISHED) {
158 return this.i18n('Published')
161 if (video.scheduledUpdate) {
162 const updateAt = new Date(video.scheduledUpdate.updateAt.toString()).toLocaleString(this.localeId)
163 return this.i18n('Publication scheduled on ') + updateAt
166 if (video.state.id === VideoState.TO_TRANSCODE && video.waitTranscoding === true) {
167 return this.i18n('Waiting transcoding')
170 if (video.state.id === VideoState.TO_TRANSCODE) {
171 return this.i18n('To transcode')
174 if (video.state.id === VideoState.TO_IMPORT) {
175 return this.i18n('To import')
182 if (this.displayVideoActions) this.showActions = true
184 this.loadWatchLater()
188 this.videoBlocked.emit()
191 onVideoUnblocked () {
192 this.videoUnblocked.emit()
196 this.videoRemoved.emit()
200 return this.authService.isLoggedIn()
203 onWatchLaterClick (currentState: boolean) {
204 if (currentState === true) this.removeFromWatchLater()
205 else this.addToWatchLater()
207 this.inWatchLaterPlaylist = !currentState
211 const body = { videoId: this.video.id }
213 this.videoPlaylistService.addVideoInPlaylist(this.watchLaterPlaylist.id, body).subscribe(
215 this.watchLaterPlaylist.playlistElementId = res.videoPlaylistElement.id
220 removeFromWatchLater () {
221 this.videoPlaylistService.removeVideoFromPlaylist(this.watchLaterPlaylist.id, this.watchLaterPlaylist.playlistElementId, this.video.id)
227 isWatchLaterPlaylistDisplayed () {
228 return this.displayVideoActions && this.isUserLoggedIn() && this.inWatchLaterPlaylist !== undefined
232 if (this.ownerDisplayType === 'account' || this.ownerDisplayType === 'videoChannel') {
233 this.ownerDisplayTypeChosen = this.ownerDisplayType
237 // If the video channel name an UUID (not really displayable, we changed this behaviour in v1.0.0-beta.12)
238 // -> Use the account name
240 this.video.channel.name === `${this.video.account.name}_channel` ||
241 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}$/)
243 this.ownerDisplayTypeChosen = 'account'
245 this.ownerDisplayTypeChosen = 'videoChannel'
249 private loadWatchLater () {
250 if (!this.isUserLoggedIn() || this.inWatchLaterPlaylist !== undefined) return
252 this.authService.userInformationLoaded
253 .pipe(switchMap(() => this.videoPlaylistService.listenToVideoPlaylistChange(this.video.id)))
254 .subscribe(existResult => {
255 const watchLaterPlaylist = this.authService.getUser().specialPlaylists.find(p => p.type === VideoPlaylistType.WATCH_LATER)
256 const existsInWatchLater = existResult.find(r => r.playlistId === watchLaterPlaylist.id)
257 this.inWatchLaterPlaylist = false
259 this.watchLaterPlaylist = {
260 id: watchLaterPlaylist.id
263 if (existsInWatchLater) {
264 this.inWatchLaterPlaylist = true
265 this.watchLaterPlaylist.playlistElementId = existsInWatchLater.playlistElementId
268 this.cd.markForCheck()
271 this.videoPlaylistService.runPlaylistCheck(this.video.id)