]>
Commit | Line | Data |
---|---|---|
1 | import { Observable } from 'rxjs' | |
2 | import { catchError, map, switchMap } from 'rxjs/operators' | |
3 | import { HttpClient, HttpParams } from '@angular/common/http' | |
4 | import { Injectable } from '@angular/core' | |
5 | import { ComponentPaginationLight, RestExtractor, RestPagination, RestService } from '@app/core' | |
6 | import { Video, VideoChannel, VideoChannelService, VideoService } from '@app/shared/shared-main' | |
7 | import { peertubeLocalStorage } from '@root-helpers/peertube-web-storage' | |
8 | import { | |
9 | ResultList, | |
10 | SearchTargetType, | |
11 | Video as VideoServerModel, | |
12 | VideoChannel as VideoChannelServerModel, | |
13 | VideoPlaylist as VideoPlaylistServerModel | |
14 | } from '@shared/models' | |
15 | import { environment } from '../../../environments/environment' | |
16 | import { VideoPlaylist, VideoPlaylistService } from '../shared-video-playlist' | |
17 | import { AdvancedSearch } from './advanced-search.model' | |
18 | ||
19 | @Injectable() | |
20 | export class SearchService { | |
21 | static BASE_SEARCH_URL = environment.apiUrl + '/api/v1/search/' | |
22 | ||
23 | constructor ( | |
24 | private authHttp: HttpClient, | |
25 | private restExtractor: RestExtractor, | |
26 | private restService: RestService, | |
27 | private videoService: VideoService, | |
28 | private playlistService: VideoPlaylistService | |
29 | ) { | |
30 | // Add ability to override search endpoint if the user updated this local storage key | |
31 | const searchUrl = peertubeLocalStorage.getItem('search-url') | |
32 | if (searchUrl) SearchService.BASE_SEARCH_URL = searchUrl | |
33 | } | |
34 | ||
35 | searchVideos (parameters: { | |
36 | search: string, | |
37 | componentPagination?: ComponentPaginationLight, | |
38 | advancedSearch?: AdvancedSearch | |
39 | }): Observable<ResultList<Video>> { | |
40 | const { search, componentPagination, advancedSearch } = parameters | |
41 | ||
42 | const url = SearchService.BASE_SEARCH_URL + 'videos' | |
43 | let pagination: RestPagination | |
44 | ||
45 | if (componentPagination) { | |
46 | pagination = this.restService.componentPaginationToRestPagination(componentPagination) | |
47 | } | |
48 | ||
49 | let params = new HttpParams() | |
50 | params = this.restService.addRestGetParams(params, pagination) | |
51 | ||
52 | if (search) params = params.append('search', search) | |
53 | ||
54 | if (advancedSearch) { | |
55 | const advancedSearchObject = advancedSearch.toAPIObject() | |
56 | params = this.restService.addObjectParams(params, advancedSearchObject) | |
57 | } | |
58 | ||
59 | return this.authHttp | |
60 | .get<ResultList<VideoServerModel>>(url, { params }) | |
61 | .pipe( | |
62 | switchMap(res => this.videoService.extractVideos(res)), | |
63 | catchError(err => this.restExtractor.handleError(err)) | |
64 | ) | |
65 | } | |
66 | ||
67 | searchVideoChannels (parameters: { | |
68 | search: string, | |
69 | searchTarget?: SearchTargetType, | |
70 | componentPagination?: ComponentPaginationLight | |
71 | }): Observable<ResultList<VideoChannel>> { | |
72 | const { search, componentPagination, searchTarget } = parameters | |
73 | ||
74 | const url = SearchService.BASE_SEARCH_URL + 'video-channels' | |
75 | ||
76 | let pagination: RestPagination | |
77 | if (componentPagination) { | |
78 | pagination = this.restService.componentPaginationToRestPagination(componentPagination) | |
79 | } | |
80 | ||
81 | let params = new HttpParams() | |
82 | params = this.restService.addRestGetParams(params, pagination) | |
83 | params = params.append('search', search) | |
84 | ||
85 | if (searchTarget) { | |
86 | params = params.append('searchTarget', searchTarget as string) | |
87 | } | |
88 | ||
89 | return this.authHttp | |
90 | .get<ResultList<VideoChannelServerModel>>(url, { params }) | |
91 | .pipe( | |
92 | map(res => VideoChannelService.extractVideoChannels(res)), | |
93 | catchError(err => this.restExtractor.handleError(err)) | |
94 | ) | |
95 | } | |
96 | ||
97 | searchVideoPlaylists (parameters: { | |
98 | search: string, | |
99 | searchTarget?: SearchTargetType, | |
100 | componentPagination?: ComponentPaginationLight | |
101 | }): Observable<ResultList<VideoPlaylist>> { | |
102 | const { search, componentPagination, searchTarget } = parameters | |
103 | ||
104 | const url = SearchService.BASE_SEARCH_URL + 'video-playlists' | |
105 | ||
106 | let pagination: RestPagination | |
107 | if (componentPagination) { | |
108 | pagination = this.restService.componentPaginationToRestPagination(componentPagination) | |
109 | } | |
110 | ||
111 | let params = new HttpParams() | |
112 | params = this.restService.addRestGetParams(params, pagination) | |
113 | params = params.append('search', search) | |
114 | ||
115 | if (searchTarget) { | |
116 | params = params.append('searchTarget', searchTarget as string) | |
117 | } | |
118 | ||
119 | return this.authHttp | |
120 | .get<ResultList<VideoPlaylistServerModel>>(url, { params }) | |
121 | .pipe( | |
122 | switchMap(res => this.playlistService.extractPlaylists(res)), | |
123 | catchError(err => this.restExtractor.handleError(err)) | |
124 | ) | |
125 | } | |
126 | } |