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
58 @Input() fitWidth = false
60 @Output() videoBlacklisted = new EventEmitter()
61 @Output() videoUnblacklisted = new EventEmitter()
62 @Output() videoRemoved = new EventEmitter()
64 videoActionsDisplayOptions: VideoActionsDisplayType = {
74 serverConfig: ServerConfig
76 addToWatchLaterText: string
77 addedToWatchLaterText: string
78 inWatchLaterPlaylist: boolean
82 playlistElementId?: number
85 private ownerDisplayTypeChosen: 'account' | 'videoChannel'
88 private screenService: ScreenService,
89 private serverService: ServerService,
91 private authService: AuthService,
92 private videoPlaylistService: VideoPlaylistService,
93 private cd: ChangeDetectorRef,
94 @Inject(LOCALE_ID) private localeId: string
100 return this.video.isVideoNSFWForUser(this.user, this.serverConfig)
104 this.serverConfig = this.serverService.getTmpConfig()
105 this.serverService.getConfig()
106 .subscribe(config => this.serverConfig = config)
110 // We rely on mouseenter to lazy load actions
111 if (this.screenService.isInTouchScreen()) {
116 displayOwnerAccount () {
117 return this.ownerDisplayTypeChosen === 'account'
120 displayOwnerVideoChannel () {
121 return this.ownerDisplayTypeChosen === 'videoChannel'
125 return this.video.privacy.id === VideoPrivacy.UNLISTED
129 return this.video.privacy.id === VideoPrivacy.PRIVATE
132 getStateLabel (video: Video) {
133 if (!video.state) return ''
135 if (video.privacy.id !== VideoPrivacy.PRIVATE && video.state.id === VideoState.PUBLISHED) {
136 return this.i18n('Published')
139 if (video.scheduledUpdate) {
140 const updateAt = new Date(video.scheduledUpdate.updateAt.toString()).toLocaleString(this.localeId)
141 return this.i18n('Publication scheduled on ') + updateAt
144 if (video.state.id === VideoState.TO_TRANSCODE && video.waitTranscoding === true) {
145 return this.i18n('Waiting transcoding')
148 if (video.state.id === VideoState.TO_TRANSCODE) {
149 return this.i18n('To transcode')
152 if (video.state.id === VideoState.TO_IMPORT) {
153 return this.i18n('To import')
160 if (this.displayVideoActions) this.showActions = true
162 this.loadWatchLater()
165 onVideoBlacklisted () {
166 this.videoBlacklisted.emit()
169 onVideoUnblacklisted () {
170 this.videoUnblacklisted.emit()
174 this.videoRemoved.emit()
178 return this.authService.isLoggedIn()
181 onWatchLaterClick (currentState: boolean) {
182 if (currentState === true) this.removeFromWatchLater()
183 else this.addToWatchLater()
185 this.inWatchLaterPlaylist = !currentState
189 const body = { videoId: this.video.id }
191 this.videoPlaylistService.addVideoInPlaylist(this.watchLaterPlaylist.id, body).subscribe(
193 this.watchLaterPlaylist.playlistElementId = res.videoPlaylistElement.id
198 removeFromWatchLater () {
199 this.videoPlaylistService.removeVideoFromPlaylist(this.watchLaterPlaylist.id, this.watchLaterPlaylist.playlistElementId, this.video.id)
205 isWatchLaterPlaylistDisplayed () {
206 return this.isUserLoggedIn() && this.inWatchLaterPlaylist !== undefined
210 if (this.ownerDisplayType === 'account' || this.ownerDisplayType === 'videoChannel') {
211 this.ownerDisplayTypeChosen = this.ownerDisplayType
215 // If the video channel name an UUID (not really displayable, we changed this behaviour in v1.0.0-beta.12)
216 // -> Use the account name
218 this.video.channel.name === `${this.video.account.name}_channel` ||
219 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}$/)
221 this.ownerDisplayTypeChosen = 'account'
223 this.ownerDisplayTypeChosen = 'videoChannel'
227 private loadWatchLater () {
228 if (!this.isUserLoggedIn() || this.inWatchLaterPlaylist !== undefined) return
230 this.authService.userInformationLoaded
231 .pipe(switchMap(() => this.videoPlaylistService.listenToVideoPlaylistChange(this.video.id)))
232 .subscribe(existResult => {
233 const watchLaterPlaylist = this.authService.getUser().specialPlaylists.find(p => p.type === VideoPlaylistType.WATCH_LATER)
234 const existsInWatchLater = existResult.find(r => r.playlistId === watchLaterPlaylist.id)
235 this.inWatchLaterPlaylist = false
237 this.watchLaterPlaylist = {
238 id: watchLaterPlaylist.id
241 if (existsInWatchLater) {
242 this.inWatchLaterPlaylist = true
243 this.watchLaterPlaylist.playlistElementId = existsInWatchLater.playlistElementId
246 this.cd.markForCheck()
249 this.videoPlaylistService.runPlaylistCheck(this.video.id)