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'
12 * Provides "recommendations" by providing the most recently uploaded videos.
15 export class RecentVideosRecommendationService implements RecommendationService {
18 private config: ServerConfig
21 private videos: VideoService,
22 private searchService: SearchService,
23 private userService: UserService,
24 private serverService: ServerService
26 this.config = this.serverService.getTmpConfig()
28 this.serverService.getConfig()
29 .subscribe(config => this.config = config)
32 getRecommendations (recommendation: RecommendationInfo): Observable<Video[]> {
33 return this.fetchPage(1, recommendation)
36 const otherVideos = videos.filter(v => v.uuid !== recommendation.uuid)
37 return otherVideos.slice(0, this.pageSize)
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))
47 const tags = recommendation.tags
48 const searchIndexConfig = this.config.search.searchIndex
50 !tags || tags.length === 0 ||
51 (searchIndexConfig.enabled === true && searchIndexConfig.disableLocalSearch === true)
53 return defaultSubscription
56 return this.userService.getAnonymousOrLoggedUser()
61 componentPagination: pagination,
62 advancedSearch: new AdvancedSearch({
63 tagsOneOf: recommendation.tags.join(','),
65 searchTarget: 'local',
67 ? this.videos.nsfwPolicyToParam(user.nsfwPolicy)
72 switchMap(params => this.searchService.searchVideos(params)),
75 if (videos.length <= 1) return defaultSubscription