2 ChangeDetectionStrategy,
11 } from '@angular/core'
12 import { User } from '../users'
13 import { Video } from './video.model'
14 import { AuthService, ServerService } from '@app/core'
15 import { ServerConfig, VideoPlaylistType, VideoPrivacy, VideoState } from '../../../../../shared'
16 import { I18n } from '@ngx-translate/i18n-polyfill'
17 import { VideoActionsDisplayType } from '@app/shared/video/video-actions-dropdown.component'
18 import { ScreenService } from '@app/shared/misc/screen.service'
19 import { VideoPlaylistService } from '@app/shared/video-playlist/video-playlist.service'
20 import { switchMap } from 'rxjs/operators'
22 export type OwnerDisplayType = 'account' | 'videoChannel' | 'auto'
23 export type MiniatureDisplayOptions = {
28 privacyLabel?: boolean
31 blacklistInfo?: boolean
36 selector: 'my-video-miniature',
37 styleUrls: [ './video-miniature.component.scss' ],
38 templateUrl: './video-miniature.component.html',
39 changeDetection: ChangeDetectionStrategy.OnPush
41 export class VideoMiniatureComponent implements OnInit {
45 @Input() ownerDisplayType: OwnerDisplayType = 'account'
46 @Input() displayOptions: MiniatureDisplayOptions = {
56 @Input() displayAsRow = false
57 @Input() displayVideoActions = true
59 @Output() videoBlacklisted = new EventEmitter()
60 @Output() videoUnblacklisted = new EventEmitter()
61 @Output() videoRemoved = new EventEmitter()
63 videoActionsDisplayOptions: VideoActionsDisplayType = {
73 serverConfig: ServerConfig
75 addToWatchLaterText: string
76 addedToWatchLaterText: string
77 inWatchLaterPlaylist: boolean
81 playlistElementId?: number
84 private ownerDisplayTypeChosen: 'account' | 'videoChannel'
87 private screenService: ScreenService,
88 private serverService: ServerService,
90 private authService: AuthService,
91 private videoPlaylistService: VideoPlaylistService,
92 private cd: ChangeDetectorRef,
93 @Inject(LOCALE_ID) private localeId: string
99 return this.video.isVideoNSFWForUser(this.user, this.serverConfig)
103 this.serverConfig = this.serverService.getTmpConfig()
104 this.serverService.getConfig()
105 .subscribe(config => this.serverConfig = config)
109 // We rely on mouseenter to lazy load actions
110 if (this.screenService.isInTouchScreen()) {
115 displayOwnerAccount () {
116 return this.ownerDisplayTypeChosen === 'account'
119 displayOwnerVideoChannel () {
120 return this.ownerDisplayTypeChosen === 'videoChannel'
124 return this.video.privacy.id === VideoPrivacy.UNLISTED
128 return this.video.privacy.id === VideoPrivacy.PRIVATE
131 getStateLabel (video: Video) {
132 if (!video.state) return ''
134 if (video.privacy.id !== VideoPrivacy.PRIVATE && video.state.id === VideoState.PUBLISHED) {
135 return this.i18n('Published')
138 if (video.scheduledUpdate) {
139 const updateAt = new Date(video.scheduledUpdate.updateAt.toString()).toLocaleString(this.localeId)
140 return this.i18n('Publication scheduled on ') + updateAt
143 if (video.state.id === VideoState.TO_TRANSCODE && video.waitTranscoding === true) {
144 return this.i18n('Waiting transcoding')
147 if (video.state.id === VideoState.TO_TRANSCODE) {
148 return this.i18n('To transcode')
151 if (video.state.id === VideoState.TO_IMPORT) {
152 return this.i18n('To import')
159 if (this.displayVideoActions) this.showActions = true
161 this.loadWatchLater()
164 onVideoBlacklisted () {
165 this.videoBlacklisted.emit()
168 onVideoUnblacklisted () {
169 this.videoUnblacklisted.emit()
173 this.videoRemoved.emit()
177 return this.authService.isLoggedIn()
180 onWatchLaterClick (currentState: boolean) {
181 if (currentState === true) this.removeFromWatchLater()
182 else this.addToWatchLater()
184 this.inWatchLaterPlaylist = !currentState
188 const body = { videoId: this.video.id }
190 this.videoPlaylistService.addVideoInPlaylist(this.watchLaterPlaylist.id, body).subscribe(
192 this.watchLaterPlaylist.playlistElementId = res.videoPlaylistElement.id
197 removeFromWatchLater () {
198 this.videoPlaylistService.removeVideoFromPlaylist(this.watchLaterPlaylist.id, this.watchLaterPlaylist.playlistElementId, this.video.id)
204 isWatchLaterPlaylistDisplayed () {
205 return this.isUserLoggedIn() && this.inWatchLaterPlaylist !== undefined
209 if (this.ownerDisplayType === 'account' || this.ownerDisplayType === 'videoChannel') {
210 this.ownerDisplayTypeChosen = this.ownerDisplayType
214 // If the video channel name an UUID (not really displayable, we changed this behaviour in v1.0.0-beta.12)
215 // -> Use the account name
217 this.video.channel.name === `${this.video.account.name}_channel` ||
218 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}$/)
220 this.ownerDisplayTypeChosen = 'account'
222 this.ownerDisplayTypeChosen = 'videoChannel'
226 private loadWatchLater () {
227 if (!this.isUserLoggedIn() || this.inWatchLaterPlaylist !== undefined) return
229 this.authService.userInformationLoaded
230 .pipe(switchMap(() => this.videoPlaylistService.listenToVideoPlaylistChange(this.video.id)))
231 .subscribe(existResult => {
232 const watchLaterPlaylist = this.authService.getUser().specialPlaylists.find(p => p.type === VideoPlaylistType.WATCH_LATER)
233 const existsInWatchLater = existResult.find(r => r.playlistId === watchLaterPlaylist.id)
234 this.inWatchLaterPlaylist = false
236 this.watchLaterPlaylist = {
237 id: watchLaterPlaylist.id
240 if (existsInWatchLater) {
241 this.inWatchLaterPlaylist = true
242 this.watchLaterPlaylist.playlistElementId = existsInWatchLater.playlistElementId
245 this.cd.markForCheck()
248 this.videoPlaylistService.runPlaylistCheck(this.video.id)