1 import { switchMap } from 'rxjs/operators'
3 ChangeDetectionStrategy,
12 } from '@angular/core'
13 import { AuthService, ScreenService, ServerService, User } from '@app/core'
14 import { HTMLServerConfig, VideoExistInPlaylist, 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 = {
25 privacyLabel?: boolean
28 blacklistInfo?: boolean
32 forceChannelInBy?: boolean
35 selector: 'my-video-miniature',
36 styleUrls: [ './video-miniature.component.scss' ],
37 templateUrl: './video-miniature.component.html',
38 changeDetection: ChangeDetectionStrategy.OnPush
40 export class VideoMiniatureComponent implements OnInit {
43 @Input() containedInPlaylists: VideoExistInPlaylist[]
45 @Input() displayOptions: MiniatureDisplayOptions = {
54 forceChannelInBy: false
57 @Input() displayVideoActions = true
58 @Input() videoActionsDisplayOptions: VideoActionsDisplayType = {
71 @Input() actorImageSize: ActorAvatarSize = '40'
73 @Input() displayAsRow = false
75 @Input() videoLinkType: LinkType = 'internal'
77 @Output() videoBlocked = new EventEmitter()
78 @Output() videoUnblocked = new EventEmitter()
79 @Output() videoRemoved = new EventEmitter()
80 @Output() videoAccountMuted = new EventEmitter()
83 serverConfig: HTMLServerConfig
85 addToWatchLaterText: string
86 addedToWatchLaterText: string
87 inWatchLaterPlaylist: boolean
92 playlistElementId?: number
95 videoRouterLink: string | any[] = []
99 private ownerDisplayType: 'account' | 'videoChannel'
102 private screenService: ScreenService,
103 private serverService: ServerService,
104 private authService: AuthService,
105 private videoPlaylistService: VideoPlaylistService,
106 private cd: ChangeDetectorRef,
107 @Inject(LOCALE_ID) private localeId: string
110 get authorAccount () {
111 return this.serverConfig.client.videos.miniature.preferAuthorDisplayName
112 ? this.video.account.displayName
113 : this.video.byAccount
116 get authorChannel () {
117 return this.serverConfig.client.videos.miniature.preferAuthorDisplayName
118 ? this.video.channel.displayName
119 : this.video.byVideoChannel
123 return this.video.isVideoNSFWForUser(this.user, this.serverConfig)
127 this.serverConfig = this.serverService.getHTMLConfig()
128 this.buildVideoLink()
132 this.channelLinkTitle = $localize`${this.video.channel.name} (channel page)`
134 // We rely on mouseenter to lazy load actions
135 if (this.screenService.isInTouchScreen()) {
141 if (this.videoLinkType === 'internal' || !this.video.url) {
142 this.videoRouterLink = Video.buildWatchUrl(this.video)
146 if (this.videoLinkType === 'external') {
147 this.videoRouterLink = null
148 this.videoHref = this.video.url
149 this.videoTarget = '_blank'
154 this.videoRouterLink = [ '/search/lazy-load-video', { url: this.video.url } ]
157 displayOwnerAccount () {
158 return this.ownerDisplayType === 'account'
161 displayOwnerVideoChannel () {
162 return this.ownerDisplayType === 'videoChannel'
166 return this.video.privacy.id === VideoPrivacy.UNLISTED
170 return this.video.privacy.id === VideoPrivacy.PRIVATE
173 getStateLabel (video: Video) {
174 if (!video.state) return ''
176 if (video.privacy.id !== VideoPrivacy.PRIVATE && video.state.id === VideoState.PUBLISHED) {
177 return $localize`Published`
180 if (video.scheduledUpdate) {
181 const updateAt = new Date(video.scheduledUpdate.updateAt.toString()).toLocaleString(this.localeId)
182 return $localize`Publication scheduled on ${updateAt}`
185 if (video.state.id === VideoState.TRANSCODING_FAILED) {
186 return $localize`Transcoding failed`
189 if (video.state.id === VideoState.TO_MOVE_TO_EXTERNAL_STORAGE_FAILED) {
190 return $localize`Move to external storage failed`
193 if (video.state.id === VideoState.TO_TRANSCODE && video.waitTranscoding === true) {
194 return $localize`Waiting transcoding`
197 if (video.state.id === VideoState.TO_TRANSCODE) {
198 return $localize`To transcode`
201 if (video.state.id === VideoState.TO_IMPORT) {
202 return $localize`To import`
205 if (video.state.id === VideoState.TO_EDIT) {
206 return $localize`To edit`
213 if (this.displayVideoActions) this.showActions = true
215 this.loadWatchLater()
219 this.videoBlocked.emit()
222 onVideoUnblocked () {
223 this.videoUnblocked.emit()
227 this.videoRemoved.emit()
230 onVideoAccountMuted () {
231 this.videoAccountMuted.emit()
235 return this.authService.isLoggedIn()
238 onWatchLaterClick (currentState: boolean) {
239 if (currentState === true) this.removeFromWatchLater()
240 else this.addToWatchLater()
242 this.inWatchLaterPlaylist = !currentState
246 const body = { videoId: this.video.id }
248 this.videoPlaylistService.addVideoInPlaylist(this.watchLaterPlaylist.id, body)
251 this.watchLaterPlaylist.playlistElementId = res.videoPlaylistElement.id
256 removeFromWatchLater () {
257 this.videoPlaylistService.removeVideoFromPlaylist(this.watchLaterPlaylist.id, this.watchLaterPlaylist.playlistElementId, this.video.id)
263 isWatchLaterPlaylistDisplayed () {
264 return this.displayVideoActions && this.isUserLoggedIn() && this.inWatchLaterPlaylist !== undefined
269 'display-as-row': this.displayAsRow
274 if (this.displayOptions.forceChannelInBy) {
275 this.ownerDisplayType = 'videoChannel'
279 const accountName = this.video.account.name
281 // If the video channel name is an UUID (not really displayable, we changed this behaviour in v1.0.0-beta.12)
282 // Or has not been customized (default created channel display name)
283 // -> Use the account name
285 this.video.channel.displayName === `Default ${accountName} channel` ||
286 this.video.channel.displayName === `Main ${accountName} channel` ||
287 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}$/)
289 this.ownerDisplayType = 'account'
291 this.ownerDisplayType = 'videoChannel'
295 private loadWatchLater () {
296 if (!this.isUserLoggedIn() || this.inWatchLaterPlaylist !== undefined) return
298 this.authService.userInformationLoaded
299 .pipe(switchMap(() => this.videoPlaylistService.listenToVideoPlaylistChange(this.video.id)))
300 .subscribe(existResult => {
301 const watchLaterPlaylist = this.authService.getUser().specialPlaylists.find(p => p.type === VideoPlaylistType.WATCH_LATER)
302 const existsInWatchLater = existResult.find(r => r.playlistId === watchLaterPlaylist.id)
303 this.inWatchLaterPlaylist = false
305 this.watchLaterPlaylist = {
306 id: watchLaterPlaylist.id
309 if (existsInWatchLater) {
310 this.inWatchLaterPlaylist = true
311 this.watchLaterPlaylist.playlistElementId = existsInWatchLater.playlistElementId
314 this.cd.markForCheck()
317 this.videoPlaylistService.runVideoExistsInPlaylistCheck(this.video.id)