]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+videos/video-list/trending/video-trending.component.ts
Merge remote-tracking branch 'weblate/develop' into develop
[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
C
37 private videoService: VideoService,
38 private hooks: HooksService
989e526a 39 ) {
9bf9d2a5 40 super()
5bcbcbe3 41
ba5d4a84
RK
42 this.defaultSort = this.parseAlgorithm(RedirectService.DEFAULT_TRENDING_ALGORITHM)
43
5bcbcbe3 44 this.headerComponentInjector = this.getInjector()
9bf9d2a5
C
45 }
46
47 ngOnInit () {
48 super.ngOnInit()
cc1561f9 49
244e76a5 50 this.generateSyndicationList()
7b95f313 51
15bedeeb
C
52 // Subscribe to alg change after we loaded the data
53 // The initial alg load is handled by the parent class
54 this.algorithmChangeSub = this.onDataSubject
55 .pipe(
56 first(),
57 switchMap(() => this.route.queryParams)
58 ).subscribe(queryParams => {
59 const oldSort = this.sort
60
61 this.loadPageRouteParams(queryParams)
f19a9e4b 62
15bedeeb 63 if (oldSort !== this.sort) this.reloadVideos()
ba5d4a84
RK
64 }
65 )
9bf9d2a5
C
66 }
67
9af61e84
C
68 ngOnDestroy () {
69 super.ngOnDestroy()
ba5d4a84 70 if (this.algorithmChangeSub) this.algorithmChangeSub.unsubscribe()
9af61e84
C
71 }
72
0cd4344f
C
73 getVideosObservable (page: number) {
74 const newPagination = immutableAssign(this.pagination, { currentPage: page })
93cae479 75 const params = {
3caf77d3
C
76 videoPagination: newPagination,
77 sort: this.sort,
3caf77d3 78 categoryOneOf: this.categoryOneOf,
440d39c5 79 languageOneOf: this.languageOneOf,
5c20a455 80 nsfwPolicy: this.nsfwPolicy,
440d39c5 81 skipCount: true
93cae479
C
82 }
83
84 return this.hooks.wrapObsFun(
85 this.videoService.getVideos.bind(this.videoService),
86 params,
87 'common',
7663e55a
C
88 'filter:api.trending-videos.videos.list.params',
89 'filter:api.trending-videos.videos.list.result'
93cae479 90 )
9bf9d2a5 91 }
244e76a5
RK
92
93 generateSyndicationList () {
d59cba29 94 this.syndicationItems = this.videoService.getVideoFeedUrls(this.sort, undefined, this.categoryOneOf)
244e76a5 95 }
5bcbcbe3
RK
96
97 getInjector () {
98 return Injector.create({
99 providers: [{
100 provide: 'data',
101 useValue: {
102 model: this.defaultSort
103 }
104 }]
105 })
106 }
ba5d4a84 107
15bedeeb
C
108 protected loadPageRouteParams (queryParams: Params) {
109 const algorithm = queryParams['alg'] || RedirectService.DEFAULT_TRENDING_ALGORITHM
110
111 this.sort = this.parseAlgorithm(algorithm)
112 }
113
ba5d4a84
RK
114 private parseAlgorithm (algorithm: string): VideoSortField {
115 switch (algorithm) {
116 case 'most-viewed':
117 return '-trending'
118 case 'most-liked':
119 return '-likes'
120 default:
121 return '-' + algorithm as VideoSortField
122 }
123 }
9bf9d2a5 124}