Lazy load all routes
[oweals/peertube.git] / client / src / app / +videos / +video-watch / recommendations / recommended-videos.component.ts
1 import { Observable } from 'rxjs'
2 import { Component, EventEmitter, Input, OnChanges, OnInit, Output } from '@angular/core'
3 import { AuthService, Notifier, SessionStorageService, User, UserService } from '@app/core'
4 import { Video } from '@app/shared/shared-main'
5 import { MiniatureDisplayOptions } from '@app/shared/shared-video-miniature'
6 import { VideoPlaylist } from '@app/shared/shared-video-playlist'
7 import { I18n } from '@ngx-translate/i18n-polyfill'
8 import { RecommendationInfo } from './recommendation-info.model'
9 import { RecommendedVideosStore } from './recommended-videos.store'
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 OnInit, OnChanges {
17   @Input() inputRecommendation: RecommendationInfo
18   @Input() playlist: VideoPlaylist
19   @Output() gotRecommendations = new EventEmitter<Video[]>()
20
21   autoPlayNextVideo: boolean
22   autoPlayNextVideoTooltip: string
23
24   displayOptions: MiniatureDisplayOptions = {
25     date: true,
26     views: true,
27     by: true,
28     avatar: true
29   }
30
31   userMiniature: User
32
33   readonly hasVideos$: Observable<boolean>
34   readonly videos$: Observable<Video[]>
35
36   constructor (
37     private userService: UserService,
38     private authService: AuthService,
39     private notifier: Notifier,
40     private i18n: I18n,
41     private store: RecommendedVideosStore,
42     private sessionStorageService: SessionStorageService
43   ) {
44     this.videos$ = this.store.recommendations$
45     this.hasVideos$ = this.store.hasRecommendations$
46     this.videos$.subscribe(videos => this.gotRecommendations.emit(videos))
47
48     if (this.authService.isLoggedIn()) {
49       this.autoPlayNextVideo = this.authService.getUser().autoPlayNextVideo
50     } else {
51       this.autoPlayNextVideo = this.sessionStorageService.getItem(User.KEYS.SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO) === 'true' || false
52       this.sessionStorageService.watch([User.KEYS.SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO]).subscribe(
53         () => this.autoPlayNextVideo = this.sessionStorageService.getItem(User.KEYS.SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO) === 'true'
54       )
55     }
56
57     this.autoPlayNextVideoTooltip = this.i18n('When active, the next video is automatically played after the current one.')
58   }
59
60   ngOnInit () {
61     this.userService.getAnonymousOrLoggedUser()
62       .subscribe(user => this.userMiniature = user)
63   }
64
65   ngOnChanges () {
66     if (this.inputRecommendation) {
67       this.store.requestNewRecommendations(this.inputRecommendation)
68     }
69   }
70
71   onVideoRemoved () {
72     this.store.requestNewRecommendations(this.inputRecommendation)
73   }
74
75   switchAutoPlayNextVideo () {
76     this.sessionStorageService.setItem(User.KEYS.SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO, this.autoPlayNextVideo.toString())
77
78     if (this.authService.isLoggedIn()) {
79       const details = {
80         autoPlayNextVideo: this.autoPlayNextVideo
81       }
82
83       this.userService.updateMyProfile(details).subscribe(
84         () => {
85           this.authService.refreshUserInformation()
86         },
87         err => this.notifier.error(err.message)
88       )
89     }
90   }
91 }