]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - 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
1 import { Observable, of } from 'rxjs'
2 import { map, switchMap } from 'rxjs/operators'
3 import { Injectable } from '@angular/core'
4 import { ServerService, UserService } from '@app/core'
5 import { AdvancedSearch } from '@app/search/advanced-search.model'
6 import { SearchService } from '@app/search/search.service'
7 import { Video, VideoService } from '@app/shared/shared-main'
8 import { ServerConfig } from '@shared/models'
9 import { RecommendationInfo } from './recommendation-info.model'
10 import { RecommendationService } from './recommendations.service'
11
12 /**
13 * Provides "recommendations" by providing the most recently uploaded videos.
14 */
15 @Injectable()
16 export class RecentVideosRecommendationService implements RecommendationService {
17 readonly pageSize = 5
18
19 private config: ServerConfig
20
21 constructor (
22 private videos: VideoService,
23 private searchService: SearchService,
24 private userService: UserService,
25 private serverService: ServerService
26 ) {
27 this.config = this.serverService.getTmpConfig()
28
29 this.serverService.getConfig()
30 .subscribe(config => this.config = config)
31 }
32
33 getRecommendations (recommendation: RecommendationInfo): Observable<Video[]> {
34 return this.fetchPage(1, recommendation)
35 .pipe(
36 map(videos => {
37 const otherVideos = videos.filter(v => v.uuid !== recommendation.uuid)
38 return otherVideos.slice(0, this.pageSize)
39 })
40 )
41 }
42
43 private fetchPage (page: number, recommendation: RecommendationInfo): Observable<Video[]> {
44 const pagination = { currentPage: page, itemsPerPage: this.pageSize + 1 }
45 const defaultSubscription = this.videos.getVideos({ videoPagination: pagination, sort: '-createdAt' })
46 .pipe(map(v => v.data))
47
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 }
56
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
77
78 return of(videos)
79 })
80 )
81 }
82 }