]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/video/video-miniature.component.ts
Limit thumbnail sizes
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / video / video-miniature.component.ts
1 import { switchMap } from 'rxjs/operators'
2 import {
3 ChangeDetectionStrategy,
4 ChangeDetectorRef,
5 Component,
6 EventEmitter,
7 Inject,
8 Input,
9 LOCALE_ID,
10 OnInit,
11 Output
12 } from '@angular/core'
13 import { AuthService, ServerService } from '@app/core'
14 import { ScreenService } from '@app/shared/misc/screen.service'
15 import { VideoPlaylistService } from '@app/shared/video-playlist/video-playlist.service'
16 import { VideoActionsDisplayType } from '@app/shared/video/video-actions-dropdown.component'
17 import { I18n } from '@ngx-translate/i18n-polyfill'
18 import { ServerConfig, VideoPlaylistType, VideoPrivacy, VideoState } from '../../../../../shared'
19 import { User } from '../users'
20 import { Video } from './video.model'
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 @Input() useLazyLoadUrl = false
61
62 @Output() videoBlocked = new EventEmitter()
63 @Output() videoUnblocked = new EventEmitter()
64 @Output() videoRemoved = new EventEmitter()
65
66 videoActionsDisplayOptions: VideoActionsDisplayType = {
67 playlist: true,
68 download: false,
69 update: true,
70 blacklist: true,
71 delete: true,
72 report: true,
73 duplicate: true
74 }
75 showActions = false
76 serverConfig: ServerConfig
77
78 addToWatchLaterText: string
79 addedToWatchLaterText: string
80 inWatchLaterPlaylist: boolean
81
82 watchLaterPlaylist: {
83 id: number
84 playlistElementId?: number
85 }
86
87 videoLink: any[] = []
88
89 private ownerDisplayTypeChosen: 'account' | 'videoChannel'
90
91 constructor (
92 private screenService: ScreenService,
93 private serverService: ServerService,
94 private i18n: I18n,
95 private authService: AuthService,
96 private videoPlaylistService: VideoPlaylistService,
97 private cd: ChangeDetectorRef,
98 @Inject(LOCALE_ID) private localeId: string
99 ) {
100
101 }
102
103 get isVideoBlur () {
104 return this.video.isVideoNSFWForUser(this.user, this.serverConfig)
105 }
106
107 ngOnInit () {
108 this.serverConfig = this.serverService.getTmpConfig()
109 this.serverService.getConfig()
110 .subscribe(config => {
111 this.serverConfig = config
112 this.buildVideoLink()
113 })
114
115 this.setUpBy()
116
117 // We rely on mouseenter to lazy load actions
118 if (this.screenService.isInTouchScreen()) {
119 this.loadActions()
120 }
121 }
122
123 buildVideoLink () {
124 if (this.useLazyLoadUrl && this.video.url) {
125 const remoteUriConfig = this.serverConfig.search.remoteUri
126
127 // Redirect on the external instance if not allowed to fetch remote data
128 const externalRedirect = (!this.authService.isLoggedIn() && !remoteUriConfig.anonymous) || !remoteUriConfig.users
129 const fromPath = window.location.pathname + window.location.search
130
131 this.videoLink = [ '/search/lazy-load-video', { url: this.video.url, externalRedirect, fromPath } ]
132 return
133 }
134
135 this.videoLink = [ '/videos/watch', this.video.uuid ]
136 }
137
138 displayOwnerAccount () {
139 return this.ownerDisplayTypeChosen === 'account'
140 }
141
142 displayOwnerVideoChannel () {
143 return this.ownerDisplayTypeChosen === 'videoChannel'
144 }
145
146 isUnlistedVideo () {
147 return this.video.privacy.id === VideoPrivacy.UNLISTED
148 }
149
150 isPrivateVideo () {
151 return this.video.privacy.id === VideoPrivacy.PRIVATE
152 }
153
154 getStateLabel (video: Video) {
155 if (!video.state) return ''
156
157 if (video.privacy.id !== VideoPrivacy.PRIVATE && video.state.id === VideoState.PUBLISHED) {
158 return this.i18n('Published')
159 }
160
161 if (video.scheduledUpdate) {
162 const updateAt = new Date(video.scheduledUpdate.updateAt.toString()).toLocaleString(this.localeId)
163 return this.i18n('Publication scheduled on ') + updateAt
164 }
165
166 if (video.state.id === VideoState.TO_TRANSCODE && video.waitTranscoding === true) {
167 return this.i18n('Waiting transcoding')
168 }
169
170 if (video.state.id === VideoState.TO_TRANSCODE) {
171 return this.i18n('To transcode')
172 }
173
174 if (video.state.id === VideoState.TO_IMPORT) {
175 return this.i18n('To import')
176 }
177
178 return ''
179 }
180
181 getAvatarUrl () {
182 if (this.ownerDisplayTypeChosen === 'account') {
183 return this.video.accountAvatarUrl
184 }
185
186 return this.video.videoChannelAvatarUrl
187 }
188
189 loadActions () {
190 if (this.displayVideoActions) this.showActions = true
191
192 this.loadWatchLater()
193 }
194
195 onVideoBlocked () {
196 this.videoBlocked.emit()
197 }
198
199 onVideoUnblocked () {
200 this.videoUnblocked.emit()
201 }
202
203 onVideoRemoved () {
204 this.videoRemoved.emit()
205 }
206
207 isUserLoggedIn () {
208 return this.authService.isLoggedIn()
209 }
210
211 onWatchLaterClick (currentState: boolean) {
212 if (currentState === true) this.removeFromWatchLater()
213 else this.addToWatchLater()
214
215 this.inWatchLaterPlaylist = !currentState
216 }
217
218 addToWatchLater () {
219 const body = { videoId: this.video.id }
220
221 this.videoPlaylistService.addVideoInPlaylist(this.watchLaterPlaylist.id, body).subscribe(
222 res => {
223 this.watchLaterPlaylist.playlistElementId = res.videoPlaylistElement.id
224 }
225 )
226 }
227
228 removeFromWatchLater () {
229 this.videoPlaylistService.removeVideoFromPlaylist(this.watchLaterPlaylist.id, this.watchLaterPlaylist.playlistElementId, this.video.id)
230 .subscribe(
231 _ => { /* empty */ }
232 )
233 }
234
235 isWatchLaterPlaylistDisplayed () {
236 return this.displayVideoActions && this.isUserLoggedIn() && this.inWatchLaterPlaylist !== undefined
237 }
238
239 private setUpBy () {
240 if (this.ownerDisplayType === 'account' || this.ownerDisplayType === 'videoChannel') {
241 this.ownerDisplayTypeChosen = this.ownerDisplayType
242 return
243 }
244
245 // If the video channel name an UUID (not really displayable, we changed this behaviour in v1.0.0-beta.12)
246 // -> Use the account name
247 if (
248 this.video.channel.name === `${this.video.account.name}_channel` ||
249 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}$/)
250 ) {
251 this.ownerDisplayTypeChosen = 'account'
252 } else {
253 this.ownerDisplayTypeChosen = 'videoChannel'
254 }
255 }
256
257 private loadWatchLater () {
258 if (!this.isUserLoggedIn() || this.inWatchLaterPlaylist !== undefined) return
259
260 this.authService.userInformationLoaded
261 .pipe(switchMap(() => this.videoPlaylistService.listenToVideoPlaylistChange(this.video.id)))
262 .subscribe(existResult => {
263 const watchLaterPlaylist = this.authService.getUser().specialPlaylists.find(p => p.type === VideoPlaylistType.WATCH_LATER)
264 const existsInWatchLater = existResult.find(r => r.playlistId === watchLaterPlaylist.id)
265 this.inWatchLaterPlaylist = false
266
267 this.watchLaterPlaylist = {
268 id: watchLaterPlaylist.id
269 }
270
271 if (existsInWatchLater) {
272 this.inWatchLaterPlaylist = true
273 this.watchLaterPlaylist.playlistElementId = existsInWatchLater.playlistElementId
274 }
275
276 this.cd.markForCheck()
277 })
278
279 this.videoPlaylistService.runPlaylistCheck(this.video.id)
280 }
281 }