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 = {
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 {
44 @Input() displayOptions: MiniatureDisplayOptions = {
53 forceChannelInBy: false
56 @Input() displayVideoActions = true
57 @Input() videoActionsDisplayOptions: VideoActionsDisplayType = {
70 @Input() actorImageSize: ActorAvatarSize = '40'
72 @Input() displayAsRow = false
74 @Input() videoLinkType: LinkType = 'internal'
76 @Output() videoBlocked = new EventEmitter()
77 @Output() videoUnblocked = new EventEmitter()
78 @Output() videoRemoved = new EventEmitter()
79 @Output() videoAccountMuted = new EventEmitter()
82 serverConfig: HTMLServerConfig
84 addToWatchLaterText: string
85 addedToWatchLaterText: string
86 inWatchLaterPlaylist: boolean
91 playlistElementId?: number
94 videoRouterLink: string | any[] = []
98 private ownerDisplayType: 'account' | 'videoChannel'
101 private screenService: ScreenService,
102 private serverService: ServerService,
103 private authService: AuthService,
104 private videoPlaylistService: VideoPlaylistService,
105 private cd: ChangeDetectorRef,
106 @Inject(LOCALE_ID) private localeId: string
109 get authorAccount () {
110 return this.serverConfig.client.videos.miniature.preferAuthorDisplayName
111 ? this.video.account.displayName
112 : this.video.byAccount
115 get authorChannel () {
116 return this.serverConfig.client.videos.miniature.preferAuthorDisplayName
117 ? this.video.channel.displayName
118 : this.video.byVideoChannel
122 return this.video.isVideoNSFWForUser(this.user, this.serverConfig)
126 this.serverConfig = this.serverService.getHTMLConfig()
127 this.buildVideoLink()
131 this.channelLinkTitle = $localize`${this.video.channel.name} (channel page)`
133 // We rely on mouseenter to lazy load actions
134 if (this.screenService.isInTouchScreen()) {
140 if (this.videoLinkType === 'internal' || !this.video.url) {
141 this.videoRouterLink = Video.buildWatchUrl(this.video)
145 if (this.videoLinkType === 'external') {
146 this.videoRouterLink = null
147 this.videoHref = this.video.url
148 this.videoTarget = '_blank'
153 this.videoRouterLink = [ '/search/lazy-load-video', { url: this.video.url } ]
156 displayOwnerAccount () {
157 return this.ownerDisplayType === 'account'
160 displayOwnerVideoChannel () {
161 return this.ownerDisplayType === 'videoChannel'
165 return this.video.privacy.id === VideoPrivacy.UNLISTED
169 return this.video.privacy.id === VideoPrivacy.PRIVATE
172 getStateLabel (video: Video) {
173 if (!video.state) return ''
175 if (video.privacy.id !== VideoPrivacy.PRIVATE && video.state.id === VideoState.PUBLISHED) {
176 return $localize`Published`
179 if (video.scheduledUpdate) {
180 const updateAt = new Date(video.scheduledUpdate.updateAt.toString()).toLocaleString(this.localeId)
181 return $localize`Publication scheduled on ${updateAt}`
184 if (video.state.id === VideoState.TRANSCODING_FAILED) {
185 return $localize`Transcoding failed`
188 if (video.state.id === VideoState.TO_MOVE_TO_EXTERNAL_STORAGE_FAILED) {
189 return $localize`Move to external storage failed`
192 if (video.state.id === VideoState.TO_TRANSCODE && video.waitTranscoding === true) {
193 return $localize`Waiting transcoding`
196 if (video.state.id === VideoState.TO_TRANSCODE) {
197 return $localize`To transcode`
200 if (video.state.id === VideoState.TO_IMPORT) {
201 return $localize`To import`
204 if (video.state.id === VideoState.TO_EDIT) {
205 return $localize`To edit`
212 if (this.displayVideoActions) this.showActions = true
214 this.loadWatchLater()
218 this.videoBlocked.emit()
221 onVideoUnblocked () {
222 this.videoUnblocked.emit()
226 this.videoRemoved.emit()
229 onVideoAccountMuted () {
230 this.videoAccountMuted.emit()
234 return this.authService.isLoggedIn()
237 onWatchLaterClick (currentState: boolean) {
238 if (currentState === true) this.removeFromWatchLater()
239 else this.addToWatchLater()
241 this.inWatchLaterPlaylist = !currentState
245 const body = { videoId: this.video.id }
247 this.videoPlaylistService.addVideoInPlaylist(this.watchLaterPlaylist.id, body)
250 this.watchLaterPlaylist.playlistElementId = res.videoPlaylistElement.id
255 removeFromWatchLater () {
256 this.videoPlaylistService.removeVideoFromPlaylist(this.watchLaterPlaylist.id, this.watchLaterPlaylist.playlistElementId, this.video.id)
262 isWatchLaterPlaylistDisplayed () {
263 return this.displayVideoActions && this.isUserLoggedIn() && this.inWatchLaterPlaylist !== undefined
268 'display-as-row': this.displayAsRow
273 if (this.displayOptions.forceChannelInBy) {
274 this.ownerDisplayType = 'videoChannel'
278 const accountName = this.video.account.name
280 // If the video channel name is an UUID (not really displayable, we changed this behaviour in v1.0.0-beta.12)
281 // Or has not been customized (default created channel display name)
282 // -> Use the account name
284 this.video.channel.displayName === `Default ${accountName} channel` ||
285 this.video.channel.displayName === `Main ${accountName} channel` ||
286 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}$/)
288 this.ownerDisplayType = 'account'
290 this.ownerDisplayType = 'videoChannel'
294 private loadWatchLater () {
295 if (!this.isUserLoggedIn() || this.inWatchLaterPlaylist !== undefined) return
297 this.authService.userInformationLoaded
298 .pipe(switchMap(() => this.videoPlaylistService.listenToVideoPlaylistChange(this.video.id)))
299 .subscribe(existResult => {
300 const watchLaterPlaylist = this.authService.getUser().specialPlaylists.find(p => p.type === VideoPlaylistType.WATCH_LATER)
301 const existsInWatchLater = existResult.find(r => r.playlistId === watchLaterPlaylist.id)
302 this.inWatchLaterPlaylist = false
304 this.watchLaterPlaylist = {
305 id: watchLaterPlaylist.id
308 if (existsInWatchLater) {
309 this.inWatchLaterPlaylist = true
310 this.watchLaterPlaylist.playlistElementId = existsInWatchLater.playlistElementId
313 this.cd.markForCheck()
316 this.videoPlaylistService.runPlaylistCheck(this.video.id)