1 import { switchMap } from 'rxjs/operators'
3 ChangeDetectionStrategy,
12 } from '@angular/core'
13 import { AuthService, ScreenService, ServerService, User } from '@app/core'
14 import { I18n } from '@ngx-translate/i18n-polyfill'
15 import { ServerConfig, VideoPlaylistType, VideoPrivacy, VideoState } from '../../../../../shared'
16 import { Video } from '../shared-main'
17 import { VideoPlaylistService } from '../shared-video-playlist'
18 import { VideoActionsDisplayType } from './video-actions-dropdown.component'
20 export type OwnerDisplayType = 'account' | 'videoChannel' | 'auto'
21 export type MiniatureDisplayOptions = {
26 privacyLabel?: boolean
29 blacklistInfo?: boolean
34 selector: 'my-video-miniature',
35 styleUrls: [ './video-miniature.component.scss' ],
36 templateUrl: './video-miniature.component.html',
37 changeDetection: ChangeDetectionStrategy.OnPush
39 export class VideoMiniatureComponent implements OnInit {
43 @Input() ownerDisplayType: OwnerDisplayType = 'account'
44 @Input() displayOptions: MiniatureDisplayOptions = {
54 @Input() displayAsRow = false
55 @Input() displayVideoActions = true
56 @Input() fitWidth = false
58 @Input() useLazyLoadUrl = false
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: ServerConfig
78 addToWatchLaterText: string
79 addedToWatchLaterText: string
80 inWatchLaterPlaylist: boolean
85 playlistElementId?: number
90 private ownerDisplayTypeChosen: 'account' | 'videoChannel'
93 private screenService: ScreenService,
94 private serverService: ServerService,
96 private authService: AuthService,
97 private videoPlaylistService: VideoPlaylistService,
98 private cd: ChangeDetectorRef,
99 @Inject(LOCALE_ID) private localeId: string
103 return this.video.isVideoNSFWForUser(this.user, this.serverConfig)
107 this.serverConfig = this.serverService.getTmpConfig()
108 this.serverService.getConfig()
109 .subscribe(config => {
110 this.serverConfig = config
111 this.buildVideoLink()
116 this.channelLinkTitle = this.i18n(
117 '{{name}} (channel page)',
118 { name: this.video.channel.name, handle: this.video.byVideoChannel }
121 // We rely on mouseenter to lazy load actions
122 if (this.screenService.isInTouchScreen()) {
128 if (this.useLazyLoadUrl && this.video.url) {
129 const remoteUriConfig = this.serverConfig.search.remoteUri
131 // Redirect on the external instance if not allowed to fetch remote data
132 const externalRedirect = (!this.authService.isLoggedIn() && !remoteUriConfig.anonymous) || !remoteUriConfig.users
133 const fromPath = window.location.pathname + window.location.search
135 this.videoLink = [ '/search/lazy-load-video', { url: this.video.url, externalRedirect, fromPath } ]
139 this.videoLink = [ '/videos/watch', this.video.uuid ]
142 displayOwnerAccount () {
143 return this.ownerDisplayTypeChosen === 'account'
146 displayOwnerVideoChannel () {
147 return this.ownerDisplayTypeChosen === 'videoChannel'
151 return this.video.privacy.id === VideoPrivacy.UNLISTED
155 return this.video.privacy.id === VideoPrivacy.PRIVATE
158 getStateLabel (video: Video) {
159 if (!video.state) return ''
161 if (video.privacy.id !== VideoPrivacy.PRIVATE && video.state.id === VideoState.PUBLISHED) {
162 return this.i18n('Published')
165 if (video.scheduledUpdate) {
166 const updateAt = new Date(video.scheduledUpdate.updateAt.toString()).toLocaleString(this.localeId)
167 return this.i18n('Publication scheduled on ') + updateAt
170 if (video.state.id === VideoState.TO_TRANSCODE && video.waitTranscoding === true) {
171 return this.i18n('Waiting transcoding')
174 if (video.state.id === VideoState.TO_TRANSCODE) {
175 return this.i18n('To transcode')
178 if (video.state.id === VideoState.TO_IMPORT) {
179 return this.i18n('To import')
186 if (this.ownerDisplayTypeChosen === 'account') {
187 return this.video.accountAvatarUrl
190 return this.video.videoChannelAvatarUrl
194 if (this.displayVideoActions) this.showActions = true
196 this.loadWatchLater()
200 this.videoBlocked.emit()
203 onVideoUnblocked () {
204 this.videoUnblocked.emit()
208 this.videoRemoved.emit()
211 onVideoAccountMuted () {
212 this.videoAccountMuted.emit()
216 return this.authService.isLoggedIn()
219 onWatchLaterClick (currentState: boolean) {
220 if (currentState === true) this.removeFromWatchLater()
221 else this.addToWatchLater()
223 this.inWatchLaterPlaylist = !currentState
227 const body = { videoId: this.video.id }
229 this.videoPlaylistService.addVideoInPlaylist(this.watchLaterPlaylist.id, body).subscribe(
231 this.watchLaterPlaylist.playlistElementId = res.videoPlaylistElement.id
236 removeFromWatchLater () {
237 this.videoPlaylistService.removeVideoFromPlaylist(this.watchLaterPlaylist.id, this.watchLaterPlaylist.playlistElementId, this.video.id)
243 isWatchLaterPlaylistDisplayed () {
244 return this.displayVideoActions && this.isUserLoggedIn() && this.inWatchLaterPlaylist !== undefined
248 if (this.ownerDisplayType === 'account' || this.ownerDisplayType === 'videoChannel') {
249 this.ownerDisplayTypeChosen = this.ownerDisplayType
253 // If the video channel name an UUID (not really displayable, we changed this behaviour in v1.0.0-beta.12)
254 // -> Use the account name
256 this.video.channel.name === `${this.video.account.name}_channel` ||
257 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}$/)
259 this.ownerDisplayTypeChosen = 'account'
261 this.ownerDisplayTypeChosen = 'videoChannel'
265 private loadWatchLater () {
266 if (!this.isUserLoggedIn() || this.inWatchLaterPlaylist !== undefined) return
268 this.authService.userInformationLoaded
269 .pipe(switchMap(() => this.videoPlaylistService.listenToVideoPlaylistChange(this.video.id)))
270 .subscribe(existResult => {
271 const watchLaterPlaylist = this.authService.getUser().specialPlaylists.find(p => p.type === VideoPlaylistType.WATCH_LATER)
272 const existsInWatchLater = existResult.find(r => r.playlistId === watchLaterPlaylist.id)
273 this.inWatchLaterPlaylist = false
275 this.watchLaterPlaylist = {
276 id: watchLaterPlaylist.id
279 if (existsInWatchLater) {
280 this.inWatchLaterPlaylist = true
281 this.watchLaterPlaylist.playlistElementId = existsInWatchLater.playlistElementId
284 this.cd.markForCheck()
287 this.videoPlaylistService.runPlaylistCheck(this.video.id)