]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/videos/recommendations/recent-videos-recommendation.service.ts
Add video recomandation by tags (#1001)
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / recommendations / recent-videos-recommendation.service.ts
CommitLineData
7f5f4152
BJ
1import { Inject, Injectable } from '@angular/core'
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'
7f5f4152
BJ
6import { map } from 'rxjs/operators'
7import { Observable } from 'rxjs'
b0c36821
J
8import { SearchService } from '@app/search/search.service'
9import { AdvancedSearch } from '@app/search/advanced-search.model'
7f5f4152
BJ
10
11/**
12 * Provides "recommendations" by providing the most recently uploaded videos.
13 */
14@Injectable()
15export class RecentVideosRecommendationService implements RecommendationService {
16
17 readonly pageSize = 5
18
19 constructor (
b0c36821
J
20 private videos: VideoService,
21 private searchService: SearchService
7f5f4152
BJ
22 ) {
23 }
24
b0c36821
J
25 getRecommendations (recommendation: RecommendationInfo): Observable<Video[]> {
26 return this.fetchPage(1, recommendation)
7f5f4152
BJ
27 .pipe(
28 map(vids => {
b0c36821 29 const otherVideos = vids.filter(v => v.uuid !== recommendation.uuid)
7f5f4152
BJ
30 return otherVideos.slice(0, this.pageSize)
31 })
32 )
33 }
34
b0c36821 35 private fetchPage (page: number, recommendation: RecommendationInfo): Observable<Video[]> {
7f5f4152 36 let pagination = { currentPage: page, itemsPerPage: this.pageSize + 1 }
b0c36821
J
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 )
7f5f4152 55 }
7f5f4152 56}