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 { LinkType } from '../../../types/link.type'
16 import { ActorAvatarSize } from '../shared-actor-image/actor-avatar.component'
17 import { Video } from '../shared-main'
18 import { VideoPlaylistService } from '../shared-video-playlist'
19 import { VideoActionsDisplayType } from './video-actions-dropdown.component'
21 export type MiniatureDisplayOptions = {
26 privacyLabel?: boolean
29 blacklistInfo?: boolean
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() actorImageSize: ActorAvatarSize = '40'
56 @Input() displayAsRow = false
58 @Input() videoLinkType: LinkType = '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: HTMLServerConfig
78 addToWatchLaterText: string
79 addedToWatchLaterText: string
80 inWatchLaterPlaylist: boolean
85 playlistElementId?: number
88 videoRouterLink: any[] = []
92 private ownerDisplayType: '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.getHTMLConfig()
109 this.buildVideoLink()
113 this.channelLinkTitle = $localize`${this.video.channel.name} (channel page)`
115 // We rely on mouseenter to lazy load actions
116 if (this.screenService.isInTouchScreen()) {
122 if (this.videoLinkType === 'internal' || !this.video.url) {
123 this.videoRouterLink = [ '/w', this.video.uuid ]
127 if (this.videoLinkType === 'external') {
128 this.videoRouterLink = null
129 this.videoHref = this.video.url
130 this.videoTarget = '_blank'
135 this.videoRouterLink = [ '/search/lazy-load-video', { url: this.video.url } ]
138 displayOwnerAccount () {
139 return this.ownerDisplayType === 'account'
142 displayOwnerVideoChannel () {
143 return this.ownerDisplayType === 'videoChannel'
147 return this.video.privacy.id === VideoPrivacy.UNLISTED
151 return this.video.privacy.id === VideoPrivacy.PRIVATE
154 getStateLabel (video: Video) {
155 if (!video.state) return ''
157 if (video.privacy.id !== VideoPrivacy.PRIVATE && video.state.id === VideoState.PUBLISHED) {
158 return $localize`Published`
161 if (video.scheduledUpdate) {
162 const updateAt = new Date(video.scheduledUpdate.updateAt.toString()).toLocaleString(this.localeId)
163 return $localize`Publication scheduled on ` + updateAt
166 if (video.state.id === VideoState.TO_TRANSCODE && video.waitTranscoding === true) {
167 return $localize`Waiting transcoding`
170 if (video.state.id === VideoState.TO_TRANSCODE) {
171 return $localize`To transcode`
174 if (video.state.id === VideoState.TO_IMPORT) {
175 return $localize`To import`
182 if (this.displayVideoActions) this.showActions = true
184 this.loadWatchLater()
188 this.videoBlocked.emit()
191 onVideoUnblocked () {
192 this.videoUnblocked.emit()
196 this.videoRemoved.emit()
199 onVideoAccountMuted () {
200 this.videoAccountMuted.emit()
204 return this.authService.isLoggedIn()
207 onWatchLaterClick (currentState: boolean) {
208 if (currentState === true) this.removeFromWatchLater()
209 else this.addToWatchLater()
211 this.inWatchLaterPlaylist = !currentState
215 const body = { videoId: this.video.id }
217 this.videoPlaylistService.addVideoInPlaylist(this.watchLaterPlaylist.id, body).subscribe(
219 this.watchLaterPlaylist.playlistElementId = res.videoPlaylistElement.id
224 removeFromWatchLater () {
225 this.videoPlaylistService.removeVideoFromPlaylist(this.watchLaterPlaylist.id, this.watchLaterPlaylist.playlistElementId, this.video.id)
231 isWatchLaterPlaylistDisplayed () {
232 return this.displayVideoActions && this.isUserLoggedIn() && this.inWatchLaterPlaylist !== undefined
237 'display-as-row': this.displayAsRow
242 const accountName = this.video.account.name
244 // If the video channel name is an UUID (not really displayable, we changed this behaviour in v1.0.0-beta.12)
245 // Or has not been customized (default created channel display name)
246 // -> Use the account name
248 this.video.channel.displayName === `Default ${accountName} channel` ||
249 this.video.channel.displayName === `Main ${accountName} channel` ||
250 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}$/)
252 this.ownerDisplayType = 'account'
254 this.ownerDisplayType = 'videoChannel'
258 private loadWatchLater () {
259 if (!this.isUserLoggedIn() || this.inWatchLaterPlaylist !== undefined) return
261 this.authService.userInformationLoaded
262 .pipe(switchMap(() => this.videoPlaylistService.listenToVideoPlaylistChange(this.video.id)))
263 .subscribe(existResult => {
264 const watchLaterPlaylist = this.authService.getUser().specialPlaylists.find(p => p.type === VideoPlaylistType.WATCH_LATER)
265 const existsInWatchLater = existResult.find(r => r.playlistId === watchLaterPlaylist.id)
266 this.inWatchLaterPlaylist = false
268 this.watchLaterPlaylist = {
269 id: watchLaterPlaylist.id
272 if (existsInWatchLater) {
273 this.inWatchLaterPlaylist = true
274 this.watchLaterPlaylist.playlistElementId = existsInWatchLater.playlistElementId
277 this.cd.markForCheck()
280 this.videoPlaylistService.runPlaylistCheck(this.video.id)