1 import { switchMap } from 'rxjs/operators'
3 ChangeDetectionStrategy,
12 } from '@angular/core'
13 import { AuthService, ScreenService, ServerService, User } from '@app/core'
14 import { ServerConfig, VideoPlaylistType, VideoPrivacy, VideoState } from '@shared/models'
15 import { Video } from '../shared-main'
16 import { VideoPlaylistService } from '../shared-video-playlist'
17 import { VideoActionsDisplayType } from './video-actions-dropdown.component'
19 export type OwnerDisplayType = 'account' | 'videoChannel'
20 export type MiniatureDisplayOptions = {
25 privacyLabel?: boolean
28 blacklistInfo?: boolean
31 export type VideoLinkType = 'internal' | 'lazy-load' | 'external'
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() displayOptions: MiniatureDisplayOptions = {
53 @Input() displayAsRow = false
54 @Input() displayVideoActions = true
55 @Input() fitWidth = false
57 @Input() videoLinkType: VideoLinkType = 'internal'
59 @Output() videoBlocked = new EventEmitter()
60 @Output() videoUnblocked = new EventEmitter()
61 @Output() videoRemoved = new EventEmitter()
62 @Output() videoAccountMuted = new EventEmitter()
64 videoActionsDisplayOptions: VideoActionsDisplayType = {
75 serverConfig: ServerConfig
77 addToWatchLaterText: string
78 addedToWatchLaterText: string
79 inWatchLaterPlaylist: boolean
84 playlistElementId?: number
87 videoRouterLink: any[] = []
91 private ownerDisplayType: 'account' | 'videoChannel'
94 private screenService: ScreenService,
95 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 = $localize`${this.video.channel.name} (channel page)`
118 // We rely on mouseenter to lazy load actions
119 if (this.screenService.isInTouchScreen()) {
125 if (this.videoLinkType === 'internal' || !this.video.url) {
126 this.videoRouterLink = [ '/videos/watch', this.video.uuid ]
130 if (this.videoLinkType === 'external') {
131 this.videoRouterLink = null
132 this.videoHref = this.video.url
133 this.videoTarget = '_blank'
138 this.videoRouterLink = [ '/search/lazy-load-video', { url: this.video.url } ]
141 displayOwnerAccount () {
142 return this.ownerDisplayType === 'account'
145 displayOwnerVideoChannel () {
146 return this.ownerDisplayType === 'videoChannel'
150 return this.video.privacy.id === VideoPrivacy.UNLISTED
154 return this.video.privacy.id === VideoPrivacy.PRIVATE
157 getStateLabel (video: Video) {
158 if (!video.state) return ''
160 if (video.privacy.id !== VideoPrivacy.PRIVATE && video.state.id === VideoState.PUBLISHED) {
161 return $localize`Published`
164 if (video.scheduledUpdate) {
165 const updateAt = new Date(video.scheduledUpdate.updateAt.toString()).toLocaleString(this.localeId)
166 return $localize`Publication scheduled on ` + updateAt
169 if (video.state.id === VideoState.TO_TRANSCODE && video.waitTranscoding === true) {
170 return $localize`Waiting transcoding`
173 if (video.state.id === VideoState.TO_TRANSCODE) {
174 return $localize`To transcode`
177 if (video.state.id === VideoState.TO_IMPORT) {
178 return $localize`To import`
185 if (this.displayOwnerAccount()) {
186 return this.video.accountAvatarUrl
189 return this.video.videoChannelAvatarUrl
193 if (this.displayVideoActions) this.showActions = true
195 this.loadWatchLater()
199 this.videoBlocked.emit()
202 onVideoUnblocked () {
203 this.videoUnblocked.emit()
207 this.videoRemoved.emit()
210 onVideoAccountMuted () {
211 this.videoAccountMuted.emit()
215 return this.authService.isLoggedIn()
218 onWatchLaterClick (currentState: boolean) {
219 if (currentState === true) this.removeFromWatchLater()
220 else this.addToWatchLater()
222 this.inWatchLaterPlaylist = !currentState
226 const body = { videoId: this.video.id }
228 this.videoPlaylistService.addVideoInPlaylist(this.watchLaterPlaylist.id, body).subscribe(
230 this.watchLaterPlaylist.playlistElementId = res.videoPlaylistElement.id
235 removeFromWatchLater () {
236 this.videoPlaylistService.removeVideoFromPlaylist(this.watchLaterPlaylist.id, this.watchLaterPlaylist.playlistElementId, this.video.id)
242 isWatchLaterPlaylistDisplayed () {
243 return this.displayVideoActions && this.isUserLoggedIn() && this.inWatchLaterPlaylist !== undefined
247 const accountName = this.video.account.name
249 // If the video channel name is an UUID (not really displayable, we changed this behaviour in v1.0.0-beta.12)
250 // Or has not been customized (default created channel display name)
251 // -> Use the account name
253 this.video.channel.displayName === `Default ${accountName} channel` ||
254 this.video.channel.displayName === `Main ${accountName} channel` ||
255 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}$/)
257 this.ownerDisplayType = 'account'
259 this.ownerDisplayType = 'videoChannel'
263 private loadWatchLater () {
264 if (!this.isUserLoggedIn() || this.inWatchLaterPlaylist !== undefined) return
266 this.authService.userInformationLoaded
267 .pipe(switchMap(() => this.videoPlaylistService.listenToVideoPlaylistChange(this.video.id)))
268 .subscribe(existResult => {
269 const watchLaterPlaylist = this.authService.getUser().specialPlaylists.find(p => p.type === VideoPlaylistType.WATCH_LATER)
270 const existsInWatchLater = existResult.find(r => r.playlistId === watchLaterPlaylist.id)
271 this.inWatchLaterPlaylist = false
273 this.watchLaterPlaylist = {
274 id: watchLaterPlaylist.id
277 if (existsInWatchLater) {
278 this.inWatchLaterPlaylist = true
279 this.watchLaterPlaylist.playlistElementId = existsInWatchLater.playlistElementId
282 this.cd.markForCheck()
285 this.videoPlaylistService.runPlaylistCheck(this.video.id)