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 = {
27 privacyLabel?: boolean
30 blacklistInfo?: 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() ownerDisplayType: OwnerDisplayType = 'account'
45 @Input() displayOptions: MiniatureDisplayOptions = {
54 @Input() displayAsRow = false
55 @Input() displayVideoActions = true
57 @Output() videoBlacklisted = new EventEmitter()
58 @Output() videoUnblacklisted = new EventEmitter()
59 @Output() videoRemoved = new EventEmitter()
61 videoActionsDisplayOptions: VideoActionsDisplayType = {
71 serverConfig: ServerConfig
73 addToWatchLaterText: string
74 addedToWatchLaterText: string
75 inWatchLaterPlaylist: boolean
79 playlistElementId?: number
82 private ownerDisplayTypeChosen: 'account' | 'videoChannel'
85 private screenService: ScreenService,
86 private serverService: ServerService,
88 private authService: AuthService,
89 private videoPlaylistService: VideoPlaylistService,
90 private cd: ChangeDetectorRef,
91 @Inject(LOCALE_ID) private localeId: string
97 return this.video.isVideoNSFWForUser(this.user, this.serverConfig)
101 this.serverConfig = this.serverService.getTmpConfig()
102 this.serverService.getConfig()
103 .subscribe(config => this.serverConfig = config)
107 // We rely on mouseenter to lazy load actions
108 if (this.screenService.isInTouchScreen()) {
113 displayOwnerAccount () {
114 return this.ownerDisplayTypeChosen === 'account'
117 displayOwnerVideoChannel () {
118 return this.ownerDisplayTypeChosen === 'videoChannel'
122 return this.video.privacy.id === VideoPrivacy.UNLISTED
126 return this.video.privacy.id === VideoPrivacy.PRIVATE
129 getStateLabel (video: Video) {
130 if (!video.state) return ''
132 if (video.privacy.id !== VideoPrivacy.PRIVATE && video.state.id === VideoState.PUBLISHED) {
133 return this.i18n('Published')
136 if (video.scheduledUpdate) {
137 const updateAt = new Date(video.scheduledUpdate.updateAt.toString()).toLocaleString(this.localeId)
138 return this.i18n('Publication scheduled on ') + updateAt
141 if (video.state.id === VideoState.TO_TRANSCODE && video.waitTranscoding === true) {
142 return this.i18n('Waiting transcoding')
145 if (video.state.id === VideoState.TO_TRANSCODE) {
146 return this.i18n('To transcode')
149 if (video.state.id === VideoState.TO_IMPORT) {
150 return this.i18n('To import')
157 if (this.displayVideoActions) this.showActions = true
159 this.loadWatchLater()
162 onVideoBlacklisted () {
163 this.videoBlacklisted.emit()
166 onVideoUnblacklisted () {
167 this.videoUnblacklisted.emit()
171 this.videoRemoved.emit()
175 return this.authService.isLoggedIn()
178 onWatchLaterClick (currentState: boolean) {
179 if (currentState === true) this.removeFromWatchLater()
180 else this.addToWatchLater()
182 this.inWatchLaterPlaylist = !currentState
186 const body = { videoId: this.video.id }
188 this.videoPlaylistService.addVideoInPlaylist(this.watchLaterPlaylist.id, body).subscribe(
190 this.watchLaterPlaylist.playlistElementId = res.videoPlaylistElement.id
195 removeFromWatchLater () {
196 this.videoPlaylistService.removeVideoFromPlaylist(this.watchLaterPlaylist.id, this.watchLaterPlaylist.playlistElementId, this.video.id)
202 isWatchLaterPlaylistDisplayed () {
203 return this.isUserLoggedIn() && this.inWatchLaterPlaylist !== undefined
207 if (this.ownerDisplayType === 'account' || this.ownerDisplayType === 'videoChannel') {
208 this.ownerDisplayTypeChosen = this.ownerDisplayType
212 // If the video channel name an UUID (not really displayable, we changed this behaviour in v1.0.0-beta.12)
213 // -> Use the account name
215 this.video.channel.name === `${this.video.account.name}_channel` ||
216 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}$/)
218 this.ownerDisplayTypeChosen = 'account'
220 this.ownerDisplayTypeChosen = 'videoChannel'
224 private loadWatchLater () {
225 if (!this.isUserLoggedIn() || this.inWatchLaterPlaylist !== undefined) return
227 this.authService.userInformationLoaded
228 .pipe(switchMap(() => this.videoPlaylistService.listenToVideoPlaylistChange(this.video.id)))
229 .subscribe(existResult => {
230 const watchLaterPlaylist = this.authService.getUser().specialPlaylists.find(p => p.type === VideoPlaylistType.WATCH_LATER)
231 const existsInWatchLater = existResult.find(r => r.playlistId === watchLaterPlaylist.id)
232 this.inWatchLaterPlaylist = false
234 this.watchLaterPlaylist = {
235 id: watchLaterPlaylist.id
238 if (existsInWatchLater) {
239 this.inWatchLaterPlaylist = true
240 this.watchLaterPlaylist.playlistElementId = existsInWatchLater.playlistElementId
243 this.cd.markForCheck()
246 this.videoPlaylistService.runPlaylistCheck(this.video.id)