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
85 playlistElementId?: number
90 private ownerDisplayTypeChosen: 'account' | 'videoChannel'
93 private screenService: ScreenService,
94 private serverService: ServerService,
96 private authService: AuthService,
97 private videoPlaylistService: VideoPlaylistService,
98 private cd: ChangeDetectorRef,
99 @Inject(LOCALE_ID) private localeId: string
103 return this.video.isVideoNSFWForUser(this.user, this.serverConfig)
107 this.serverConfig = this.serverService.getTmpConfig()
108 this.serverService.getConfig()
109 .subscribe(config => {
110 this.serverConfig = config
111 this.buildVideoLink()
116 this.channelLinkTitle = this.i18n(
117 '{{name}} (channel page)',
118 { name: this.video.channel.name, handle: this.video.byVideoChannel }
121 // We rely on mouseenter to lazy load actions
122 if (this.screenService.isInTouchScreen()) {
128 if (this.useLazyLoadUrl && this.video.url) {
129 const remoteUriConfig = this.serverConfig.search.remoteUri
131 // Redirect on the external instance if not allowed to fetch remote data
132 const externalRedirect = (!this.authService.isLoggedIn() && !remoteUriConfig.anonymous) || !remoteUriConfig.users
133 const fromPath = window.location.pathname + window.location.search
135 this.videoLink = [ '/search/lazy-load-video', { url: this.video.url, externalRedirect, fromPath } ]
139 this.videoLink = [ '/videos/watch', this.video.uuid ]
142 displayOwnerAccount () {
143 return this.ownerDisplayTypeChosen === 'account'
146 displayOwnerVideoChannel () {
147 return this.ownerDisplayTypeChosen === 'videoChannel'
151 return this.video.privacy.id === VideoPrivacy.UNLISTED
155 return this.video.privacy.id === VideoPrivacy.PRIVATE
158 getStateLabel (video: Video) {
159 if (!video.state) return ''
161 if (video.privacy.id !== VideoPrivacy.PRIVATE && video.state.id === VideoState.PUBLISHED) {
162 return this.i18n('Published')
165 if (video.scheduledUpdate) {
166 const updateAt = new Date(video.scheduledUpdate.updateAt.toString()).toLocaleString(this.localeId)
167 return this.i18n('Publication scheduled on ') + updateAt
170 if (video.state.id === VideoState.TO_TRANSCODE && video.waitTranscoding === true) {
171 return this.i18n('Waiting transcoding')
174 if (video.state.id === VideoState.TO_TRANSCODE) {
175 return this.i18n('To transcode')
178 if (video.state.id === VideoState.TO_IMPORT) {
179 return this.i18n('To import')
186 if (this.ownerDisplayTypeChosen === 'account') {
187 return this.video.accountAvatarUrl
190 return this.video.videoChannelAvatarUrl
194 if (this.displayVideoActions) this.showActions = true
196 this.loadWatchLater()
200 this.videoBlocked.emit()
203 onVideoUnblocked () {
204 this.videoUnblocked.emit()
208 this.videoRemoved.emit()
212 return this.authService.isLoggedIn()
215 onWatchLaterClick (currentState: boolean) {
216 if (currentState === true) this.removeFromWatchLater()
217 else this.addToWatchLater()
219 this.inWatchLaterPlaylist = !currentState
223 const body = { videoId: this.video.id }
225 this.videoPlaylistService.addVideoInPlaylist(this.watchLaterPlaylist.id, body).subscribe(
227 this.watchLaterPlaylist.playlistElementId = res.videoPlaylistElement.id
232 removeFromWatchLater () {
233 this.videoPlaylistService.removeVideoFromPlaylist(this.watchLaterPlaylist.id, this.watchLaterPlaylist.playlistElementId, this.video.id)
239 isWatchLaterPlaylistDisplayed () {
240 return this.displayVideoActions && this.isUserLoggedIn() && this.inWatchLaterPlaylist !== undefined
244 if (this.ownerDisplayType === 'account' || this.ownerDisplayType === 'videoChannel') {
245 this.ownerDisplayTypeChosen = this.ownerDisplayType
249 // If the video channel name an UUID (not really displayable, we changed this behaviour in v1.0.0-beta.12)
250 // -> Use the account name
252 this.video.channel.name === `${this.video.account.name}_channel` ||
253 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}$/)
255 this.ownerDisplayTypeChosen = 'account'
257 this.ownerDisplayTypeChosen = 'videoChannel'
261 private loadWatchLater () {
262 if (!this.isUserLoggedIn() || this.inWatchLaterPlaylist !== undefined) return
264 this.authService.userInformationLoaded
265 .pipe(switchMap(() => this.videoPlaylistService.listenToVideoPlaylistChange(this.video.id)))
266 .subscribe(existResult => {
267 const watchLaterPlaylist = this.authService.getUser().specialPlaylists.find(p => p.type === VideoPlaylistType.WATCH_LATER)
268 const existsInWatchLater = existResult.find(r => r.playlistId === watchLaterPlaylist.id)
269 this.inWatchLaterPlaylist = false
271 this.watchLaterPlaylist = {
272 id: watchLaterPlaylist.id
275 if (existsInWatchLater) {
276 this.inWatchLaterPlaylist = true
277 this.watchLaterPlaylist.playlistElementId = existsInWatchLater.playlistElementId
280 this.cd.markForCheck()
283 this.videoPlaylistService.runPlaylistCheck(this.video.id)