]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/videos/recommendations/recent-videos-recommendation.service.ts
remove unused imports
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / recommendations / recent-videos-recommendation.service.ts
CommitLineData
23db998f 1import { Injectable } from '@angular/core'
7f5f4152
BJ
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
23db998f 22 ) { }
7f5f4152 23
b0c36821
J
24 getRecommendations (recommendation: RecommendationInfo): Observable<Video[]> {
25 return this.fetchPage(1, recommendation)
7f5f4152 26 .pipe(
e972e046
C
27 map(videos => {
28 const otherVideos = videos.filter(v => v.uuid !== recommendation.uuid)
7f5f4152
BJ
29 return otherVideos.slice(0, this.pageSize)
30 })
31 )
32 }
33
b0c36821 34 private fetchPage (page: number, recommendation: RecommendationInfo): Observable<Video[]> {
7f5f4152 35 let pagination = { currentPage: page, itemsPerPage: this.pageSize + 1 }
b0c36821
J
36 if (!recommendation.tags) {
37 return this.videos.getVideos(pagination, '-createdAt')
38 .pipe(
39 map(v => v.videos)
40 )
41 }
42 if (recommendation.tags.length === 0) {
43 return this.videos.getVideos(pagination, '-createdAt')
44 .pipe(
45 map(v => v.videos)
46 )
47 }
48 return this.searchService.searchVideos('',
49 pagination,
50 new AdvancedSearch({ tagsOneOf: recommendation.tags.join(','), sort: '-createdAt' })
51 ).pipe(
52 map(v => v.videos)
53 )
7f5f4152 54 }
7f5f4152 55}