diff options
author | Chocobozzz <me@florianbigard.com> | 2018-08-21 16:18:59 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2018-08-27 09:41:54 +0200 |
commit | 22a16e36f6526887ed8f5e5d3c9f9e5da0b4a8cd (patch) | |
tree | 93c53e0619f966bd9ff1bb698c411277a9447a41 /client/src/app/shared/user-subscription | |
parent | 99492dbc0d87ef54d0dab7d8d44f8d0de5722bdd (diff) | |
download | PeerTube-22a16e36f6526887ed8f5e5d3c9f9e5da0b4a8cd.tar.gz PeerTube-22a16e36f6526887ed8f5e5d3c9f9e5da0b4a8cd.tar.zst PeerTube-22a16e36f6526887ed8f5e5d3c9f9e5da0b4a8cd.zip |
Add local user subscriptions
Diffstat (limited to 'client/src/app/shared/user-subscription')
5 files changed, 194 insertions, 0 deletions
diff --git a/client/src/app/shared/user-subscription/index.ts b/client/src/app/shared/user-subscription/index.ts new file mode 100644 index 000000000..024b36a41 --- /dev/null +++ b/client/src/app/shared/user-subscription/index.ts | |||
@@ -0,0 +1,2 @@ | |||
1 | export * from './user-subscription.service' | ||
2 | export * from './subscribe-button.component' \ No newline at end of file | ||
diff --git a/client/src/app/shared/user-subscription/subscribe-button.component.html b/client/src/app/shared/user-subscription/subscribe-button.component.html new file mode 100644 index 000000000..63b313662 --- /dev/null +++ b/client/src/app/shared/user-subscription/subscribe-button.component.html | |||
@@ -0,0 +1,15 @@ | |||
1 | <span i18n *ngIf="subscribed === false" class="subscribe-button" role="button" (click)="subscribe()"> | ||
2 | <span>Subscribe</span> | ||
3 | <span *ngIf="displayFollowers && videoChannel.followersCount !== 0" class="followers-count"> | ||
4 | {{ videoChannel.followersCount | myNumberFormatter }} | ||
5 | </span> | ||
6 | </span> | ||
7 | |||
8 | <span *ngIf="subscribed === true" class="unsubscribe-button" role="button" (click)="unsubscribe()"> | ||
9 | <span class="subscribed" i18n>Subscribed</span> | ||
10 | <span class="unsubscribe" i18n>Unsubscribe</span> | ||
11 | |||
12 | <span *ngIf="displayFollowers && videoChannel.followersCount !== 0" class="followers-count"> | ||
13 | {{ videoChannel.followersCount | myNumberFormatter }} | ||
14 | </span> | ||
15 | </span> | ||
diff --git a/client/src/app/shared/user-subscription/subscribe-button.component.scss b/client/src/app/shared/user-subscription/subscribe-button.component.scss new file mode 100644 index 000000000..9811fdc0c --- /dev/null +++ b/client/src/app/shared/user-subscription/subscribe-button.component.scss | |||
@@ -0,0 +1,37 @@ | |||
1 | @import '_variables'; | ||
2 | @import '_mixins'; | ||
3 | |||
4 | .subscribe-button { | ||
5 | @include peertube-button; | ||
6 | @include orange-button; | ||
7 | } | ||
8 | |||
9 | .unsubscribe-button { | ||
10 | @include peertube-button; | ||
11 | @include grey-button | ||
12 | } | ||
13 | |||
14 | .subscribe-button, | ||
15 | .unsubscribe-button { | ||
16 | padding: 3px 7px; | ||
17 | } | ||
18 | |||
19 | .unsubscribe-button { | ||
20 | .subscribed { | ||
21 | display: inline; | ||
22 | } | ||
23 | |||
24 | .unsubscribe { | ||
25 | display: none; | ||
26 | } | ||
27 | |||
28 | &:hover { | ||
29 | .subscribed { | ||
30 | display: none; | ||
31 | } | ||
32 | |||
33 | .unsubscribe { | ||
34 | display: inline; | ||
35 | } | ||
36 | } | ||
37 | } \ No newline at end of file | ||
diff --git a/client/src/app/shared/user-subscription/subscribe-button.component.ts b/client/src/app/shared/user-subscription/subscribe-button.component.ts new file mode 100644 index 000000000..46d6dbaf7 --- /dev/null +++ b/client/src/app/shared/user-subscription/subscribe-button.component.ts | |||
@@ -0,0 +1,74 @@ | |||
1 | import { Component, Input, OnInit } from '@angular/core' | ||
2 | import { AuthService } from '@app/core' | ||
3 | import { RestExtractor } from '@app/shared/rest' | ||
4 | import { RedirectService } from '@app/core/routing/redirect.service' | ||
5 | import { UserSubscriptionService } from '@app/shared/user-subscription/user-subscription.service' | ||
6 | import { VideoChannel } from '@app/shared/video-channel/video-channel.model' | ||
7 | import { NotificationsService } from 'angular2-notifications' | ||
8 | import { I18n } from '@ngx-translate/i18n-polyfill' | ||
9 | |||
10 | @Component({ | ||
11 | selector: 'my-subscribe-button', | ||
12 | templateUrl: './subscribe-button.component.html', | ||
13 | styleUrls: [ './subscribe-button.component.scss' ] | ||
14 | }) | ||
15 | export class SubscribeButtonComponent implements OnInit { | ||
16 | @Input() videoChannel: VideoChannel | ||
17 | @Input() displayFollowers = false | ||
18 | |||
19 | subscribed: boolean | ||
20 | |||
21 | constructor ( | ||
22 | private authService: AuthService, | ||
23 | private restExtractor: RestExtractor, | ||
24 | private redirectService: RedirectService, | ||
25 | private notificationsService: NotificationsService, | ||
26 | private userSubscriptionService: UserSubscriptionService, | ||
27 | private i18n: I18n | ||
28 | ) { } | ||
29 | |||
30 | get uri () { | ||
31 | return this.videoChannel.name + '@' + this.videoChannel.host | ||
32 | } | ||
33 | |||
34 | ngOnInit () { | ||
35 | this.userSubscriptionService.isSubscriptionExists(this.uri) | ||
36 | .subscribe( | ||
37 | exists => this.subscribed = exists, | ||
38 | |||
39 | err => this.notificationsService.error(this.i18n('Error'), err.message) | ||
40 | ) | ||
41 | } | ||
42 | |||
43 | subscribe () { | ||
44 | this.userSubscriptionService.addSubscription(this.uri) | ||
45 | .subscribe( | ||
46 | () => { | ||
47 | this.subscribed = true | ||
48 | |||
49 | this.notificationsService.success( | ||
50 | this.i18n('Subscribed'), | ||
51 | this.i18n('Subscribed to {{nameWithHost}}', { nameWithHost: this.videoChannel.displayName }) | ||
52 | ) | ||
53 | }, | ||
54 | |||
55 | err => this.notificationsService.error(this.i18n('Error'), err.message) | ||
56 | ) | ||
57 | } | ||
58 | |||
59 | unsubscribe () { | ||
60 | this.userSubscriptionService.deleteSubscription(this.uri) | ||
61 | .subscribe( | ||
62 | () => { | ||
63 | this.subscribed = false | ||
64 | |||
65 | this.notificationsService.success( | ||
66 | this.i18n('Unsubscribed'), | ||
67 | this.i18n('Unsubscribed from {{nameWithHost}}', { nameWithHost: this.videoChannel.displayName }) | ||
68 | ) | ||
69 | }, | ||
70 | |||
71 | err => this.notificationsService.error(this.i18n('Error'), err.message) | ||
72 | ) | ||
73 | } | ||
74 | } | ||
diff --git a/client/src/app/shared/user-subscription/user-subscription.service.ts b/client/src/app/shared/user-subscription/user-subscription.service.ts new file mode 100644 index 000000000..3103706d1 --- /dev/null +++ b/client/src/app/shared/user-subscription/user-subscription.service.ts | |||
@@ -0,0 +1,66 @@ | |||
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 | } | ||