]>
Commit | Line | Data |
---|---|---|
22a16e36 C |
1 | import { catchError, map } from 'rxjs/operators' |
2 | import { HttpClient } from '@angular/common/http' | |
3 | import { Injectable } from '@angular/core' | |
4 | import { ResultList } from '../../../../../shared' | |
5 | import { environment } from '../../../environments/environment' | |
6 | import { RestExtractor } from '../rest' | |
7 | import { Observable, of } from 'rxjs' | |
8 | import { VideoChannel } from '@app/shared/video-channel/video-channel.model' | |
9 | import { VideoChannelService } from '@app/shared/video-channel/video-channel.service' | |
10 | import { VideoChannel as VideoChannelServer } from '../../../../../shared/models/videos' | |
11 | ||
12 | @Injectable() | |
13 | export class UserSubscriptionService { | |
14 | static BASE_USER_SUBSCRIPTIONS_URL = environment.apiUrl + '/api/v1/users/me/subscriptions' | |
15 | ||
16 | constructor ( | |
17 | private authHttp: HttpClient, | |
18 | private restExtractor: RestExtractor | |
19 | ) { | |
20 | } | |
21 | ||
22 | deleteSubscription (nameWithHost: string) { | |
23 | const url = UserSubscriptionService.BASE_USER_SUBSCRIPTIONS_URL + '/' + nameWithHost | |
24 | ||
25 | return this.authHttp.delete(url) | |
26 | .pipe( | |
27 | map(this.restExtractor.extractDataBool), | |
28 | catchError(err => this.restExtractor.handleError(err)) | |
29 | ) | |
30 | } | |
31 | ||
32 | addSubscription (nameWithHost: string) { | |
33 | const url = UserSubscriptionService.BASE_USER_SUBSCRIPTIONS_URL | |
34 | ||
35 | const body = { uri: nameWithHost } | |
36 | return this.authHttp.post(url, body) | |
37 | .pipe( | |
38 | map(this.restExtractor.extractDataBool), | |
39 | catchError(err => this.restExtractor.handleError(err)) | |
40 | ) | |
41 | } | |
42 | ||
43 | listSubscriptions (): Observable<ResultList<VideoChannel>> { | |
44 | const url = UserSubscriptionService.BASE_USER_SUBSCRIPTIONS_URL | |
45 | ||
46 | return this.authHttp.get<ResultList<VideoChannelServer>>(url) | |
47 | .pipe( | |
48 | map(res => VideoChannelService.extractVideoChannels(res)), | |
49 | catchError(err => this.restExtractor.handleError(err)) | |
50 | ) | |
51 | } | |
52 | ||
53 | isSubscriptionExists (nameWithHost: string): Observable<boolean> { | |
54 | const url = UserSubscriptionService.BASE_USER_SUBSCRIPTIONS_URL + '/' + nameWithHost | |
55 | ||
56 | return this.authHttp.get(url) | |
57 | .pipe( | |
58 | map(this.restExtractor.extractDataBool), | |
59 | catchError(err => { | |
60 | if (err.status === 404) return of(false) | |
61 | ||
62 | return this.restExtractor.handleError(err) | |
63 | }) | |
64 | ) | |
65 | } | |
66 | } |