]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/videos/recommendations/recent-videos-recommendation.service.ts
Add client hooks
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / recommendations / recent-videos-recommendation.service.ts
1 import { Injectable } from '@angular/core'
2 import { RecommendationService } from '@app/videos/recommendations/recommendations.service'
3 import { Video } from '@app/shared/video/video.model'
4 import { RecommendationInfo } from '@app/shared/video/recommendation-info.model'
5 import { VideoService } from '@app/shared/video/video.service'
6 import { map, switchMap } from 'rxjs/operators'
7 import { Observable, of } from 'rxjs'
8 import { SearchService } from '@app/search/search.service'
9 import { AdvancedSearch } from '@app/search/advanced-search.model'
10
11 /**
12 * Provides "recommendations" by providing the most recently uploaded videos.
13 */
14 @Injectable()
15 export class RecentVideosRecommendationService implements RecommendationService {
16 readonly pageSize = 5
17
18 constructor (
19 private videos: VideoService,
20 private searchService: SearchService
21 ) { }
22
23 getRecommendations (recommendation: RecommendationInfo): Observable<Video[]> {
24 return this.fetchPage(1, recommendation)
25 .pipe(
26 map(videos => {
27 const otherVideos = videos.filter(v => v.uuid !== recommendation.uuid)
28 return otherVideos.slice(0, this.pageSize)
29 })
30 )
31 }
32
33 private fetchPage (page: number, recommendation: RecommendationInfo): Observable<Video[]> {
34 const pagination = { currentPage: page, itemsPerPage: this.pageSize + 1 }
35 const defaultSubscription = this.videos.getVideos({ videoPagination: pagination, sort: '-createdAt' })
36 .pipe(map(v => v.data))
37
38 if (!recommendation.tags || recommendation.tags.length === 0) return defaultSubscription
39
40 const params = {
41 search: '',
42 componentPagination: pagination,
43 advancedSearch: new AdvancedSearch({ tagsOneOf: recommendation.tags.join(','), sort: '-createdAt' })
44 }
45
46 return this.searchService.searchVideos(params)
47 .pipe(
48 map(v => v.data),
49 switchMap(videos => {
50 if (videos.length <= 1) return defaultSubscription
51
52 return of(videos)
53 })
54 )
55 }
56 }