a1d4f0e81b1485e368a0812ff587c2dafea4778c
[oweals/peertube.git] / client / src / app / shared / video / video-miniature.component.ts
1 import {
2   ChangeDetectionStrategy,
3   ChangeDetectorRef,
4   Component,
5   EventEmitter,
6   Inject,
7   Input,
8   LOCALE_ID,
9   OnInit,
10   Output
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'
21
22 export type OwnerDisplayType = 'account' | 'videoChannel' | 'auto'
23 export type MiniatureDisplayOptions = {
24   date?: boolean
25   views?: boolean
26   by?: boolean
27   avatar?: boolean
28   privacyLabel?: boolean
29   privacyText?: boolean
30   state?: boolean
31   blacklistInfo?: boolean
32   nsfw?: boolean
33 }
34
35 @Component({
36   selector: 'my-video-miniature',
37   styleUrls: [ './video-miniature.component.scss' ],
38   templateUrl: './video-miniature.component.html',
39   changeDetection: ChangeDetectionStrategy.OnPush
40 })
41 export class VideoMiniatureComponent implements OnInit {
42   @Input() user: User
43   @Input() video: Video
44
45   @Input() ownerDisplayType: OwnerDisplayType = 'account'
46   @Input() displayOptions: MiniatureDisplayOptions = {
47     date: true,
48     views: true,
49     by: true,
50     avatar: false,
51     privacyLabel: false,
52     privacyText: false,
53     state: false,
54     blacklistInfo: false
55   }
56   @Input() displayAsRow = false
57   @Input() displayVideoActions = true
58   @Input() fitWidth = false
59
60   @Output() videoBlacklisted = new EventEmitter()
61   @Output() videoUnblacklisted = new EventEmitter()
62   @Output() videoRemoved = new EventEmitter()
63
64   videoActionsDisplayOptions: VideoActionsDisplayType = {
65     playlist: true,
66     download: false,
67     update: true,
68     blacklist: true,
69     delete: true,
70     report: true,
71     duplicate: true
72   }
73   showActions = false
74   serverConfig: ServerConfig
75
76   addToWatchLaterText: string
77   addedToWatchLaterText: string
78   inWatchLaterPlaylist: boolean
79
80   watchLaterPlaylist: {
81     id: number
82     playlistElementId?: number
83   }
84
85   private ownerDisplayTypeChosen: 'account' | 'videoChannel'
86
87   constructor (
88     private screenService: ScreenService,
89     private serverService: ServerService,
90     private i18n: I18n,
91     private authService: AuthService,
92     private videoPlaylistService: VideoPlaylistService,
93     private cd: ChangeDetectorRef,
94     @Inject(LOCALE_ID) private localeId: string
95   ) {
96
97   }
98
99   get isVideoBlur () {
100     return this.video.isVideoNSFWForUser(this.user, this.serverConfig)
101   }
102
103   ngOnInit () {
104     this.serverConfig = this.serverService.getTmpConfig()
105     this.serverService.getConfig()
106         .subscribe(config => this.serverConfig = config)
107
108     this.setUpBy()
109
110     // We rely on mouseenter to lazy load actions
111     if (this.screenService.isInTouchScreen()) {
112       this.loadActions()
113     }
114   }
115
116   displayOwnerAccount () {
117     return this.ownerDisplayTypeChosen === 'account'
118   }
119
120   displayOwnerVideoChannel () {
121     return this.ownerDisplayTypeChosen === 'videoChannel'
122   }
123
124   isUnlistedVideo () {
125     return this.video.privacy.id === VideoPrivacy.UNLISTED
126   }
127
128   isPrivateVideo () {
129     return this.video.privacy.id === VideoPrivacy.PRIVATE
130   }
131
132   getStateLabel (video: Video) {
133     if (!video.state) return ''
134
135     if (video.privacy.id !== VideoPrivacy.PRIVATE && video.state.id === VideoState.PUBLISHED) {
136       return this.i18n('Published')
137     }
138
139     if (video.scheduledUpdate) {
140       const updateAt = new Date(video.scheduledUpdate.updateAt.toString()).toLocaleString(this.localeId)
141       return this.i18n('Publication scheduled on ') + updateAt
142     }
143
144     if (video.state.id === VideoState.TO_TRANSCODE && video.waitTranscoding === true) {
145       return this.i18n('Waiting transcoding')
146     }
147
148     if (video.state.id === VideoState.TO_TRANSCODE) {
149       return this.i18n('To transcode')
150     }
151
152     if (video.state.id === VideoState.TO_IMPORT) {
153       return this.i18n('To import')
154     }
155
156     return ''
157   }
158
159   loadActions () {
160     if (this.displayVideoActions) this.showActions = true
161
162     this.loadWatchLater()
163   }
164
165   onVideoBlacklisted () {
166     this.videoBlacklisted.emit()
167   }
168
169   onVideoUnblacklisted () {
170     this.videoUnblacklisted.emit()
171   }
172
173   onVideoRemoved () {
174     this.videoRemoved.emit()
175   }
176
177   isUserLoggedIn () {
178     return this.authService.isLoggedIn()
179   }
180
181   onWatchLaterClick (currentState: boolean) {
182     if (currentState === true) this.removeFromWatchLater()
183     else this.addToWatchLater()
184
185     this.inWatchLaterPlaylist = !currentState
186   }
187
188   addToWatchLater () {
189     const body = { videoId: this.video.id }
190
191     this.videoPlaylistService.addVideoInPlaylist(this.watchLaterPlaylist.id, body).subscribe(
192       res => {
193         this.watchLaterPlaylist.playlistElementId = res.videoPlaylistElement.id
194       }
195     )
196   }
197
198   removeFromWatchLater () {
199     this.videoPlaylistService.removeVideoFromPlaylist(this.watchLaterPlaylist.id, this.watchLaterPlaylist.playlistElementId, this.video.id)
200         .subscribe(
201           _ => { /* empty */ }
202         )
203   }
204
205   isWatchLaterPlaylistDisplayed () {
206     return this.isUserLoggedIn() && this.inWatchLaterPlaylist !== undefined
207   }
208
209   private setUpBy () {
210     if (this.ownerDisplayType === 'account' || this.ownerDisplayType === 'videoChannel') {
211       this.ownerDisplayTypeChosen = this.ownerDisplayType
212       return
213     }
214
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
217     if (
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}$/)
220     ) {
221       this.ownerDisplayTypeChosen = 'account'
222     } else {
223       this.ownerDisplayTypeChosen = 'videoChannel'
224     }
225   }
226
227   private loadWatchLater () {
228     if (!this.isUserLoggedIn() || this.inWatchLaterPlaylist !== undefined) return
229
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
236
237           this.watchLaterPlaylist = {
238             id: watchLaterPlaylist.id
239           }
240
241           if (existsInWatchLater) {
242             this.inWatchLaterPlaylist = true
243             this.watchLaterPlaylist.playlistElementId = existsInWatchLater.playlistElementId
244           }
245
246           this.cd.markForCheck()
247         })
248
249     this.videoPlaylistService.runPlaylistCheck(this.video.id)
250   }
251 }