]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+videos/+video-watch/recommendations/recent-videos-recommendation.service.ts
Reorganize client shared modules
[github/Chocobozzz/PeerTube.git] / client / src / app / +videos / +video-watch / recommendations / recent-videos-recommendation.service.ts
1 import { Observable, of } from 'rxjs'
2 import { map, switchMap } from 'rxjs/operators'
3 import { Injectable } from '@angular/core'
4 import { ServerService, UserService } from '@app/core'
5 import { Video, VideoService } from '@app/shared/shared-main'
6 import { AdvancedSearch, SearchService } from '@app/shared/shared-search'
7 import { ServerConfig } from '@shared/models'
8 import { RecommendationInfo } from './recommendation-info.model'
9 import { RecommendationService } from './recommendations.service'
10
11 /**
12 * Provides "recommendations" by providing the most recently uploaded videos.
13 */
14 @Injectable()
15 export class RecentVideosRecommendationService implements RecommendationService {
16 readonly pageSize = 5
17
18 private config: ServerConfig
19
20 constructor (
21 private videos: VideoService,
22 private searchService: SearchService,
23 private userService: UserService,
24 private serverService: ServerService
25 ) {
26 this.config = this.serverService.getTmpConfig()
27
28 this.serverService.getConfig()
29 .subscribe(config => this.config = config)
30 }
31
32 getRecommendations (recommendation: RecommendationInfo): Observable<Video[]> {
33 return this.fetchPage(1, recommendation)
34 .pipe(
35 map(videos => {
36 const otherVideos = videos.filter(v => v.uuid !== recommendation.uuid)
37 return otherVideos.slice(0, this.pageSize)
38 })
39 )
40 }
41
42 private fetchPage (page: number, recommendation: RecommendationInfo): Observable<Video[]> {
43 const pagination = { currentPage: page, itemsPerPage: this.pageSize + 1 }
44 const defaultSubscription = this.videos.getVideos({ videoPagination: pagination, sort: '-createdAt' })
45 .pipe(map(v => v.data))
46
47 const tags = recommendation.tags
48 const searchIndexConfig = this.config.search.searchIndex
49 if (
50 !tags || tags.length === 0 ||
51 (searchIndexConfig.enabled === true && searchIndexConfig.disableLocalSearch === true)
52 ) {
53 return defaultSubscription
54 }
55
56 return this.userService.getAnonymousOrLoggedUser()
57 .pipe(
58 map(user => {
59 return {
60 search: '',
61 componentPagination: pagination,
62 advancedSearch: new AdvancedSearch({
63 tagsOneOf: recommendation.tags.join(','),
64 sort: '-createdAt',
65 searchTarget: 'local',
66 nsfw: user.nsfwPolicy
67 ? this.videos.nsfwPolicyToParam(user.nsfwPolicy)
68 : undefined
69 })
70 }
71 }),
72 switchMap(params => this.searchService.searchVideos(params)),
73 map(v => v.data),
74 switchMap(videos => {
75 if (videos.length <= 1) return defaultSubscription
76
77 return of(videos)
78 })
79 )
80 }
81 }