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