]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/videos/recommendations/recent-videos-recommendation.service.ts
Fix scrolling with hash in url
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / recommendations / recent-videos-recommendation.service.ts
CommitLineData
ff9c3d9b 1import { Injectable, OnInit } from '@angular/core'
7f5f4152
BJ
2import { RecommendationService } from '@app/videos/recommendations/recommendations.service'
3import { Video } from '@app/shared/video/video.model'
b0c36821
J
4import { RecommendationInfo } from '@app/shared/video/recommendation-info.model'
5import { VideoService } from '@app/shared/video/video.service'
01fe5bd7
C
6import { map, switchMap } from 'rxjs/operators'
7import { Observable, of } from 'rxjs'
b0c36821
J
8import { SearchService } from '@app/search/search.service'
9import { AdvancedSearch } from '@app/search/advanced-search.model'
ff9c3d9b
C
10import { ServerService } from '@app/core'
11import { ServerConfig } from '@shared/models'
12import { truncate } from 'lodash'
7f5f4152
BJ
13
14/**
15 * Provides "recommendations" by providing the most recently uploaded videos.
16 */
17@Injectable()
18export class RecentVideosRecommendationService implements RecommendationService {
7f5f4152
BJ
19 readonly pageSize = 5
20
ff9c3d9b
C
21 private config: ServerConfig
22
7f5f4152 23 constructor (
b0c36821 24 private videos: VideoService,
ff9c3d9b
C
25 private searchService: SearchService,
26 private serverService: ServerService
27 ) {
28 this.config = this.serverService.getTmpConfig()
29
30 this.serverService.getConfig()
31 .subscribe(config => this.config = config)
32 }
7f5f4152 33
b0c36821
J
34 getRecommendations (recommendation: RecommendationInfo): Observable<Video[]> {
35 return this.fetchPage(1, recommendation)
7f5f4152 36 .pipe(
e972e046
C
37 map(videos => {
38 const otherVideos = videos.filter(v => v.uuid !== recommendation.uuid)
7f5f4152
BJ
39 return otherVideos.slice(0, this.pageSize)
40 })
41 )
42 }
43
b0c36821 44 private fetchPage (page: number, recommendation: RecommendationInfo): Observable<Video[]> {
01fe5bd7 45 const pagination = { currentPage: page, itemsPerPage: this.pageSize + 1 }
3caf77d3 46 const defaultSubscription = this.videos.getVideos({ videoPagination: pagination, sort: '-createdAt' })
93cae479 47 .pipe(map(v => v.data))
01fe5bd7 48
ff9c3d9b
C
49 const tags = recommendation.tags
50 const searchIndexConfig = this.config.search.searchIndex
51 if (
52 !tags || tags.length === 0 ||
53 (searchIndexConfig.enabled === true && searchIndexConfig.disableLocalSearch === true)
54 ) {
55 return defaultSubscription
56 }
01fe5bd7 57
93cae479
C
58 const params = {
59 search: '',
60 componentPagination: pagination,
ff9c3d9b 61 advancedSearch: new AdvancedSearch({ tagsOneOf: recommendation.tags.join(','), sort: '-createdAt', searchTarget: 'local' })
93cae479
C
62 }
63
64 return this.searchService.searchVideos(params)
65 .pipe(
66 map(v => v.data),
67 switchMap(videos => {
68 if (videos.length <= 1) return defaultSubscription
69
70 return of(videos)
71 })
72 )
7f5f4152 73 }
7f5f4152 74}