]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/shared-video-miniature/video-miniature.component.ts
Merge branch 'develop' into shorter-URLs-channels-accounts
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-video-miniature / 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, ScreenService, ServerService, User } from '@app/core'
14 import { ServerConfig, VideoPlaylistType, VideoPrivacy, VideoState } from '@shared/models'
15 import { ActorAvatarSize } from '../shared-actor-image/actor-avatar.component'
16 import { Video } from '../shared-main'
17 import { VideoPlaylistService } from '../shared-video-playlist'
18 import { VideoActionsDisplayType } from './video-actions-dropdown.component'
19
20 export type MiniatureDisplayOptions = {
21 date?: boolean
22 views?: boolean
23 by?: boolean
24 avatar?: boolean
25 privacyLabel?: boolean
26 privacyText?: boolean
27 state?: boolean
28 blacklistInfo?: boolean
29 nsfw?: boolean
30 }
31 export type VideoLinkType = 'internal' | 'lazy-load' | 'external'
32
33 @Component({
34 selector: 'my-video-miniature',
35 styleUrls: [ './video-miniature.component.scss' ],
36 templateUrl: './video-miniature.component.html',
37 changeDetection: ChangeDetectionStrategy.OnPush
38 })
39 export class VideoMiniatureComponent implements OnInit {
40 @Input() user: User
41 @Input() video: Video
42
43 @Input() displayOptions: MiniatureDisplayOptions = {
44 date: true,
45 views: true,
46 by: true,
47 avatar: false,
48 privacyLabel: false,
49 privacyText: false,
50 state: false,
51 blacklistInfo: false
52 }
53 @Input() displayVideoActions = true
54
55 @Input() actorImageSize: ActorAvatarSize = '40'
56
57 @Input() displayAsRow = false
58
59 @Input() videoLinkType: VideoLinkType = 'internal'
60
61 @Output() videoBlocked = new EventEmitter()
62 @Output() videoUnblocked = new EventEmitter()
63 @Output() videoRemoved = new EventEmitter()
64 @Output() videoAccountMuted = 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 mute: true
75 }
76 showActions = false
77 serverConfig: ServerConfig
78
79 addToWatchLaterText: string
80 addedToWatchLaterText: string
81 inWatchLaterPlaylist: boolean
82 channelLinkTitle = ''
83
84 watchLaterPlaylist: {
85 id: number
86 playlistElementId?: number
87 }
88
89 videoRouterLink: any[] = []
90 videoHref: string
91 videoTarget: string
92
93 private ownerDisplayType: 'account' | 'videoChannel'
94
95 constructor (
96 private screenService: ScreenService,
97 private serverService: ServerService,
98 private authService: AuthService,
99 private videoPlaylistService: VideoPlaylistService,
100 private cd: ChangeDetectorRef,
101 @Inject(LOCALE_ID) private localeId: string
102 ) {}
103
104 get isVideoBlur () {
105 return this.video.isVideoNSFWForUser(this.user, this.serverConfig)
106 }
107
108 ngOnInit () {
109 this.serverConfig = this.serverService.getTmpConfig()
110 this.serverService.getConfig()
111 .subscribe(config => {
112 this.serverConfig = config
113 this.buildVideoLink()
114 })
115
116 this.setUpBy()
117
118 this.channelLinkTitle = $localize`${this.video.channel.name} (channel page)`
119
120 // We rely on mouseenter to lazy load actions
121 if (this.screenService.isInTouchScreen()) {
122 this.loadActions()
123 }
124 }
125
126 buildVideoLink () {
127 if (this.videoLinkType === 'internal' || !this.video.url) {
128 this.videoRouterLink = [ '/videos/watch', this.video.uuid ]
129 return
130 }
131
132 if (this.videoLinkType === 'external') {
133 this.videoRouterLink = null
134 this.videoHref = this.video.url
135 this.videoTarget = '_blank'
136 return
137 }
138
139 // Lazy load
140 this.videoRouterLink = [ '/search/lazy-load-video', { url: this.video.url } ]
141 }
142
143 displayOwnerAccount () {
144 return this.ownerDisplayType === 'account'
145 }
146
147 displayOwnerVideoChannel () {
148 return this.ownerDisplayType === 'videoChannel'
149 }
150
151 isUnlistedVideo () {
152 return this.video.privacy.id === VideoPrivacy.UNLISTED
153 }
154
155 isPrivateVideo () {
156 return this.video.privacy.id === VideoPrivacy.PRIVATE
157 }
158
159 getStateLabel (video: Video) {
160 if (!video.state) return ''
161
162 if (video.privacy.id !== VideoPrivacy.PRIVATE && video.state.id === VideoState.PUBLISHED) {
163 return $localize`Published`
164 }
165
166 if (video.scheduledUpdate) {
167 const updateAt = new Date(video.scheduledUpdate.updateAt.toString()).toLocaleString(this.localeId)
168 return $localize`Publication scheduled on ` + updateAt
169 }
170
171 if (video.state.id === VideoState.TO_TRANSCODE && video.waitTranscoding === true) {
172 return $localize`Waiting transcoding`
173 }
174
175 if (video.state.id === VideoState.TO_TRANSCODE) {
176 return $localize`To transcode`
177 }
178
179 if (video.state.id === VideoState.TO_IMPORT) {
180 return $localize`To import`
181 }
182
183 return ''
184 }
185
186 loadActions () {
187 if (this.displayVideoActions) this.showActions = true
188
189 this.loadWatchLater()
190 }
191
192 onVideoBlocked () {
193 this.videoBlocked.emit()
194 }
195
196 onVideoUnblocked () {
197 this.videoUnblocked.emit()
198 }
199
200 onVideoRemoved () {
201 this.videoRemoved.emit()
202 }
203
204 onVideoAccountMuted () {
205 this.videoAccountMuted.emit()
206 }
207
208 isUserLoggedIn () {
209 return this.authService.isLoggedIn()
210 }
211
212 onWatchLaterClick (currentState: boolean) {
213 if (currentState === true) this.removeFromWatchLater()
214 else this.addToWatchLater()
215
216 this.inWatchLaterPlaylist = !currentState
217 }
218
219 addToWatchLater () {
220 const body = { videoId: this.video.id }
221
222 this.videoPlaylistService.addVideoInPlaylist(this.watchLaterPlaylist.id, body).subscribe(
223 res => {
224 this.watchLaterPlaylist.playlistElementId = res.videoPlaylistElement.id
225 }
226 )
227 }
228
229 removeFromWatchLater () {
230 this.videoPlaylistService.removeVideoFromPlaylist(this.watchLaterPlaylist.id, this.watchLaterPlaylist.playlistElementId, this.video.id)
231 .subscribe(
232 _ => { /* empty */ }
233 )
234 }
235
236 isWatchLaterPlaylistDisplayed () {
237 return this.displayVideoActions && this.isUserLoggedIn() && this.inWatchLaterPlaylist !== undefined
238 }
239
240 getClasses () {
241 return {
242 'display-as-row': this.displayAsRow
243 }
244 }
245
246 private setUpBy () {
247 const accountName = this.video.account.name
248
249 // If the video channel name is an UUID (not really displayable, we changed this behaviour in v1.0.0-beta.12)
250 // Or has not been customized (default created channel display name)
251 // -> Use the account name
252 if (
253 this.video.channel.displayName === `Default ${accountName} channel` ||
254 this.video.channel.displayName === `Main ${accountName} channel` ||
255 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}$/)
256 ) {
257 this.ownerDisplayType = 'account'
258 } else {
259 this.ownerDisplayType = 'videoChannel'
260 }
261 }
262
263 private loadWatchLater () {
264 if (!this.isUserLoggedIn() || this.inWatchLaterPlaylist !== undefined) return
265
266 this.authService.userInformationLoaded
267 .pipe(switchMap(() => this.videoPlaylistService.listenToVideoPlaylistChange(this.video.id)))
268 .subscribe(existResult => {
269 const watchLaterPlaylist = this.authService.getUser().specialPlaylists.find(p => p.type === VideoPlaylistType.WATCH_LATER)
270 const existsInWatchLater = existResult.find(r => r.playlistId === watchLaterPlaylist.id)
271 this.inWatchLaterPlaylist = false
272
273 this.watchLaterPlaylist = {
274 id: watchLaterPlaylist.id
275 }
276
277 if (existsInWatchLater) {
278 this.inWatchLaterPlaylist = true
279 this.watchLaterPlaylist.playlistElementId = existsInWatchLater.playlistElementId
280 }
281
282 this.cd.markForCheck()
283 })
284
285 this.videoPlaylistService.runPlaylistCheck(this.video.id)
286 }
287 }