]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+videos/+video-watch/shared/recommendations/recent-videos-recommendation.service.ts
Fix privacy concern for remote videos
[github/Chocobozzz/PeerTube.git] / client / src / app / +videos / +video-watch / shared / recommendations / recent-videos-recommendation.service.ts
CommitLineData
01fe5bd7 1import { Observable, of } from 'rxjs'
5c20a455
C
2import { map, switchMap } from 'rxjs/operators'
3import { Injectable } from '@angular/core'
67ed6552 4import { ServerService, UserService } from '@app/core'
67ed6552 5import { Video, VideoService } from '@app/shared/shared-main'
1942f11d 6import { AdvancedSearch, SearchService } from '@app/shared/shared-search'
2989628b 7import { HTMLServerConfig } from '@shared/models'
67ed6552
C
8import { RecommendationInfo } from './recommendation-info.model'
9import { RecommendationService } from './recommendations.service'
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
2989628b 18 private config: HTMLServerConfig
ff9c3d9b 19
7f5f4152 20 constructor (
b0c36821 21 private videos: VideoService,
ff9c3d9b 22 private searchService: SearchService,
5c20a455 23 private userService: UserService,
ff9c3d9b
C
24 private serverService: ServerService
25 ) {
2989628b 26 this.config = this.serverService.getHTMLConfig()
5c20a455 27 }
7f5f4152 28
b0c36821 29 getRecommendations (recommendation: RecommendationInfo): Observable<Video[]> {
2989628b 30
b0c36821 31 return this.fetchPage(1, recommendation)
7f5f4152 32 .pipe(
e972e046
C
33 map(videos => {
34 const otherVideos = videos.filter(v => v.uuid !== recommendation.uuid)
7f5f4152
BJ
35 return otherVideos.slice(0, this.pageSize)
36 })
37 )
38 }
39
b0c36821 40 private fetchPage (page: number, recommendation: RecommendationInfo): Observable<Video[]> {
01fe5bd7 41 const pagination = { currentPage: page, itemsPerPage: this.pageSize + 1 }
3caf77d3 42 const defaultSubscription = this.videos.getVideos({ videoPagination: pagination, sort: '-createdAt' })
93cae479 43 .pipe(map(v => v.data))
01fe5bd7 44
ff9c3d9b
C
45 const tags = recommendation.tags
46 const searchIndexConfig = this.config.search.searchIndex
47 if (
48 !tags || tags.length === 0 ||
49 (searchIndexConfig.enabled === true && searchIndexConfig.disableLocalSearch === true)
50 ) {
51 return defaultSubscription
52 }
01fe5bd7 53
5c20a455
C
54 return this.userService.getAnonymousOrLoggedUser()
55 .pipe(
56 map(user => {
57 return {
58 search: '',
59 componentPagination: pagination,
60 advancedSearch: new AdvancedSearch({
61 tagsOneOf: recommendation.tags.join(','),
456c9bfd 62 sort: '-publishedAt',
5c20a455
C
63 searchTarget: 'local',
64 nsfw: user.nsfwPolicy
65 ? this.videos.nsfwPolicyToParam(user.nsfwPolicy)
66 : undefined
67 })
68 }
69 }),
70 switchMap(params => this.searchService.searchVideos(params)),
71 map(v => v.data),
72 switchMap(videos => {
73 if (videos.length <= 1) return defaultSubscription
93cae479 74
5c20a455
C
75 return of(videos)
76 })
77 )
7f5f4152 78 }
7f5f4152 79}