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
32 export type VideoLinkType = 'internal' | 'lazy-load' | 'external'
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() ownerDisplayType: OwnerDisplayType = 'account'
45 @Input() displayOptions: MiniatureDisplayOptions = {
55 @Input() displayAsRow = false
56 @Input() displayVideoActions = true
57 @Input() fitWidth = false
59 @Input() videoLinkType: VideoLinkType = 'internal'
61 @Output() videoBlocked = new EventEmitter()
62 @Output() videoUnblocked = new EventEmitter()
63 @Output() videoRemoved = new EventEmitter()
64 @Output() videoAccountMuted = new EventEmitter()
66 videoActionsDisplayOptions: VideoActionsDisplayType = {
77 serverConfig: ServerConfig
79 addToWatchLaterText: string
80 addedToWatchLaterText: string
81 inWatchLaterPlaylist: boolean
86 playlistElementId?: number
89 videoRouterLink: any[] = []
93 private ownerDisplayTypeChosen: 'account' | 'videoChannel'
96 private screenService: ScreenService,
97 private serverService: ServerService,
99 private authService: AuthService,
100 private videoPlaylistService: VideoPlaylistService,
101 private cd: ChangeDetectorRef,
102 @Inject(LOCALE_ID) private localeId: string
106 return this.video.isVideoNSFWForUser(this.user, this.serverConfig)
110 this.serverConfig = this.serverService.getTmpConfig()
111 this.serverService.getConfig()
112 .subscribe(config => {
113 this.serverConfig = config
114 this.buildVideoLink()
119 this.channelLinkTitle = this.i18n(
120 '{{name}} (channel page)',
121 { name: this.video.channel.name, handle: this.video.byVideoChannel }
124 // We rely on mouseenter to lazy load actions
125 if (this.screenService.isInTouchScreen()) {
131 if (this.videoLinkType === 'internal' || !this.video.url) {
132 this.videoRouterLink = [ '/videos/watch', this.video.uuid ]
136 if (this.videoLinkType === 'external') {
137 this.videoRouterLink = null
138 this.videoHref = this.video.url
139 this.videoTarget = '_blank'
144 this.videoRouterLink = [ '/search/lazy-load-video', { url: this.video.url } ]
147 displayOwnerAccount () {
148 return this.ownerDisplayTypeChosen === 'account'
151 displayOwnerVideoChannel () {
152 return this.ownerDisplayTypeChosen === 'videoChannel'
156 return this.video.privacy.id === VideoPrivacy.UNLISTED
160 return this.video.privacy.id === VideoPrivacy.PRIVATE
163 getStateLabel (video: Video) {
164 if (!video.state) return ''
166 if (video.privacy.id !== VideoPrivacy.PRIVATE && video.state.id === VideoState.PUBLISHED) {
167 return this.i18n('Published')
170 if (video.scheduledUpdate) {
171 const updateAt = new Date(video.scheduledUpdate.updateAt.toString()).toLocaleString(this.localeId)
172 return this.i18n('Publication scheduled on ') + updateAt
175 if (video.state.id === VideoState.TO_TRANSCODE && video.waitTranscoding === true) {
176 return this.i18n('Waiting transcoding')
179 if (video.state.id === VideoState.TO_TRANSCODE) {
180 return this.i18n('To transcode')
183 if (video.state.id === VideoState.TO_IMPORT) {
184 return this.i18n('To import')
191 if (this.ownerDisplayTypeChosen === 'account') {
192 return this.video.accountAvatarUrl
195 return this.video.videoChannelAvatarUrl
199 if (this.displayVideoActions) this.showActions = true
201 this.loadWatchLater()
205 this.videoBlocked.emit()
208 onVideoUnblocked () {
209 this.videoUnblocked.emit()
213 this.videoRemoved.emit()
216 onVideoAccountMuted () {
217 this.videoAccountMuted.emit()
221 return this.authService.isLoggedIn()
224 onWatchLaterClick (currentState: boolean) {
225 if (currentState === true) this.removeFromWatchLater()
226 else this.addToWatchLater()
228 this.inWatchLaterPlaylist = !currentState
232 const body = { videoId: this.video.id }
234 this.videoPlaylistService.addVideoInPlaylist(this.watchLaterPlaylist.id, body).subscribe(
236 this.watchLaterPlaylist.playlistElementId = res.videoPlaylistElement.id
241 removeFromWatchLater () {
242 this.videoPlaylistService.removeVideoFromPlaylist(this.watchLaterPlaylist.id, this.watchLaterPlaylist.playlistElementId, this.video.id)
248 isWatchLaterPlaylistDisplayed () {
249 return this.displayVideoActions && this.isUserLoggedIn() && this.inWatchLaterPlaylist !== undefined
253 if (this.ownerDisplayType === 'account' || this.ownerDisplayType === 'videoChannel') {
254 this.ownerDisplayTypeChosen = this.ownerDisplayType
258 // If the video channel name an UUID (not really displayable, we changed this behaviour in v1.0.0-beta.12)
259 // -> Use the account name
261 this.video.channel.name === `${this.video.account.name}_channel` ||
262 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}$/)
264 this.ownerDisplayTypeChosen = 'account'
266 this.ownerDisplayTypeChosen = 'videoChannel'
270 private loadWatchLater () {
271 if (!this.isUserLoggedIn() || this.inWatchLaterPlaylist !== undefined) return
273 this.authService.userInformationLoaded
274 .pipe(switchMap(() => this.videoPlaylistService.listenToVideoPlaylistChange(this.video.id)))
275 .subscribe(existResult => {
276 const watchLaterPlaylist = this.authService.getUser().specialPlaylists.find(p => p.type === VideoPlaylistType.WATCH_LATER)
277 const existsInWatchLater = existResult.find(r => r.playlistId === watchLaterPlaylist.id)
278 this.inWatchLaterPlaylist = false
280 this.watchLaterPlaylist = {
281 id: watchLaterPlaylist.id
284 if (existsInWatchLater) {
285 this.inWatchLaterPlaylist = true
286 this.watchLaterPlaylist.playlistElementId = existsInWatchLater.playlistElementId
289 this.cd.markForCheck()
292 this.videoPlaylistService.runPlaylistCheck(this.video.id)