]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/video/video-thumbnail.component.ts
Improve SQL query for my special playlists
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / video / video-thumbnail.component.ts
CommitLineData
29128b2f 1import { Component, Input, OnInit, ChangeDetectorRef } from '@angular/core'
202f6b6c 2import { Video } from './video.model'
bbe0f064 3import { ScreenService } from '@app/shared/misc/screen.service'
29128b2f
RK
4import { AuthService, ThemeService } from '@app/core'
5import { VideoPlaylistService } from '../video-playlist/video-playlist.service'
29128b2f 6import { VideoPlaylistElementCreate } from '../../../../../shared'
202f6b6c
C
7
8@Component({
9 selector: 'my-video-thumbnail',
10 styleUrls: [ './video-thumbnail.component.scss' ],
11 templateUrl: './video-thumbnail.component.html'
12})
13export class VideoThumbnailComponent {
14 @Input() video: Video
15 @Input() nsfw = false
e2f01c47
C
16 @Input() routerLink: any[]
17 @Input() queryParams: any[]
3290f37c 18
29128b2f
RK
19 addToWatchLaterText = 'Add to watch later'
20 addedToWatchLaterText = 'Added to watch later'
21 addedToWatchLater: boolean
22
23 watchLaterPlaylist: any
24
25 constructor (
26 private screenService: ScreenService,
27 private authService: AuthService,
28 private videoPlaylistService: VideoPlaylistService,
29 private cd: ChangeDetectorRef
30 ) {}
31
32 load () {
33 if (this.addedToWatchLater !== undefined) return
3fbba1d2 34 if (!this.isUserLoggedIn()) return
29128b2f
RK
35
36 this.videoPlaylistService.doesVideoExistInPlaylist(this.video.id)
37 .subscribe(
38 existResult => {
39 for (const playlist of this.authService.getUser().specialPlaylists) {
40 const existingPlaylist = existResult[ this.video.id ].find(p => p.playlistId === playlist.id)
41 this.addedToWatchLater = !!existingPlaylist
42
43 if (existingPlaylist) {
44 this.watchLaterPlaylist = {
45 playlistId: existingPlaylist.playlistId,
46 playlistElementId: existingPlaylist.playlistElementId
47 }
48 } else {
49 this.watchLaterPlaylist = {
50 playlistId: playlist.id
51 }
52 }
53
54 this.cd.markForCheck()
55 }
56 }
57 )
e2f01c47 58 }
bbe0f064 59
3290f37c
C
60 getImageUrl () {
61 if (!this.video) return ''
62
bbe0f064 63 if (this.screenService.isInMobileView()) {
3290f37c
C
64 return this.video.previewUrl
65 }
66
67 return this.video.thumbnailUrl
68 }
6e46de09
C
69
70 getProgressPercent () {
71 if (!this.video.userHistory) return 0
72
73 const currentTime = this.video.userHistory.currentTime
74
75 return (currentTime / this.video.duration) * 100
76 }
e2f01c47
C
77
78 getVideoRouterLink () {
79 if (this.routerLink) return this.routerLink
80
81 return [ '/videos/watch', this.video.uuid ]
82 }
29128b2f
RK
83
84 isUserLoggedIn () {
85 return this.authService.isLoggedIn()
86 }
87
88 addToWatchLater () {
89 if (this.addedToWatchLater === undefined) return
90 this.addedToWatchLater = true
91
92 this.videoPlaylistService.addVideoInPlaylist(
93 this.watchLaterPlaylist.playlistId,
94 { videoId: this.video.id } as VideoPlaylistElementCreate
95 ).subscribe(
96 res => {
97 this.addedToWatchLater = true
98 this.watchLaterPlaylist.playlistElementId = res.videoPlaylistElement.id
99 }
100 )
101 }
102
103 removeFromWatchLater () {
104 if (this.addedToWatchLater === undefined) return
105 this.addedToWatchLater = false
106
107 this.videoPlaylistService.removeVideoFromPlaylist(
108 this.watchLaterPlaylist.playlistId,
109 this.watchLaterPlaylist.playlistElementId
110 ).subscribe(
111 _ => {
112 this.addedToWatchLater = false
113 }
114 )
115 }
202f6b6c 116}