Rename streaming playlists routes/directories
[oweals/peertube.git] / client / src / app / shared / users / user-history.service.ts
1 import { HttpClient, HttpParams } from '@angular/common/http'
2 import { Injectable } from '@angular/core'
3 import { environment } from '../../../environments/environment'
4 import { RestExtractor } from '../rest/rest-extractor.service'
5 import { RestService } from '../rest/rest.service'
6 import { Video } from '../video/video.model'
7 import { catchError, map, switchMap } from 'rxjs/operators'
8 import { ComponentPagination } from '@app/shared/rest/component-pagination.model'
9 import { VideoService } from '@app/shared/video/video.service'
10 import { ResultList } from '../../../../../shared'
11
12 @Injectable()
13 export class UserHistoryService {
14   static BASE_USER_VIDEOS_HISTORY_URL = environment.apiUrl + '/api/v1/users/me/history/videos'
15
16   constructor (
17     private authHttp: HttpClient,
18     private restExtractor: RestExtractor,
19     private restService: RestService,
20     private videoService: VideoService
21   ) {}
22
23   getUserVideosHistory (historyPagination: ComponentPagination) {
24     const pagination = this.restService.componentPaginationToRestPagination(historyPagination)
25
26     let params = new HttpParams()
27     params = this.restService.addRestGetParams(params, pagination)
28
29     return this.authHttp
30                .get<ResultList<Video>>(UserHistoryService.BASE_USER_VIDEOS_HISTORY_URL, { params })
31                .pipe(
32                  switchMap(res => this.videoService.extractVideos(res)),
33                  catchError(err => this.restExtractor.handleError(err))
34                )
35   }
36
37   deleteUserVideosHistory () {
38     return this.authHttp
39                .post(UserHistoryService.BASE_USER_VIDEOS_HISTORY_URL + '/remove', {})
40                .pipe(
41                  map(() => this.restExtractor.extractDataBool()),
42                  catchError(err => this.restExtractor.handleError(err))
43                )
44   }
45 }