1 import { Subscription } from 'rxjs'
2 import { first, switchMap } from 'rxjs/operators'
3 import { Component, ComponentFactoryResolver, Injector, OnDestroy, OnInit } from '@angular/core'
4 import { ActivatedRoute, Params, Router } from '@angular/router'
5 import { AuthService, LocalStorageService, Notifier, RedirectService, ScreenService, ServerService, UserService } from '@app/core'
6 import { HooksService } from '@app/core/plugins/hooks.service'
7 import { immutableAssign } from '@app/helpers'
8 import { VideoService } from '@app/shared/shared-main'
9 import { AbstractVideoList } from '@app/shared/shared-video-miniature'
10 import { VideoSortField } from '@shared/models'
11 import { VideoTrendingHeaderComponent } from './video-trending-header.component'
14 selector: 'my-videos-hot',
15 styleUrls: [ '../../../shared/shared-video-miniature/abstract-video-list.scss' ],
16 templateUrl: '../../../shared/shared-video-miniature/abstract-video-list.html'
18 export class VideoTrendingComponent extends AbstractVideoList implements OnInit, OnDestroy {
19 HeaderComponent = VideoTrendingHeaderComponent
21 defaultSort: VideoSortField = '-trending'
23 loadUserVideoPreferences = true
25 private algorithmChangeSub: Subscription
28 protected router: Router,
29 protected serverService: ServerService,
30 protected route: ActivatedRoute,
31 protected notifier: Notifier,
32 protected authService: AuthService,
33 protected userService: UserService,
34 protected screenService: ScreenService,
35 protected storageService: LocalStorageService,
36 protected cfr: ComponentFactoryResolver,
37 private videoService: VideoService,
38 private redirectService: RedirectService,
39 private hooks: HooksService
43 this.defaultSort = this.parseAlgorithm(this.redirectService.getDefaultTrendingAlgorithm())
45 this.headerComponentInjector = this.getInjector()
51 this.generateSyndicationList()
53 // Subscribe to alg change after we loaded the data
54 // The initial alg load is handled by the parent class
55 this.algorithmChangeSub = this.onDataSubject
58 switchMap(() => this.route.queryParams)
59 ).subscribe(queryParams => {
60 const oldSort = this.sort
62 this.loadPageRouteParams(queryParams)
64 if (oldSort !== this.sort) this.reloadVideos()
71 if (this.algorithmChangeSub) this.algorithmChangeSub.unsubscribe()
74 getVideosObservable (page: number) {
75 const newPagination = immutableAssign(this.pagination, { currentPage: page })
77 videoPagination: newPagination,
79 categoryOneOf: this.categoryOneOf,
80 languageOneOf: this.languageOneOf,
81 nsfwPolicy: this.nsfwPolicy,
85 return this.hooks.wrapObsFun(
86 this.videoService.getVideos.bind(this.videoService),
89 'filter:api.trending-videos.videos.list.params',
90 'filter:api.trending-videos.videos.list.result'
94 generateSyndicationList () {
95 this.syndicationItems = this.videoService.getVideoFeedUrls(this.sort, undefined, this.categoryOneOf)
99 return Injector.create({
103 model: this.defaultSort
109 protected loadPageRouteParams (queryParams: Params) {
110 const algorithm = queryParams['alg'] || this.redirectService.getDefaultTrendingAlgorithm()
112 this.sort = this.parseAlgorithm(algorithm)
115 private parseAlgorithm (algorithm: string): VideoSortField {
124 return '-' + algorithm as VideoSortField