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