recommended-videos.component.ts 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 { VideoPlaylist } from '@app/shared/video-playlist/video-playlist.model'
  5. import { RecommendationInfo } from '@app/shared/video/recommendation-info.model'
  6. import { RecommendedVideosStore } from '@app/videos/recommendations/recommended-videos.store'
  7. import { User } from '@app/shared'
  8. import { AuthService, Notifier } from '@app/core'
  9. import { UserService } from '@app/shared/users/user.service'
  10. import { peertubeLocalStorage } from '@app/shared/misc/peertube-local-storage'
  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. static LOCAL_STORAGE_AUTO_PLAY_NEXT_VIDEO = 'auto_play_next_video'
  18. @Input() inputRecommendation: RecommendationInfo
  19. @Input() user: User
  20. @Input() playlist: VideoPlaylist
  21. @Output() gotRecommendations = new EventEmitter<Video[]>()
  22. readonly hasVideos$: Observable<boolean>
  23. readonly videos$: Observable<Video[]>
  24. private autoPlayNextVideo: boolean
  25. constructor (
  26. private userService: UserService,
  27. private authService: AuthService,
  28. private notifier: Notifier,
  29. private store: RecommendedVideosStore
  30. ) {
  31. this.videos$ = this.store.recommendations$
  32. this.hasVideos$ = this.store.hasRecommendations$
  33. this.videos$.subscribe(videos => this.gotRecommendations.emit(videos))
  34. this.autoPlayNextVideo = this.authService.isLoggedIn()
  35. ? this.authService.getUser().autoPlayNextVideo
  36. : peertubeLocalStorage.getItem(RecommendedVideosComponent.LOCAL_STORAGE_AUTO_PLAY_NEXT_VIDEO) === 'true' || false
  37. }
  38. public ngOnChanges (): void {
  39. if (this.inputRecommendation) {
  40. this.store.requestNewRecommendations(this.inputRecommendation)
  41. }
  42. }
  43. onVideoRemoved () {
  44. this.store.requestNewRecommendations(this.inputRecommendation)
  45. }
  46. switchAutoPlayNextVideo () {
  47. peertubeLocalStorage.setItem(RecommendedVideosComponent.LOCAL_STORAGE_AUTO_PLAY_NEXT_VIDEO, this.autoPlayNextVideo.toString())
  48. if (this.authService.isLoggedIn()) {
  49. const details = {
  50. autoPlayNextVideo: this.autoPlayNextVideo
  51. }
  52. this.userService.updateMyProfile(details).subscribe(
  53. () => {
  54. this.authService.refreshUserInformation()
  55. },
  56. err => this.notifier.error(err.message)
  57. )
  58. }
  59. }
  60. }