]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/videos/recommendations/recent-videos-recommendation.service.ts
Refactor: Separated "Other Videos" section into a dedicated component/service (#969)
[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 { VideoService, VideosProvider } from '@app/shared/video/video.service'
5 import { map } from 'rxjs/operators'
6 import { Observable } from 'rxjs'
7
8 /**
9 * Provides "recommendations" by providing the most recently uploaded videos.
10 */
11 @Injectable()
12 export class RecentVideosRecommendationService implements RecommendationService {
13
14 readonly pageSize = 5
15
16 constructor (
17 @Inject(VideoService) private videos: VideosProvider
18 ) {
19 }
20
21 getRecommendations (uuid: string): Observable<Video[]> {
22 return this.fetchPage(1)
23 .pipe(
24 map(vids => {
25 const otherVideos = vids.filter(v => v.uuid !== uuid)
26 return otherVideos.slice(0, this.pageSize)
27 })
28 )
29 }
30
31 private fetchPage (page: number): Observable<Video[]> {
32 let pagination = { currentPage: page, itemsPerPage: this.pageSize + 1 }
33 return this.videos.getVideos(pagination, '-createdAt')
34 .pipe(
35 map(v => v.videos)
36 )
37 }
38
39 }