]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/search/search.service.ts
Add hook to alter player build options
[github/Chocobozzz/PeerTube.git] / client / src / app / search / search.service.ts
1 import { catchError, map, switchMap } from 'rxjs/operators'
2 import { HttpClient, HttpParams } from '@angular/common/http'
3 import { Injectable } from '@angular/core'
4 import { Observable } from 'rxjs'
5 import { ComponentPagination } from '@app/shared/rest/component-pagination.model'
6 import { VideoService } from '@app/shared/video/video.service'
7 import { RestExtractor, RestService } from '@app/shared'
8 import { environment } from '../../environments/environment'
9 import { ResultList, Video as VideoServerModel, VideoChannel as VideoChannelServerModel } from '../../../../shared'
10 import { Video } from '@app/shared/video/video.model'
11 import { AdvancedSearch } from '@app/search/advanced-search.model'
12 import { VideoChannel } from '@app/shared/video-channel/video-channel.model'
13 import { VideoChannelService } from '@app/shared/video-channel/video-channel.service'
14
15 @Injectable()
16 export class SearchService {
17 static BASE_SEARCH_URL = environment.apiUrl + '/api/v1/search/'
18
19 constructor (
20 private authHttp: HttpClient,
21 private restExtractor: RestExtractor,
22 private restService: RestService,
23 private videoService: VideoService
24 ) {}
25
26 searchVideos (parameters: {
27 search: string,
28 componentPagination: ComponentPagination,
29 advancedSearch: AdvancedSearch
30 }): Observable<ResultList<Video>> {
31 const { search, componentPagination, advancedSearch } = parameters
32
33 const url = SearchService.BASE_SEARCH_URL + 'videos'
34 const pagination = this.restService.componentPaginationToRestPagination(componentPagination)
35
36 let params = new HttpParams()
37 params = this.restService.addRestGetParams(params, pagination)
38
39 if (search) params = params.append('search', search)
40
41 const advancedSearchObject = advancedSearch.toAPIObject()
42 params = this.restService.addObjectParams(params, advancedSearchObject)
43
44 return this.authHttp
45 .get<ResultList<VideoServerModel>>(url, { params })
46 .pipe(
47 switchMap(res => this.videoService.extractVideos(res)),
48 catchError(err => this.restExtractor.handleError(err))
49 )
50 }
51
52 searchVideoChannels (parameters: {
53 search: string,
54 componentPagination: ComponentPagination
55 }): Observable<ResultList<VideoChannel>> {
56 const { search, componentPagination } = parameters
57
58 const url = SearchService.BASE_SEARCH_URL + 'video-channels'
59 const pagination = this.restService.componentPaginationToRestPagination(componentPagination)
60
61 let params = new HttpParams()
62 params = this.restService.addRestGetParams(params, pagination)
63 params = params.append('search', search)
64
65 return this.authHttp
66 .get<ResultList<VideoChannelServerModel>>(url, { params })
67 .pipe(
68 map(res => VideoChannelService.extractVideoChannels(res)),
69 catchError(err => this.restExtractor.handleError(err))
70 )
71 }
72 }