]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/videos/recommendations/recent-videos-recommendation.service.ts
0ee34b9cb28a5d254f6c37e68be582bcee2c557c
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / recommendations / recent-videos-recommendation.service.ts
1 import { Inject, 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 } from 'rxjs/operators'
7 import { Observable } 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
17 readonly pageSize = 5
18
19 constructor (
20 private videos: VideoService,
21 private searchService: SearchService
22 ) {
23 }
24
25 getRecommendations (recommendation: RecommendationInfo): Observable<Video[]> {
26 return this.fetchPage(1, recommendation)
27 .pipe(
28 map(videos => {
29 const otherVideos = videos.filter(v => v.uuid !== recommendation.uuid)
30 return otherVideos.slice(0, this.pageSize)
31 })
32 )
33 }
34
35 private fetchPage (page: number, recommendation: RecommendationInfo): Observable<Video[]> {
36 let pagination = { currentPage: page, itemsPerPage: this.pageSize + 1 }
37 if (!recommendation.tags) {
38 return this.videos.getVideos(pagination, '-createdAt')
39 .pipe(
40 map(v => v.videos)
41 )
42 }
43 if (recommendation.tags.length === 0) {
44 return this.videos.getVideos(pagination, '-createdAt')
45 .pipe(
46 map(v => v.videos)
47 )
48 }
49 return this.searchService.searchVideos('',
50 pagination,
51 new AdvancedSearch({ tagsOneOf: recommendation.tags.join(','), sort: '-createdAt' })
52 ).pipe(
53 map(v => v.videos)
54 )
55 }
56 }