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