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: string | 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
103 get authorAccount () {
104 return this.serverConfig.client.videos.miniature.preferAuthorDisplayName
105 ? this.video.account.displayName
106 : this.video.byAccount
109 get authorChannel () {
110 return this.serverConfig.client.videos.miniature.preferAuthorDisplayName
111 ? this.video.channel.displayName
112 : this.video.byVideoChannel
116 return this.video.isVideoNSFWForUser(this.user, this.serverConfig)
120 this.serverConfig = this.serverService.getHTMLConfig()
121 this.buildVideoLink()
125 this.channelLinkTitle = $localize`${this.video.channel.name} (channel page)`
127 // We rely on mouseenter to lazy load actions
128 if (this.screenService.isInTouchScreen()) {
134 if (this.videoLinkType === 'internal' || !this.video.url) {
135 this.videoRouterLink = Video.buildWatchUrl(this.video)
139 if (this.videoLinkType === 'external') {
140 this.videoRouterLink = null
141 this.videoHref = this.video.url
142 this.videoTarget = '_blank'
147 this.videoRouterLink = [ '/search/lazy-load-video', { url: this.video.url } ]
150 displayOwnerAccount () {
151 return this.ownerDisplayType === 'account'
154 displayOwnerVideoChannel () {
155 return this.ownerDisplayType === 'videoChannel'
159 return this.video.privacy.id === VideoPrivacy.UNLISTED
163 return this.video.privacy.id === VideoPrivacy.PRIVATE
166 getStateLabel (video: Video) {
167 if (!video.state) return ''
169 if (video.privacy.id !== VideoPrivacy.PRIVATE && video.state.id === VideoState.PUBLISHED) {
170 return $localize`Published`
173 if (video.scheduledUpdate) {
174 const updateAt = new Date(video.scheduledUpdate.updateAt.toString()).toLocaleString(this.localeId)
175 return $localize`Publication scheduled on ` + updateAt
178 if (video.state.id === VideoState.TRANSCODING_FAILED) {
179 return $localize`Transcoding failed`
182 if (video.state.id === VideoState.TO_MOVE_TO_EXTERNAL_STORAGE_FAILED) {
183 return $localize`Move to external storage failed`
186 if (video.state.id === VideoState.TO_TRANSCODE && video.waitTranscoding === true) {
187 return $localize`Waiting transcoding`
190 if (video.state.id === VideoState.TO_TRANSCODE) {
191 return $localize`To transcode`
194 if (video.state.id === VideoState.TO_IMPORT) {
195 return $localize`To import`
198 if (video.state.id === VideoState.TO_EDIT) {
199 return $localize`To edit`
206 if (this.displayVideoActions) this.showActions = true
208 this.loadWatchLater()
212 this.videoBlocked.emit()
215 onVideoUnblocked () {
216 this.videoUnblocked.emit()
220 this.videoRemoved.emit()
223 onVideoAccountMuted () {
224 this.videoAccountMuted.emit()
228 return this.authService.isLoggedIn()
231 onWatchLaterClick (currentState: boolean) {
232 if (currentState === true) this.removeFromWatchLater()
233 else this.addToWatchLater()
235 this.inWatchLaterPlaylist = !currentState
239 const body = { videoId: this.video.id }
241 this.videoPlaylistService.addVideoInPlaylist(this.watchLaterPlaylist.id, body)
244 this.watchLaterPlaylist.playlistElementId = res.videoPlaylistElement.id
249 removeFromWatchLater () {
250 this.videoPlaylistService.removeVideoFromPlaylist(this.watchLaterPlaylist.id, this.watchLaterPlaylist.playlistElementId, this.video.id)
256 isWatchLaterPlaylistDisplayed () {
257 return this.displayVideoActions && this.isUserLoggedIn() && this.inWatchLaterPlaylist !== undefined
262 'display-as-row': this.displayAsRow
267 const accountName = this.video.account.name
269 // If the video channel name is an UUID (not really displayable, we changed this behaviour in v1.0.0-beta.12)
270 // Or has not been customized (default created channel display name)
271 // -> Use the account name
273 this.video.channel.displayName === `Default ${accountName} channel` ||
274 this.video.channel.displayName === `Main ${accountName} channel` ||
275 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}$/)
277 this.ownerDisplayType = 'account'
279 this.ownerDisplayType = 'videoChannel'
283 private loadWatchLater () {
284 if (!this.isUserLoggedIn() || this.inWatchLaterPlaylist !== undefined) return
286 this.authService.userInformationLoaded
287 .pipe(switchMap(() => this.videoPlaylistService.listenToVideoPlaylistChange(this.video.id)))
288 .subscribe(existResult => {
289 const watchLaterPlaylist = this.authService.getUser().specialPlaylists.find(p => p.type === VideoPlaylistType.WATCH_LATER)
290 const existsInWatchLater = existResult.find(r => r.playlistId === watchLaterPlaylist.id)
291 this.inWatchLaterPlaylist = false
293 this.watchLaterPlaylist = {
294 id: watchLaterPlaylist.id
297 if (existsInWatchLater) {
298 this.inWatchLaterPlaylist = true
299 this.watchLaterPlaylist.playlistElementId = existsInWatchLater.playlistElementId
302 this.cd.markForCheck()
305 this.videoPlaylistService.runPlaylistCheck(this.video.id)