1 import { Component, Input } from '@angular/core'
2 import { Router } from '@angular/router'
3 import { AuthService, ComponentPagination, LocalStorageService, Notifier, SessionStorageService, UserService } from '@app/core'
4 import { peertubeLocalStorage, peertubeSessionStorage } from '@app/helpers/peertube-web-storage'
5 import { VideoPlaylist, VideoPlaylistElement, VideoPlaylistService } from '@app/shared/shared-video-playlist'
6 import { I18n } from '@ngx-translate/i18n-polyfill'
7 import { VideoDetails, VideoPlaylistPrivacy } from '@shared/models'
10 selector: 'my-video-watch-playlist',
11 templateUrl: './video-watch-playlist.component.html',
12 styleUrls: [ './video-watch-playlist.component.scss' ]
14 export class VideoWatchPlaylistComponent {
15 static LOCAL_STORAGE_AUTO_PLAY_NEXT_VIDEO_PLAYLIST = 'auto_play_video_playlist'
16 static SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO_PLAYLIST = 'loop_playlist'
18 @Input() video: VideoDetails
19 @Input() playlist: VideoPlaylist
21 playlistElements: VideoPlaylistElement[] = []
22 playlistPagination: ComponentPagination = {
28 autoPlayNextVideoPlaylist: boolean
29 autoPlayNextVideoPlaylistSwitchText = ''
31 loopPlaylistSwitchText = ''
32 noPlaylistVideos = false
33 currentPlaylistPosition = 1
36 private userService: UserService,
37 private auth: AuthService,
38 private notifier: Notifier,
40 private videoPlaylist: VideoPlaylistService,
41 private localStorageService: LocalStorageService,
42 private sessionStorageService: SessionStorageService,
43 private router: Router
46 this.autoPlayNextVideoPlaylist = this.auth.isLoggedIn()
47 ? this.auth.getUser().autoPlayNextVideoPlaylist
48 : this.localStorageService.getItem(VideoWatchPlaylistComponent.LOCAL_STORAGE_AUTO_PLAY_NEXT_VIDEO_PLAYLIST) !== 'false'
49 this.setAutoPlayNextVideoPlaylistSwitchText()
52 this.loopPlaylist = this.sessionStorageService.getItem(VideoWatchPlaylistComponent.SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO_PLAYLIST) === 'true'
53 this.setLoopPlaylistSwitchText()
56 onPlaylistVideosNearOfBottom () {
58 if (this.playlistPagination.totalItems <= (this.playlistPagination.currentPage * this.playlistPagination.itemsPerPage)) return
60 this.playlistPagination.currentPage += 1
61 this.loadPlaylistElements(this.playlist,false)
64 onElementRemoved (playlistElement: VideoPlaylistElement) {
65 this.playlistElements = this.playlistElements.filter(e => e.id !== playlistElement.id)
67 this.playlistPagination.totalItems--
71 return this.playlist.isLocal === true &&
72 this.auth.isLoggedIn() &&
73 this.playlist.ownerAccount.name === this.auth.getUser().username
76 isUnlistedPlaylist () {
77 return this.playlist.privacy.id === VideoPlaylistPrivacy.UNLISTED
80 isPrivatePlaylist () {
81 return this.playlist.privacy.id === VideoPlaylistPrivacy.PRIVATE
85 return this.playlist.privacy.id === VideoPlaylistPrivacy.PUBLIC
88 loadPlaylistElements (playlist: VideoPlaylist, redirectToFirst = false) {
89 this.videoPlaylist.getPlaylistVideos(playlist.uuid, this.playlistPagination)
90 .subscribe(({ total, data }) => {
91 this.playlistElements = this.playlistElements.concat(data)
92 this.playlistPagination.totalItems = total
94 const firstAvailableVideos = this.playlistElements.find(e => !!e.video)
95 if (!firstAvailableVideos) {
96 this.noPlaylistVideos = true
100 this.updatePlaylistIndex(this.video)
102 if (redirectToFirst) {
105 start: firstAvailableVideos.startTimestamp,
106 stop: firstAvailableVideos.stopTimestamp,
107 videoId: firstAvailableVideos.video.uuid
111 this.router.navigate([], extras)
116 updatePlaylistIndex (video: VideoDetails) {
117 if (this.playlistElements.length === 0 || !video) return
119 for (const playlistElement of this.playlistElements) {
120 if (playlistElement.video && playlistElement.video.id === video.id) {
121 this.currentPlaylistPosition = playlistElement.position
126 // Load more videos to find our video
127 this.onPlaylistVideosNearOfBottom()
130 findNextPlaylistVideo (position = this.currentPlaylistPosition): VideoPlaylistElement {
131 if (this.currentPlaylistPosition >= this.playlistPagination.totalItems) {
132 // we have reached the end of the playlist: either loop or stop
133 if (this.loopPlaylist) {
134 this.currentPlaylistPosition = position = 0
140 const next = this.playlistElements.find(e => e.position === position)
142 if (!next || !next.video) {
143 return this.findNextPlaylistVideo(position + 1)
149 navigateToNextPlaylistVideo () {
150 const next = this.findNextPlaylistVideo(this.currentPlaylistPosition + 1)
152 const start = next.startTimestamp
153 const stop = next.stopTimestamp
154 this.router.navigate([],{ queryParams: { videoId: next.video.uuid, start, stop } })
157 switchAutoPlayNextVideoPlaylist () {
158 this.autoPlayNextVideoPlaylist = !this.autoPlayNextVideoPlaylist
159 this.setAutoPlayNextVideoPlaylistSwitchText()
161 peertubeLocalStorage.setItem(
162 VideoWatchPlaylistComponent.LOCAL_STORAGE_AUTO_PLAY_NEXT_VIDEO_PLAYLIST,
163 this.autoPlayNextVideoPlaylist.toString()
166 if (this.auth.isLoggedIn()) {
168 autoPlayNextVideoPlaylist: this.autoPlayNextVideoPlaylist
171 this.userService.updateMyProfile(details).subscribe(
173 this.auth.refreshUserInformation()
175 err => this.notifier.error(err.message)
180 switchLoopPlaylist () {
181 this.loopPlaylist = !this.loopPlaylist
182 this.setLoopPlaylistSwitchText()
184 peertubeSessionStorage.setItem(
185 VideoWatchPlaylistComponent.SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO_PLAYLIST,
186 this.loopPlaylist.toString()
190 private setAutoPlayNextVideoPlaylistSwitchText () {
191 this.autoPlayNextVideoPlaylistSwitchText = this.autoPlayNextVideoPlaylist
192 ? this.i18n('Stop autoplaying next video')
193 : this.i18n('Autoplay next video')
196 private setLoopPlaylistSwitchText () {
197 this.loopPlaylistSwitchText = this.loopPlaylist
198 ? this.i18n('Stop looping playlist videos')
199 : this.i18n('Loop playlist videos')