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