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