1 import { switchMap } from 'rxjs/operators'
3 ChangeDetectionStrategy,
12 } from '@angular/core'
13 import { AuthService, ScreenService, ServerService, User } from '@app/core'
14 import { HTMLServerConfig, 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: HTMLServerConfig
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.getHTMLConfig()
110 this.buildVideoLink()
114 this.channelLinkTitle = $localize`${this.video.channel.name} (channel page)`
116 // We rely on mouseenter to lazy load actions
117 if (this.screenService.isInTouchScreen()) {
123 if (this.videoLinkType === 'internal' || !this.video.url) {
124 this.videoRouterLink = [ '/w', this.video.uuid ]
128 if (this.videoLinkType === 'external') {
129 this.videoRouterLink = null
130 this.videoHref = this.video.url
131 this.videoTarget = '_blank'
136 this.videoRouterLink = [ '/search/lazy-load-video', { url: this.video.url } ]
139 displayOwnerAccount () {
140 return this.ownerDisplayType === 'account'
143 displayOwnerVideoChannel () {
144 return this.ownerDisplayType === 'videoChannel'
148 return this.video.privacy.id === VideoPrivacy.UNLISTED
152 return this.video.privacy.id === VideoPrivacy.PRIVATE
155 getStateLabel (video: Video) {
156 if (!video.state) return ''
158 if (video.privacy.id !== VideoPrivacy.PRIVATE && video.state.id === VideoState.PUBLISHED) {
159 return $localize`Published`
162 if (video.scheduledUpdate) {
163 const updateAt = new Date(video.scheduledUpdate.updateAt.toString()).toLocaleString(this.localeId)
164 return $localize`Publication scheduled on ` + updateAt
167 if (video.state.id === VideoState.TO_TRANSCODE && video.waitTranscoding === true) {
168 return $localize`Waiting transcoding`
171 if (video.state.id === VideoState.TO_TRANSCODE) {
172 return $localize`To transcode`
175 if (video.state.id === VideoState.TO_IMPORT) {
176 return $localize`To import`
183 if (this.displayVideoActions) this.showActions = true
185 this.loadWatchLater()
189 this.videoBlocked.emit()
192 onVideoUnblocked () {
193 this.videoUnblocked.emit()
197 this.videoRemoved.emit()
200 onVideoAccountMuted () {
201 this.videoAccountMuted.emit()
205 return this.authService.isLoggedIn()
208 onWatchLaterClick (currentState: boolean) {
209 if (currentState === true) this.removeFromWatchLater()
210 else this.addToWatchLater()
212 this.inWatchLaterPlaylist = !currentState
216 const body = { videoId: this.video.id }
218 this.videoPlaylistService.addVideoInPlaylist(this.watchLaterPlaylist.id, body).subscribe(
220 this.watchLaterPlaylist.playlistElementId = res.videoPlaylistElement.id
225 removeFromWatchLater () {
226 this.videoPlaylistService.removeVideoFromPlaylist(this.watchLaterPlaylist.id, this.watchLaterPlaylist.playlistElementId, this.video.id)
232 isWatchLaterPlaylistDisplayed () {
233 return this.displayVideoActions && this.isUserLoggedIn() && this.inWatchLaterPlaylist !== undefined
238 'display-as-row': this.displayAsRow
243 const accountName = this.video.account.name
245 // If the video channel name is an UUID (not really displayable, we changed this behaviour in v1.0.0-beta.12)
246 // Or has not been customized (default created channel display name)
247 // -> Use the account name
249 this.video.channel.displayName === `Default ${accountName} channel` ||
250 this.video.channel.displayName === `Main ${accountName} 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.ownerDisplayType = 'account'
255 this.ownerDisplayType = '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)