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 { ActorAvatarSize } from '../shared-actor-image/actor-avatar.component'
16 import { Video } from '../shared-main'
17 import { VideoPlaylistService } from '../shared-video-playlist'
18 import { VideoActionsDisplayType } from './video-actions-dropdown.component'
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() displayVideoActions = true
55 @Input() actorImageSize: ActorAvatarSize = '40'
57 @Input() displayAsRow = false
59 @Input() videoLinkType: VideoLinkType = 'internal'
61 @Output() videoBlocked = new EventEmitter()
62 @Output() videoUnblocked = new EventEmitter()
63 @Output() videoRemoved = new EventEmitter()
64 @Output() videoAccountMuted = new EventEmitter()
66 videoActionsDisplayOptions: VideoActionsDisplayType = {
77 serverConfig: ServerConfig
79 addToWatchLaterText: string
80 addedToWatchLaterText: string
81 inWatchLaterPlaylist: boolean
86 playlistElementId?: number
89 videoRouterLink: any[] = []
93 private ownerDisplayType: 'account' | 'videoChannel'
96 private screenService: ScreenService,
97 private serverService: ServerService,
98 private authService: AuthService,
99 private videoPlaylistService: VideoPlaylistService,
100 private cd: ChangeDetectorRef,
101 @Inject(LOCALE_ID) private localeId: string
105 return this.video.isVideoNSFWForUser(this.user, this.serverConfig)
109 this.serverConfig = this.serverService.getTmpConfig()
110 this.serverService.getConfig()
111 .subscribe(config => {
112 this.serverConfig = config
113 this.buildVideoLink()
118 this.channelLinkTitle = $localize`${this.video.channel.name} (channel page)`
120 // We rely on mouseenter to lazy load actions
121 if (this.screenService.isInTouchScreen()) {
127 if (this.videoLinkType === 'internal' || !this.video.url) {
128 this.videoRouterLink = [ '/videos/watch', this.video.uuid ]
132 if (this.videoLinkType === 'external') {
133 this.videoRouterLink = null
134 this.videoHref = this.video.url
135 this.videoTarget = '_blank'
140 this.videoRouterLink = [ '/search/lazy-load-video', { url: this.video.url } ]
143 displayOwnerAccount () {
144 return this.ownerDisplayType === 'account'
147 displayOwnerVideoChannel () {
148 return this.ownerDisplayType === 'videoChannel'
152 return this.video.privacy.id === VideoPrivacy.UNLISTED
156 return this.video.privacy.id === VideoPrivacy.PRIVATE
159 getStateLabel (video: Video) {
160 if (!video.state) return ''
162 if (video.privacy.id !== VideoPrivacy.PRIVATE && video.state.id === VideoState.PUBLISHED) {
163 return $localize`Published`
166 if (video.scheduledUpdate) {
167 const updateAt = new Date(video.scheduledUpdate.updateAt.toString()).toLocaleString(this.localeId)
168 return $localize`Publication scheduled on ` + updateAt
171 if (video.state.id === VideoState.TO_TRANSCODE && video.waitTranscoding === true) {
172 return $localize`Waiting transcoding`
175 if (video.state.id === VideoState.TO_TRANSCODE) {
176 return $localize`To transcode`
179 if (video.state.id === VideoState.TO_IMPORT) {
180 return $localize`To import`
187 if (this.displayVideoActions) this.showActions = true
189 this.loadWatchLater()
193 this.videoBlocked.emit()
196 onVideoUnblocked () {
197 this.videoUnblocked.emit()
201 this.videoRemoved.emit()
204 onVideoAccountMuted () {
205 this.videoAccountMuted.emit()
209 return this.authService.isLoggedIn()
212 onWatchLaterClick (currentState: boolean) {
213 if (currentState === true) this.removeFromWatchLater()
214 else this.addToWatchLater()
216 this.inWatchLaterPlaylist = !currentState
220 const body = { videoId: this.video.id }
222 this.videoPlaylistService.addVideoInPlaylist(this.watchLaterPlaylist.id, body).subscribe(
224 this.watchLaterPlaylist.playlistElementId = res.videoPlaylistElement.id
229 removeFromWatchLater () {
230 this.videoPlaylistService.removeVideoFromPlaylist(this.watchLaterPlaylist.id, this.watchLaterPlaylist.playlistElementId, this.video.id)
236 isWatchLaterPlaylistDisplayed () {
237 return this.displayVideoActions && this.isUserLoggedIn() && this.inWatchLaterPlaylist !== undefined
242 'display-as-row': this.displayAsRow
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)