]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - 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
1import { Component, Input, OnInit, ChangeDetectorRef } from '@angular/core'
2import { Video } from './video.model'
3import { ScreenService } from '@app/shared/misc/screen.service'
4import { AuthService, ThemeService } from '@app/core'
5import { VideoPlaylistService } from '../video-playlist/video-playlist.service'
6import { VideoPlaylistElementCreate } from '../../../../../shared'
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
16 @Input() routerLink: any[]
17 @Input() queryParams: any[]
18
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
34 if (!this.isUserLoggedIn()) return
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 )
58 }
59
60 getImageUrl () {
61 if (!this.video) return ''
62
63 if (this.screenService.isInMobileView()) {
64 return this.video.previewUrl
65 }
66
67 return this.video.thumbnailUrl
68 }
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 }
77
78 getVideoRouterLink () {
79 if (this.routerLink) return this.routerLink
80
81 return [ '/videos/watch', this.video.uuid ]
82 }
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 }
116}