]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/videos/recommendations/recommended-videos.component.ts
autoplay next video switch for both user and visitors
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / recommendations / recommended-videos.component.ts
1 import { Component, Input, Output, OnChanges, EventEmitter } from '@angular/core'
2 import { Observable } from 'rxjs'
3 import { Video } from '@app/shared/video/video.model'
4 import { RecommendationInfo } from '@app/shared/video/recommendation-info.model'
5 import { RecommendedVideosStore } from '@app/videos/recommendations/recommended-videos.store'
6 import { User } from '@app/shared'
7 import { AuthService, Notifier } from '@app/core'
8 import { UserService } from '@app/shared/users/user.service'
9 import { peertubeLocalStorage } from '@app/shared/misc/peertube-local-storage'
10
11 @Component({
12 selector: 'my-recommended-videos',
13 templateUrl: './recommended-videos.component.html',
14 styleUrls: [ './recommended-videos.component.scss' ]
15 })
16 export class RecommendedVideosComponent implements OnChanges {
17 private static LOCAL_STORAGE_AUTO_PLAY_NEXT_VIDEO = 'auto_play_next_video'
18
19 @Input() inputRecommendation: RecommendationInfo
20 @Input() user: User
21 @Output() gotRecommendations = new EventEmitter<Video[]>()
22
23 readonly hasVideos$: Observable<boolean>
24 readonly videos$: Observable<Video[]>
25
26 private autoPlayNextVideo: boolean
27
28 constructor (
29 private userService: UserService,
30 private authService: AuthService,
31 private notifier: Notifier,
32 private store: RecommendedVideosStore
33 ) {
34 this.videos$ = this.store.recommendations$
35 this.hasVideos$ = this.store.hasRecommendations$
36 this.videos$.subscribe(videos => this.gotRecommendations.emit(videos))
37
38 this.autoPlayNextVideo = this.authService.isLoggedIn()
39 ? this.authService.getUser().autoPlayNextVideo
40 : peertubeLocalStorage.getItem(RecommendedVideosComponent.LOCAL_STORAGE_AUTO_PLAY_NEXT_VIDEO) === 'true' || false
41 }
42
43 public ngOnChanges (): void {
44 if (this.inputRecommendation) {
45 this.store.requestNewRecommendations(this.inputRecommendation)
46 }
47 }
48
49 onVideoRemoved () {
50 this.store.requestNewRecommendations(this.inputRecommendation)
51 }
52
53 switchAutoPlayNextVideo () {
54 peertubeLocalStorage.setItem(RecommendedVideosComponent.LOCAL_STORAGE_AUTO_PLAY_NEXT_VIDEO, this.autoPlayNextVideo.toString())
55
56 if (this.authService.isLoggedIn()) {
57 const details = {
58 autoPlayNextVideo: this.autoPlayNextVideo
59 }
60
61 this.userService.updateMyProfile(details).subscribe(
62 () => {
63 this.authService.refreshUserInformation()
64 },
65 err => this.notifier.error(err.message)
66 )
67 }
68 }
69 }