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 MiniatureDisplayOptions = {
24 privacyLabel?: boolean
27 blacklistInfo?: boolean
30 export type VideoLinkType = 'internal' | 'lazy-load' | 'external'
33 selector: 'my-video-miniature',
34 styleUrls: [ './video-miniature.component.scss' ],
35 templateUrl: './video-miniature.component.html',
36 changeDetection: ChangeDetectionStrategy.OnPush
38 export class VideoMiniatureComponent implements OnInit {
42 @Input() displayOptions: MiniatureDisplayOptions = {
52 @Input() displayVideoActions = true
54 @Input() displayAsRow = false
56 @Input() videoLinkType: VideoLinkType = 'internal'
58 @Output() videoBlocked = new EventEmitter()
59 @Output() videoUnblocked = new EventEmitter()
60 @Output() videoRemoved = new EventEmitter()
61 @Output() videoAccountMuted = new EventEmitter()
63 videoActionsDisplayOptions: VideoActionsDisplayType = {
74 serverConfig: ServerConfig
76 addToWatchLaterText: string
77 addedToWatchLaterText: string
78 inWatchLaterPlaylist: boolean
83 playlistElementId?: number
86 videoRouterLink: any[] = []
90 private ownerDisplayType: 'account' | 'videoChannel'
93 private screenService: ScreenService,
94 private serverService: ServerService,
95 private authService: AuthService,
96 private videoPlaylistService: VideoPlaylistService,
97 private cd: ChangeDetectorRef,
98 @Inject(LOCALE_ID) private localeId: string
102 return this.video.isVideoNSFWForUser(this.user, this.serverConfig)
106 this.serverConfig = this.serverService.getTmpConfig()
107 this.serverService.getConfig()
108 .subscribe(config => {
109 this.serverConfig = config
110 this.buildVideoLink()
115 this.channelLinkTitle = $localize`${this.video.channel.name} (channel page)`
117 // We rely on mouseenter to lazy load actions
118 if (this.screenService.isInTouchScreen()) {
124 if (this.videoLinkType === 'internal' || !this.video.url) {
125 this.videoRouterLink = [ '/videos/watch', this.video.uuid ]
129 if (this.videoLinkType === 'external') {
130 this.videoRouterLink = null
131 this.videoHref = this.video.url
132 this.videoTarget = '_blank'
137 this.videoRouterLink = [ '/search/lazy-load-video', { url: this.video.url } ]
140 displayOwnerAccount () {
141 return this.ownerDisplayType === 'account'
144 displayOwnerVideoChannel () {
145 return this.ownerDisplayType === '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 $localize`Published`
163 if (video.scheduledUpdate) {
164 const updateAt = new Date(video.scheduledUpdate.updateAt.toString()).toLocaleString(this.localeId)
165 return $localize`Publication scheduled on ` + updateAt
168 if (video.state.id === VideoState.TO_TRANSCODE && video.waitTranscoding === true) {
169 return $localize`Waiting transcoding`
172 if (video.state.id === VideoState.TO_TRANSCODE) {
173 return $localize`To transcode`
176 if (video.state.id === VideoState.TO_IMPORT) {
177 return $localize`To import`
184 if (this.displayOwnerAccount()) {
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()
209 onVideoAccountMuted () {
210 this.videoAccountMuted.emit()
214 return this.authService.isLoggedIn()
217 onWatchLaterClick (currentState: boolean) {
218 if (currentState === true) this.removeFromWatchLater()
219 else this.addToWatchLater()
221 this.inWatchLaterPlaylist = !currentState
225 const body = { videoId: this.video.id }
227 this.videoPlaylistService.addVideoInPlaylist(this.watchLaterPlaylist.id, body).subscribe(
229 this.watchLaterPlaylist.playlistElementId = res.videoPlaylistElement.id
234 removeFromWatchLater () {
235 this.videoPlaylistService.removeVideoFromPlaylist(this.watchLaterPlaylist.id, this.watchLaterPlaylist.playlistElementId, this.video.id)
241 isWatchLaterPlaylistDisplayed () {
242 return this.displayVideoActions && this.isUserLoggedIn() && this.inWatchLaterPlaylist !== undefined
247 'display-as-row': this.displayAsRow
252 const accountName = this.video.account.name
254 // If the video channel name is an UUID (not really displayable, we changed this behaviour in v1.0.0-beta.12)
255 // Or has not been customized (default created channel display name)
256 // -> Use the account name
258 this.video.channel.displayName === `Default ${accountName} channel` ||
259 this.video.channel.displayName === `Main ${accountName} channel` ||
260 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}$/)
262 this.ownerDisplayType = 'account'
264 this.ownerDisplayType = 'videoChannel'
268 private loadWatchLater () {
269 if (!this.isUserLoggedIn() || this.inWatchLaterPlaylist !== undefined) return
271 this.authService.userInformationLoaded
272 .pipe(switchMap(() => this.videoPlaylistService.listenToVideoPlaylistChange(this.video.id)))
273 .subscribe(existResult => {
274 const watchLaterPlaylist = this.authService.getUser().specialPlaylists.find(p => p.type === VideoPlaylistType.WATCH_LATER)
275 const existsInWatchLater = existResult.find(r => r.playlistId === watchLaterPlaylist.id)
276 this.inWatchLaterPlaylist = false
278 this.watchLaterPlaylist = {
279 id: watchLaterPlaylist.id
282 if (existsInWatchLater) {
283 this.inWatchLaterPlaylist = true
284 this.watchLaterPlaylist.playlistElementId = existsInWatchLater.playlistElementId
287 this.cd.markForCheck()
290 this.videoPlaylistService.runPlaylistCheck(this.video.id)