1 import { switchMap } from 'rxjs/operators'
3 ChangeDetectionStrategy,
12 } from '@angular/core'
13 import { AuthService, ScreenService, ServerService, User } from '@app/core'
14 import { I18n } from '@ngx-translate/i18n-polyfill'
15 import { ServerConfig, VideoPlaylistType, VideoPrivacy, VideoState } from '../../../../../shared'
16 import { Video } from '../shared-main'
17 import { VideoPlaylistService } from '../shared-video-playlist'
18 import { VideoActionsDisplayType } from './video-actions-dropdown.component'
20 export type OwnerDisplayType = 'account' | 'videoChannel' | 'auto'
21 export type MiniatureDisplayOptions = {
26 privacyLabel?: boolean
29 blacklistInfo?: boolean
34 selector: 'my-video-miniature',
35 styleUrls: [ './video-miniature.component.scss' ],
36 templateUrl: './video-miniature.component.html',
37 changeDetection: ChangeDetectionStrategy.OnPush
39 export class VideoMiniatureComponent implements OnInit {
43 @Input() ownerDisplayType: OwnerDisplayType = 'account'
44 @Input() displayOptions: MiniatureDisplayOptions = {
54 @Input() displayAsRow = false
55 @Input() displayVideoActions = true
56 @Input() fitWidth = false
58 @Input() useLazyLoadUrl = false
60 @Output() videoBlocked = new EventEmitter()
61 @Output() videoUnblocked = new EventEmitter()
62 @Output() videoRemoved = new EventEmitter()
64 videoActionsDisplayOptions: VideoActionsDisplayType = {
74 serverConfig: ServerConfig
76 addToWatchLaterText: string
77 addedToWatchLaterText: string
78 inWatchLaterPlaylist: boolean
83 playlistElementId?: number
88 private ownerDisplayTypeChosen: 'account' | 'videoChannel'
91 private screenService: ScreenService,
92 private serverService: ServerService,
94 private authService: AuthService,
95 private videoPlaylistService: VideoPlaylistService,
96 private cd: ChangeDetectorRef,
97 @Inject(LOCALE_ID) private localeId: string
101 return this.video.isVideoNSFWForUser(this.user, this.serverConfig)
105 this.serverConfig = this.serverService.getTmpConfig()
106 this.serverService.getConfig()
107 .subscribe(config => {
108 this.serverConfig = config
109 this.buildVideoLink()
114 this.channelLinkTitle = this.i18n(
115 '{{name}} (channel page)',
116 { name: this.video.channel.name, handle: this.video.byVideoChannel }
119 // We rely on mouseenter to lazy load actions
120 if (this.screenService.isInTouchScreen()) {
126 if (this.useLazyLoadUrl && this.video.url) {
127 const remoteUriConfig = this.serverConfig.search.remoteUri
129 // Redirect on the external instance if not allowed to fetch remote data
130 const externalRedirect = (!this.authService.isLoggedIn() && !remoteUriConfig.anonymous) || !remoteUriConfig.users
131 const fromPath = window.location.pathname + window.location.search
133 this.videoLink = [ '/search/lazy-load-video', { url: this.video.url, externalRedirect, fromPath } ]
137 this.videoLink = [ '/videos/watch', this.video.uuid ]
140 displayOwnerAccount () {
141 return this.ownerDisplayTypeChosen === 'account'
144 displayOwnerVideoChannel () {
145 return this.ownerDisplayTypeChosen === 'videoChannel'
149 return this.video.privacy.id === VideoPrivacy.UNLISTED
153 return this.video.privacy.id === VideoPrivacy.PRIVATE
156 getStateLabel (video: Video) {
157 if (!video.state) return ''
159 if (video.privacy.id !== VideoPrivacy.PRIVATE && video.state.id === VideoState.PUBLISHED) {
160 return this.i18n('Published')
163 if (video.scheduledUpdate) {
164 const updateAt = new Date(video.scheduledUpdate.updateAt.toString()).toLocaleString(this.localeId)
165 return this.i18n('Publication scheduled on ') + updateAt
168 if (video.state.id === VideoState.TO_TRANSCODE && video.waitTranscoding === true) {
169 return this.i18n('Waiting transcoding')
172 if (video.state.id === VideoState.TO_TRANSCODE) {
173 return this.i18n('To transcode')
176 if (video.state.id === VideoState.TO_IMPORT) {
177 return this.i18n('To import')
184 if (this.ownerDisplayTypeChosen === 'account') {
185 return this.video.accountAvatarUrl
188 return this.video.videoChannelAvatarUrl
192 if (this.displayVideoActions) this.showActions = true
194 this.loadWatchLater()
198 this.videoBlocked.emit()
201 onVideoUnblocked () {
202 this.videoUnblocked.emit()
206 this.videoRemoved.emit()
210 return this.authService.isLoggedIn()
213 onWatchLaterClick (currentState: boolean) {
214 if (currentState === true) this.removeFromWatchLater()
215 else this.addToWatchLater()
217 this.inWatchLaterPlaylist = !currentState
221 const body = { videoId: this.video.id }
223 this.videoPlaylistService.addVideoInPlaylist(this.watchLaterPlaylist.id, body).subscribe(
225 this.watchLaterPlaylist.playlistElementId = res.videoPlaylistElement.id
230 removeFromWatchLater () {
231 this.videoPlaylistService.removeVideoFromPlaylist(this.watchLaterPlaylist.id, this.watchLaterPlaylist.playlistElementId, this.video.id)
237 isWatchLaterPlaylistDisplayed () {
238 return this.displayVideoActions && this.isUserLoggedIn() && this.inWatchLaterPlaylist !== undefined
242 if (this.ownerDisplayType === 'account' || this.ownerDisplayType === 'videoChannel') {
243 this.ownerDisplayTypeChosen = this.ownerDisplayType
247 // If the video channel name an UUID (not really displayable, we changed this behaviour in v1.0.0-beta.12)
248 // -> Use the account name
250 this.video.channel.name === `${this.video.account.name}_channel` ||
251 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}$/)
253 this.ownerDisplayTypeChosen = 'account'
255 this.ownerDisplayTypeChosen = 'videoChannel'
259 private loadWatchLater () {
260 if (!this.isUserLoggedIn() || this.inWatchLaterPlaylist !== undefined) return
262 this.authService.userInformationLoaded
263 .pipe(switchMap(() => this.videoPlaylistService.listenToVideoPlaylistChange(this.video.id)))
264 .subscribe(existResult => {
265 const watchLaterPlaylist = this.authService.getUser().specialPlaylists.find(p => p.type === VideoPlaylistType.WATCH_LATER)
266 const existsInWatchLater = existResult.find(r => r.playlistId === watchLaterPlaylist.id)
267 this.inWatchLaterPlaylist = false
269 this.watchLaterPlaylist = {
270 id: watchLaterPlaylist.id
273 if (existsInWatchLater) {
274 this.inWatchLaterPlaylist = true
275 this.watchLaterPlaylist.playlistElementId = existsInWatchLater.playlistElementId
278 this.cd.markForCheck()
281 this.videoPlaylistService.runPlaylistCheck(this.video.id)