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' | 'auto'
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() ownerDisplayType: OwnerDisplayType = 'account'
44 @Input() displayOptions: MiniatureDisplayOptions = {
54 @Input() displayAsRow = false
55 @Input() displayVideoActions = true
56 @Input() fitWidth = false
58 @Input() videoLinkType: VideoLinkType = 'internal'
60 @Output() videoBlocked = new EventEmitter()
61 @Output() videoUnblocked = new EventEmitter()
62 @Output() videoRemoved = new EventEmitter()
63 @Output() videoAccountMuted = new EventEmitter()
65 videoActionsDisplayOptions: VideoActionsDisplayType = {
76 serverConfig: ServerConfig
78 addToWatchLaterText: string
79 addedToWatchLaterText: string
80 inWatchLaterPlaylist: boolean
85 playlistElementId?: number
88 videoRouterLink: any[] = []
92 private ownerDisplayTypeChosen: 'account' | 'videoChannel'
95 private screenService: ScreenService,
96 private serverService: ServerService,
97 private authService: AuthService,
98 private videoPlaylistService: VideoPlaylistService,
99 private cd: ChangeDetectorRef,
100 @Inject(LOCALE_ID) private localeId: string
104 return this.video.isVideoNSFWForUser(this.user, this.serverConfig)
108 this.serverConfig = this.serverService.getTmpConfig()
109 this.serverService.getConfig()
110 .subscribe(config => {
111 this.serverConfig = config
112 this.buildVideoLink()
117 this.channelLinkTitle = $localize`${this.video.channel.name} (channel page)`
119 // We rely on mouseenter to lazy load actions
120 if (this.screenService.isInTouchScreen()) {
126 if (this.videoLinkType === 'internal' || !this.video.url) {
127 this.videoRouterLink = [ '/videos/watch', this.video.uuid ]
131 if (this.videoLinkType === 'external') {
132 this.videoRouterLink = null
133 this.videoHref = this.video.url
134 this.videoTarget = '_blank'
139 this.videoRouterLink = [ '/search/lazy-load-video', { url: this.video.url } ]
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 $localize`Published`
165 if (video.scheduledUpdate) {
166 const updateAt = new Date(video.scheduledUpdate.updateAt.toString()).toLocaleString(this.localeId)
167 return $localize`Publication scheduled on ` + updateAt
170 if (video.state.id === VideoState.TO_TRANSCODE && video.waitTranscoding === true) {
171 return $localize`Waiting transcoding`
174 if (video.state.id === VideoState.TO_TRANSCODE) {
175 return $localize`To transcode`
178 if (video.state.id === VideoState.TO_IMPORT) {
179 return $localize`To import`
186 if (this.displayOwnerAccount()) {
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()
211 onVideoAccountMuted () {
212 this.videoAccountMuted.emit()
216 return this.authService.isLoggedIn()
219 onWatchLaterClick (currentState: boolean) {
220 if (currentState === true) this.removeFromWatchLater()
221 else this.addToWatchLater()
223 this.inWatchLaterPlaylist = !currentState
227 const body = { videoId: this.video.id }
229 this.videoPlaylistService.addVideoInPlaylist(this.watchLaterPlaylist.id, body).subscribe(
231 this.watchLaterPlaylist.playlistElementId = res.videoPlaylistElement.id
236 removeFromWatchLater () {
237 this.videoPlaylistService.removeVideoFromPlaylist(this.watchLaterPlaylist.id, this.watchLaterPlaylist.playlistElementId, this.video.id)
243 isWatchLaterPlaylistDisplayed () {
244 return this.displayVideoActions && this.isUserLoggedIn() && this.inWatchLaterPlaylist !== undefined
248 if (this.ownerDisplayType === 'account' || this.ownerDisplayType === 'videoChannel') {
249 this.ownerDisplayTypeChosen = this.ownerDisplayType
253 // If the video channel name is an UUID (not really displayable, we changed this behaviour in v1.0.0-beta.12)
254 // Or is just a suffix of the account (default created channel)
255 // -> Use the account name
257 this.video.channel.name === `${this.video.account.name}_channel` ||
258 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}$/)
260 this.ownerDisplayTypeChosen = 'account'
262 this.ownerDisplayTypeChosen = 'videoChannel'
266 private loadWatchLater () {
267 if (!this.isUserLoggedIn() || this.inWatchLaterPlaylist !== undefined) return
269 this.authService.userInformationLoaded
270 .pipe(switchMap(() => this.videoPlaylistService.listenToVideoPlaylistChange(this.video.id)))
271 .subscribe(existResult => {
272 const watchLaterPlaylist = this.authService.getUser().specialPlaylists.find(p => p.type === VideoPlaylistType.WATCH_LATER)
273 const existsInWatchLater = existResult.find(r => r.playlistId === watchLaterPlaylist.id)
274 this.inWatchLaterPlaylist = false
276 this.watchLaterPlaylist = {
277 id: watchLaterPlaylist.id
280 if (existsInWatchLater) {
281 this.inWatchLaterPlaylist = true
282 this.watchLaterPlaylist.playlistElementId = existsInWatchLater.playlistElementId
285 this.cd.markForCheck()
288 this.videoPlaylistService.runPlaylistCheck(this.video.id)