diff options
Diffstat (limited to 'client/src/app/+videos/+video-watch/shared/recommendations/recent-videos-recommendation.service.ts')
-rw-r--r-- | client/src/app/+videos/+video-watch/shared/recommendations/recent-videos-recommendation.service.ts | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/client/src/app/+videos/+video-watch/shared/recommendations/recent-videos-recommendation.service.ts b/client/src/app/+videos/+video-watch/shared/recommendations/recent-videos-recommendation.service.ts new file mode 100644 index 000000000..4654da847 --- /dev/null +++ b/client/src/app/+videos/+video-watch/shared/recommendations/recent-videos-recommendation.service.ts | |||
@@ -0,0 +1,79 @@ | |||
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 { HTMLServerConfig } 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: HTMLServerConfig | ||
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.getHTMLConfig() | ||
27 | } | ||
28 | |||
29 | getRecommendations (recommendation: RecommendationInfo): Observable<Video[]> { | ||
30 | |||
31 | return this.fetchPage(1, recommendation) | ||
32 | .pipe( | ||
33 | map(videos => { | ||
34 | const otherVideos = videos.filter(v => v.uuid !== recommendation.uuid) | ||
35 | return otherVideos.slice(0, this.pageSize) | ||
36 | }) | ||
37 | ) | ||
38 | } | ||
39 | |||
40 | private fetchPage (page: number, recommendation: RecommendationInfo): Observable<Video[]> { | ||
41 | const pagination = { currentPage: page, itemsPerPage: this.pageSize + 1 } | ||
42 | const defaultSubscription = this.videos.getVideos({ videoPagination: pagination, sort: '-createdAt' }) | ||
43 | .pipe(map(v => v.data)) | ||
44 | |||
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 | } | ||
53 | |||
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(','), | ||
62 | sort: '-publishedAt', | ||
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 | ||
74 | |||
75 | return of(videos) | ||
76 | }) | ||
77 | ) | ||
78 | } | ||
79 | } | ||