]>
Commit | Line | Data |
---|---|---|
01fe5bd7 | 1 | import { Observable, of } from 'rxjs' |
5c20a455 C |
2 | import { map, switchMap } from 'rxjs/operators' |
3 | import { Injectable } from '@angular/core' | |
67ed6552 | 4 | import { ServerService, UserService } from '@app/core' |
67ed6552 | 5 | import { Video, VideoService } from '@app/shared/shared-main' |
1942f11d | 6 | import { AdvancedSearch, SearchService } from '@app/shared/shared-search' |
ff9c3d9b | 7 | import { ServerConfig } from '@shared/models' |
67ed6552 C |
8 | import { RecommendationInfo } from './recommendation-info.model' |
9 | import { RecommendationService } from './recommendations.service' | |
7f5f4152 BJ |
10 | |
11 | /** | |
12 | * Provides "recommendations" by providing the most recently uploaded videos. | |
13 | */ | |
14 | @Injectable() | |
15 | export class RecentVideosRecommendationService implements RecommendationService { | |
7f5f4152 BJ |
16 | readonly pageSize = 5 |
17 | ||
ff9c3d9b C |
18 | private config: ServerConfig |
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 | ) { | |
26 | this.config = this.serverService.getTmpConfig() | |
27 | ||
28 | this.serverService.getConfig() | |
29 | .subscribe(config => this.config = config) | |
5c20a455 | 30 | } |
7f5f4152 | 31 | |
b0c36821 J |
32 | getRecommendations (recommendation: RecommendationInfo): Observable<Video[]> { |
33 | return this.fetchPage(1, recommendation) | |
7f5f4152 | 34 | .pipe( |
e972e046 C |
35 | map(videos => { |
36 | const otherVideos = videos.filter(v => v.uuid !== recommendation.uuid) | |
7f5f4152 BJ |
37 | return otherVideos.slice(0, this.pageSize) |
38 | }) | |
39 | ) | |
40 | } | |
41 | ||
b0c36821 | 42 | private fetchPage (page: number, recommendation: RecommendationInfo): Observable<Video[]> { |
01fe5bd7 | 43 | const pagination = { currentPage: page, itemsPerPage: this.pageSize + 1 } |
3caf77d3 | 44 | const defaultSubscription = this.videos.getVideos({ videoPagination: pagination, sort: '-createdAt' }) |
93cae479 | 45 | .pipe(map(v => v.data)) |
01fe5bd7 | 46 | |
ff9c3d9b C |
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 | } | |
01fe5bd7 | 55 | |
5c20a455 C |
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(','), | |
456c9bfd | 64 | sort: '-publishedAt', |
5c20a455 C |
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 | |
93cae479 | 76 | |
5c20a455 C |
77 | return of(videos) |
78 | }) | |
79 | ) | |
7f5f4152 | 80 | } |
7f5f4152 | 81 | } |