diff options
Diffstat (limited to 'client/src/app/videos/recommendations/recent-videos-recommendation.service.ts')
-rw-r--r-- | client/src/app/videos/recommendations/recent-videos-recommendation.service.ts | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/client/src/app/videos/recommendations/recent-videos-recommendation.service.ts b/client/src/app/videos/recommendations/recent-videos-recommendation.service.ts new file mode 100644 index 000000000..708d67699 --- /dev/null +++ b/client/src/app/videos/recommendations/recent-videos-recommendation.service.ts | |||
@@ -0,0 +1,39 @@ | |||
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 | } | ||