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