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 = {
53 @Input() displayVideoActions = true
54 @Input() videoActionsDisplayOptions: VideoActionsDisplayType = {
67 @Input() actorImageSize: ActorAvatarSize = '40'
69 @Input() displayAsRow = false
71 @Input() videoLinkType: LinkType = 'internal'
73 @Output() videoBlocked = new EventEmitter()
74 @Output() videoUnblocked = new EventEmitter()
75 @Output() videoRemoved = new EventEmitter()
76 @Output() videoAccountMuted = new EventEmitter()
79 serverConfig: HTMLServerConfig
81 addToWatchLaterText: string
82 addedToWatchLaterText: string
83 inWatchLaterPlaylist: boolean
88 playlistElementId?: number
91 videoRouterLink: string | any[] = []
95 private ownerDisplayType: 'account' | 'videoChannel'
98 private screenService: ScreenService,
99 private serverService: ServerService,
100 private authService: AuthService,
101 private videoPlaylistService: VideoPlaylistService,
102 private cd: ChangeDetectorRef,
103 @Inject(LOCALE_ID) private localeId: string
106 get authorAccount () {
107 return this.serverConfig.client.videos.miniature.preferAuthorDisplayName
108 ? this.video.account.displayName
109 : this.video.byAccount
112 get authorChannel () {
113 return this.serverConfig.client.videos.miniature.preferAuthorDisplayName
114 ? this.video.channel.displayName
115 : this.video.byVideoChannel
119 return this.video.isVideoNSFWForUser(this.user, this.serverConfig)
123 this.serverConfig = this.serverService.getHTMLConfig()
124 this.buildVideoLink()
128 this.channelLinkTitle = $localize`${this.video.channel.name} (channel page)`
130 // We rely on mouseenter to lazy load actions
131 if (this.screenService.isInTouchScreen()) {
137 if (this.videoLinkType === 'internal' || !this.video.url) {
138 this.videoRouterLink = Video.buildWatchUrl(this.video)
142 if (this.videoLinkType === 'external') {
143 this.videoRouterLink = null
144 this.videoHref = this.video.url
145 this.videoTarget = '_blank'
150 this.videoRouterLink = [ '/search/lazy-load-video', { url: this.video.url } ]
153 displayOwnerAccount () {
154 return this.ownerDisplayType === 'account'
157 displayOwnerVideoChannel () {
158 return this.ownerDisplayType === 'videoChannel'
162 return this.video.privacy.id === VideoPrivacy.UNLISTED
166 return this.video.privacy.id === VideoPrivacy.PRIVATE
169 getStateLabel (video: Video) {
170 if (!video.state) return ''
172 if (video.privacy.id !== VideoPrivacy.PRIVATE && video.state.id === VideoState.PUBLISHED) {
173 return $localize`Published`
176 if (video.scheduledUpdate) {
177 const updateAt = new Date(video.scheduledUpdate.updateAt.toString()).toLocaleString(this.localeId)
178 return $localize`Publication scheduled on ${updateAt}`
181 if (video.state.id === VideoState.TRANSCODING_FAILED) {
182 return $localize`Transcoding failed`
185 if (video.state.id === VideoState.TO_MOVE_TO_EXTERNAL_STORAGE_FAILED) {
186 return $localize`Move to external storage failed`
189 if (video.state.id === VideoState.TO_TRANSCODE && video.waitTranscoding === true) {
190 return $localize`Waiting transcoding`
193 if (video.state.id === VideoState.TO_TRANSCODE) {
194 return $localize`To transcode`
197 if (video.state.id === VideoState.TO_IMPORT) {
198 return $localize`To import`
201 if (video.state.id === VideoState.TO_EDIT) {
202 return $localize`To edit`
209 if (this.displayVideoActions) this.showActions = true
211 this.loadWatchLater()
215 this.videoBlocked.emit()
218 onVideoUnblocked () {
219 this.videoUnblocked.emit()
223 this.videoRemoved.emit()
226 onVideoAccountMuted () {
227 this.videoAccountMuted.emit()
231 return this.authService.isLoggedIn()
234 onWatchLaterClick (currentState: boolean) {
235 if (currentState === true) this.removeFromWatchLater()
236 else this.addToWatchLater()
238 this.inWatchLaterPlaylist = !currentState
242 const body = { videoId: this.video.id }
244 this.videoPlaylistService.addVideoInPlaylist(this.watchLaterPlaylist.id, body)
247 this.watchLaterPlaylist.playlistElementId = res.videoPlaylistElement.id
252 removeFromWatchLater () {
253 this.videoPlaylistService.removeVideoFromPlaylist(this.watchLaterPlaylist.id, this.watchLaterPlaylist.playlistElementId, this.video.id)
259 isWatchLaterPlaylistDisplayed () {
260 return this.displayVideoActions && this.isUserLoggedIn() && this.inWatchLaterPlaylist !== undefined
265 'display-as-row': this.displayAsRow
270 const accountName = this.video.account.name
272 // If the video channel name is an UUID (not really displayable, we changed this behaviour in v1.0.0-beta.12)
273 // Or has not been customized (default created channel display name)
274 // -> Use the account name
276 this.video.channel.displayName === `Default ${accountName} channel` ||
277 this.video.channel.displayName === `Main ${accountName} channel` ||
278 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}$/)
280 this.ownerDisplayType = 'account'
282 this.ownerDisplayType = 'videoChannel'
286 private loadWatchLater () {
287 if (!this.isUserLoggedIn() || this.inWatchLaterPlaylist !== undefined) return
289 this.authService.userInformationLoaded
290 .pipe(switchMap(() => this.videoPlaylistService.listenToVideoPlaylistChange(this.video.id)))
291 .subscribe(existResult => {
292 const watchLaterPlaylist = this.authService.getUser().specialPlaylists.find(p => p.type === VideoPlaylistType.WATCH_LATER)
293 const existsInWatchLater = existResult.find(r => r.playlistId === watchLaterPlaylist.id)
294 this.inWatchLaterPlaylist = false
296 this.watchLaterPlaylist = {
297 id: watchLaterPlaylist.id
300 if (existsInWatchLater) {
301 this.inWatchLaterPlaylist = true
302 this.watchLaterPlaylist.playlistElementId = existsInWatchLater.playlistElementId
305 this.cd.markForCheck()
308 this.videoPlaylistService.runPlaylistCheck(this.video.id)