]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/videos/+video-watch/video-watch-playlist.component.ts
make playlists ignore user history
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / +video-watch / video-watch-playlist.component.ts
CommitLineData
72675ebe
C
1import { Component, Input } from '@angular/core'
2import { VideoPlaylist } from '@app/shared/video-playlist/video-playlist.model'
3import { ComponentPagination } from '@app/shared/rest/component-pagination.model'
72675ebe 4import { VideoDetails, VideoPlaylistPrivacy } from '@shared/models'
72675ebe 5import { Router } from '@angular/router'
bee29df8
RK
6import { User, UserService } from '@app/shared'
7import { AuthService, Notifier } from '@app/core'
bfbd9128
C
8import { VideoPlaylistService } from '@app/shared/video-playlist/video-playlist.service'
9import { VideoPlaylistElement } from '@app/shared/video-playlist/video-playlist-element.model'
bee29df8
RK
10import { peertubeLocalStorage } from '@app/shared/misc/peertube-local-storage'
11import { I18n } from '@ngx-translate/i18n-polyfill'
72675ebe
C
12
13@Component({
14 selector: 'my-video-watch-playlist',
15 templateUrl: './video-watch-playlist.component.html',
16 styleUrls: [ './video-watch-playlist.component.scss' ]
17})
18export class VideoWatchPlaylistComponent {
bee29df8
RK
19 static LOCAL_STORAGE_AUTO_PLAY_NEXT_VIDEO_PLAYLIST = 'auto_play_video_playlist'
20
72675ebe
C
21 @Input() video: VideoDetails
22 @Input() playlist: VideoPlaylist
23
bfbd9128 24 playlistElements: VideoPlaylistElement[] = []
72675ebe
C
25 playlistPagination: ComponentPagination = {
26 currentPage: 1,
27 itemsPerPage: 30,
28 totalItems: null
29 }
30
bee29df8
RK
31 autoPlayNextVideoPlaylist: boolean
32 autoPlayNextVideoPlaylistSwitchText = ''
72675ebe
C
33 noPlaylistVideos = false
34 currentPlaylistPosition = 1
35
36 constructor (
bee29df8 37 private userService: UserService,
72675ebe 38 private auth: AuthService,
bee29df8
RK
39 private notifier: Notifier,
40 private i18n: I18n,
bfbd9128 41 private videoPlaylist: VideoPlaylistService,
72675ebe 42 private router: Router
bee29df8
RK
43 ) {
44 this.autoPlayNextVideoPlaylist = this.auth.isLoggedIn()
45 ? this.auth.getUser().autoPlayNextVideoPlaylist
46 : peertubeLocalStorage.getItem(VideoWatchPlaylistComponent.LOCAL_STORAGE_AUTO_PLAY_NEXT_VIDEO_PLAYLIST) !== 'false'
47 this.setAutoPlayNextVideoPlaylistSwitchText()
48 }
72675ebe
C
49
50 onPlaylistVideosNearOfBottom () {
51 // Last page
52 if (this.playlistPagination.totalItems <= (this.playlistPagination.currentPage * this.playlistPagination.itemsPerPage)) return
53
54 this.playlistPagination.currentPage += 1
55 this.loadPlaylistElements(this.playlist,false)
56 }
57
bfbd9128
C
58 onElementRemoved (playlistElement: VideoPlaylistElement) {
59 this.playlistElements = this.playlistElements.filter(e => e.id !== playlistElement.id)
72675ebe
C
60
61 this.playlistPagination.totalItems--
62 }
63
64 isPlaylistOwned () {
1acd784c
C
65 return this.playlist.isLocal === true &&
66 this.auth.isLoggedIn() &&
67 this.playlist.ownerAccount.name === this.auth.getUser().username
72675ebe
C
68 }
69
70 isUnlistedPlaylist () {
71 return this.playlist.privacy.id === VideoPlaylistPrivacy.UNLISTED
72 }
73
74 isPrivatePlaylist () {
75 return this.playlist.privacy.id === VideoPlaylistPrivacy.PRIVATE
76 }
77
78 isPublicPlaylist () {
79 return this.playlist.privacy.id === VideoPlaylistPrivacy.PUBLIC
80 }
81
82 loadPlaylistElements (playlist: VideoPlaylist, redirectToFirst = false) {
bfbd9128 83 this.videoPlaylist.getPlaylistVideos(playlist.uuid, this.playlistPagination)
93cae479 84 .subscribe(({ total, data }) => {
bfbd9128 85 this.playlistElements = this.playlistElements.concat(data)
93cae479 86 this.playlistPagination.totalItems = total
72675ebe 87
bfbd9128
C
88 const firstAvailableVideos = this.playlistElements.find(e => !!e.video)
89 if (!firstAvailableVideos) {
72675ebe
C
90 this.noPlaylistVideos = true
91 return
92 }
93
94 this.updatePlaylistIndex(this.video)
95
96 if (redirectToFirst) {
97 const extras = {
f1d9b2d6
C
98 queryParams: {
99 start: firstAvailableVideos.startTimestamp,
100 stop: firstAvailableVideos.stopTimestamp,
101 videoId: firstAvailableVideos.video.uuid
102 },
72675ebe
C
103 replaceUrl: true
104 }
105 this.router.navigate([], extras)
106 }
107 })
108 }
109
110 updatePlaylistIndex (video: VideoDetails) {
bfbd9128 111 if (this.playlistElements.length === 0 || !video) return
72675ebe 112
bfbd9128
C
113 for (const playlistElement of this.playlistElements) {
114 if (playlistElement.video && playlistElement.video.id === video.id) {
115 this.currentPlaylistPosition = playlistElement.position
72675ebe
C
116 return
117 }
118 }
119
120 // Load more videos to find our video
121 this.onPlaylistVideosNearOfBottom()
122 }
123
124 navigateToNextPlaylistVideo () {
125 if (this.currentPlaylistPosition < this.playlistPagination.totalItems) {
bfbd9128 126 const next = this.playlistElements.find(e => e.position === this.currentPlaylistPosition + 1)
72675ebe 127
bfbd9128
C
128 if (!next || !next.video) {
129 this.currentPlaylistPosition++
130 this.navigateToNextPlaylistVideo()
131 return
132 }
133
134 const start = next.startTimestamp
135 const stop = next.stopTimestamp
136 this.router.navigate([],{ queryParams: { videoId: next.video.uuid, start, stop } })
72675ebe
C
137 }
138 }
bee29df8
RK
139
140 switchAutoPlayNextVideoPlaylist () {
141 this.autoPlayNextVideoPlaylist = !this.autoPlayNextVideoPlaylist
142 this.setAutoPlayNextVideoPlaylistSwitchText()
143
144 peertubeLocalStorage.setItem(
145 VideoWatchPlaylistComponent.LOCAL_STORAGE_AUTO_PLAY_NEXT_VIDEO_PLAYLIST,
146 this.autoPlayNextVideoPlaylist.toString()
147 )
148
149 if (this.auth.isLoggedIn()) {
150 const details = {
151 autoPlayNextVideoPlaylist: this.autoPlayNextVideoPlaylist
152 }
153
154 this.userService.updateMyProfile(details).subscribe(
155 () => {
156 this.auth.refreshUserInformation()
157 },
158 err => this.notifier.error(err.message)
159 )
160 }
161 }
162
163 private setAutoPlayNextVideoPlaylistSwitchText () {
164 this.autoPlayNextVideoPlaylistSwitchText = this.i18n('{{verb}} autoplay for playlists', {
165 verb: this.autoPlayNextVideoPlaylist ? this.i18n('Disable') : this.i18n('Enable')
166 })
167 }
72675ebe 168}