]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/videos/recommendations/recent-videos-recommendation.service.ts
Add plugin table migration table
[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'
01fe5bd7
C
6import { map, switchMap } from 'rxjs/operators'
7import { Observable, of } 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 {
7f5f4152
BJ
16 readonly pageSize = 5
17
18 constructor (
b0c36821
J
19 private videos: VideoService,
20 private searchService: SearchService
23db998f 21 ) { }
7f5f4152 22
b0c36821
J
23 getRecommendations (recommendation: RecommendationInfo): Observable<Video[]> {
24 return this.fetchPage(1, recommendation)
7f5f4152 25 .pipe(
e972e046
C
26 map(videos => {
27 const otherVideos = videos.filter(v => v.uuid !== recommendation.uuid)
7f5f4152
BJ
28 return otherVideos.slice(0, this.pageSize)
29 })
30 )
31 }
32
b0c36821 33 private fetchPage (page: number, recommendation: RecommendationInfo): Observable<Video[]> {
01fe5bd7 34 const pagination = { currentPage: page, itemsPerPage: this.pageSize + 1 }
3caf77d3 35 const defaultSubscription = this.videos.getVideos({ videoPagination: pagination, sort: '-createdAt' })
01fe5bd7
C
36 .pipe(map(v => v.videos))
37
38 if (!recommendation.tags || recommendation.tags.length === 0) return defaultSubscription
39
b0c36821
J
40 return this.searchService.searchVideos('',
41 pagination,
42 new AdvancedSearch({ tagsOneOf: recommendation.tags.join(','), sort: '-createdAt' })
43 ).pipe(
01fe5bd7
C
44 map(v => v.videos),
45 switchMap(videos => {
46 if (videos.length <= 1) return defaultSubscription
47
48 return of(videos)
49 })
b0c36821 50 )
7f5f4152 51 }
7f5f4152 52}