]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+videos/video-list/trending/video-trending.component.ts
move from trending routes to alg param
[github/Chocobozzz/PeerTube.git] / client / src / app / +videos / video-list / trending / video-trending.component.ts
CommitLineData
5bcbcbe3 1import { Component, ComponentFactoryResolver, Injector, OnDestroy, OnInit } from '@angular/core'
9bf9d2a5 2import { ActivatedRoute, Router } from '@angular/router'
ba5d4a84 3import { AuthService, LocalStorageService, Notifier, RedirectService, ScreenService, ServerService, UserService } from '@app/core'
93cae479 4import { HooksService } from '@app/core/plugins/hooks.service'
67ed6552
C
5import { immutableAssign } from '@app/helpers'
6import { VideoService } from '@app/shared/shared-main'
7import { AbstractVideoList } from '@app/shared/shared-video-miniature'
67ed6552 8import { VideoSortField } from '@shared/models'
ba5d4a84 9import { Subscription } from 'rxjs'
5bcbcbe3 10import { VideoTrendingHeaderComponent } from './video-trending-header.component'
9bf9d2a5
C
11
12@Component({
ba5d4a84 13 selector: 'my-videos-hot',
5bcbcbe3
RK
14 styleUrls: [ '../../../shared/shared-video-miniature/abstract-video-list.scss' ],
15 templateUrl: '../../../shared/shared-video-miniature/abstract-video-list.html'
9bf9d2a5 16})
ba5d4a84 17export class VideoTrendingComponent extends AbstractVideoList implements OnInit, OnDestroy {
5bcbcbe3 18 HeaderComponent = VideoTrendingHeaderComponent
989e526a 19 titlePage: string
9a629c6e 20 defaultSort: VideoSortField = '-trending'
9bf9d2a5 21
5c20a455 22 useUserVideoPreferences = true
3caf77d3 23
ba5d4a84
RK
24 private algorithmChangeSub: Subscription
25
989e526a
C
26 constructor (
27 protected router: Router,
489290b8 28 protected serverService: ServerService,
989e526a 29 protected route: ActivatedRoute,
f8b2c1b4 30 protected notifier: Notifier,
989e526a 31 protected authService: AuthService,
d3217560 32 protected userService: UserService,
bbe0f064 33 protected screenService: ScreenService,
d3217560 34 protected storageService: LocalStorageService,
5bcbcbe3 35 protected cfr: ComponentFactoryResolver,
93cae479
C
36 private videoService: VideoService,
37 private hooks: HooksService
989e526a 38 ) {
9bf9d2a5 39 super()
5bcbcbe3 40
ba5d4a84
RK
41 this.defaultSort = this.parseAlgorithm(RedirectService.DEFAULT_TRENDING_ALGORITHM)
42
5bcbcbe3 43 this.headerComponentInjector = this.getInjector()
9bf9d2a5
C
44 }
45
46 ngOnInit () {
47 super.ngOnInit()
cc1561f9 48
244e76a5 49 this.generateSyndicationList()
7b95f313 50
ba5d4a84
RK
51 this.algorithmChangeSub = this.route.queryParams.subscribe(
52 queryParams => {
53 const algorithm = queryParams['alg'] || RedirectService.DEFAULT_TRENDING_ALGORITHM
f19a9e4b 54
ba5d4a84
RK
55 this.sort = this.parseAlgorithm(algorithm)
56 this.reloadVideos()
57 }
58 )
9bf9d2a5
C
59 }
60
9af61e84
C
61 ngOnDestroy () {
62 super.ngOnDestroy()
ba5d4a84 63 if (this.algorithmChangeSub) this.algorithmChangeSub.unsubscribe()
9af61e84
C
64 }
65
0cd4344f
C
66 getVideosObservable (page: number) {
67 const newPagination = immutableAssign(this.pagination, { currentPage: page })
93cae479 68 const params = {
3caf77d3
C
69 videoPagination: newPagination,
70 sort: this.sort,
3caf77d3 71 categoryOneOf: this.categoryOneOf,
440d39c5 72 languageOneOf: this.languageOneOf,
5c20a455 73 nsfwPolicy: this.nsfwPolicy,
440d39c5 74 skipCount: true
93cae479
C
75 }
76
77 return this.hooks.wrapObsFun(
78 this.videoService.getVideos.bind(this.videoService),
79 params,
80 'common',
7663e55a
C
81 'filter:api.trending-videos.videos.list.params',
82 'filter:api.trending-videos.videos.list.result'
93cae479 83 )
9bf9d2a5 84 }
244e76a5
RK
85
86 generateSyndicationList () {
d59cba29 87 this.syndicationItems = this.videoService.getVideoFeedUrls(this.sort, undefined, this.categoryOneOf)
244e76a5 88 }
5bcbcbe3
RK
89
90 getInjector () {
91 return Injector.create({
92 providers: [{
93 provide: 'data',
94 useValue: {
95 model: this.defaultSort
96 }
97 }]
98 })
99 }
ba5d4a84
RK
100
101 private parseAlgorithm (algorithm: string): VideoSortField {
102 switch (algorithm) {
103 case 'most-viewed':
104 return '-trending'
105 case 'most-liked':
106 return '-likes'
107 default:
108 return '-' + algorithm as VideoSortField
109 }
110 }
9bf9d2a5 111}