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 = {
70 serverConfig: ServerConfig
72 addToWatchLaterText: string
73 addedToWatchLaterText: string
74 inWatchLaterPlaylist: boolean
78 playlistElementId?: number
81 private ownerDisplayTypeChosen: 'account' | 'videoChannel'
84 private screenService: ScreenService,
85 private serverService: ServerService,
87 private authService: AuthService,
88 private videoPlaylistService: VideoPlaylistService,
89 private cd: ChangeDetectorRef,
90 @Inject(LOCALE_ID) private localeId: string
96 return this.video.isVideoNSFWForUser(this.user, this.serverConfig)
100 this.serverConfig = this.serverService.getTmpConfig()
101 this.serverService.getConfig()
102 .subscribe(config => this.serverConfig = config)
106 // We rely on mouseenter to lazy load actions
107 if (this.screenService.isInTouchScreen()) {
112 displayOwnerAccount () {
113 return this.ownerDisplayTypeChosen === 'account'
116 displayOwnerVideoChannel () {
117 return this.ownerDisplayTypeChosen === 'videoChannel'
121 return this.video.privacy.id === VideoPrivacy.UNLISTED
125 return this.video.privacy.id === VideoPrivacy.PRIVATE
128 getStateLabel (video: Video) {
129 if (!video.state) return ''
131 if (video.privacy.id !== VideoPrivacy.PRIVATE && video.state.id === VideoState.PUBLISHED) {
132 return this.i18n('Published')
135 if (video.scheduledUpdate) {
136 const updateAt = new Date(video.scheduledUpdate.updateAt.toString()).toLocaleString(this.localeId)
137 return this.i18n('Publication scheduled on ') + updateAt
140 if (video.state.id === VideoState.TO_TRANSCODE && video.waitTranscoding === true) {
141 return this.i18n('Waiting transcoding')
144 if (video.state.id === VideoState.TO_TRANSCODE) {
145 return this.i18n('To transcode')
148 if (video.state.id === VideoState.TO_IMPORT) {
149 return this.i18n('To import')
156 if (this.displayVideoActions) this.showActions = true
158 this.loadWatchLater()
161 onVideoBlacklisted () {
162 this.videoBlacklisted.emit()
165 onVideoUnblacklisted () {
166 this.videoUnblacklisted.emit()
170 this.videoRemoved.emit()
174 return this.authService.isLoggedIn()
177 onWatchLaterClick (currentState: boolean) {
178 if (currentState === true) this.removeFromWatchLater()
179 else this.addToWatchLater()
181 this.inWatchLaterPlaylist = !currentState
185 const body = { videoId: this.video.id }
187 this.videoPlaylistService.addVideoInPlaylist(this.watchLaterPlaylist.id, body).subscribe(
189 this.watchLaterPlaylist.playlistElementId = res.videoPlaylistElement.id
194 removeFromWatchLater () {
195 this.videoPlaylistService.removeVideoFromPlaylist(this.watchLaterPlaylist.id, this.watchLaterPlaylist.playlistElementId, this.video.id)
201 isWatchLaterPlaylistDisplayed () {
202 return this.inWatchLaterPlaylist !== undefined
206 if (this.ownerDisplayType === 'account' || this.ownerDisplayType === 'videoChannel') {
207 this.ownerDisplayTypeChosen = this.ownerDisplayType
211 // If the video channel name an UUID (not really displayable, we changed this behaviour in v1.0.0-beta.12)
212 // -> Use the account name
214 this.video.channel.name === `${this.video.account.name}_channel` ||
215 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}$/)
217 this.ownerDisplayTypeChosen = 'account'
219 this.ownerDisplayTypeChosen = 'videoChannel'
223 private loadWatchLater () {
224 if (!this.isUserLoggedIn() || this.inWatchLaterPlaylist !== undefined) return
226 this.authService.userInformationLoaded
227 .pipe(switchMap(() => this.videoPlaylistService.listenToVideoPlaylistChange(this.video.id)))
228 .subscribe(existResult => {
229 const watchLaterPlaylist = this.authService.getUser().specialPlaylists.find(p => p.type === VideoPlaylistType.WATCH_LATER)
230 const existsInWatchLater = existResult.find(r => r.playlistId === watchLaterPlaylist.id)
231 this.inWatchLaterPlaylist = false
233 this.watchLaterPlaylist = {
234 id: watchLaterPlaylist.id
237 if (existsInWatchLater) {
238 this.inWatchLaterPlaylist = true
239 this.watchLaterPlaylist.playlistElementId = existsInWatchLater.playlistElementId
242 this.cd.markForCheck()
245 this.videoPlaylistService.runPlaylistCheck(this.video.id)