aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/shared/user-subscription
diff options
context:
space:
mode:
Diffstat (limited to 'client/src/app/shared/user-subscription')
-rw-r--r--client/src/app/shared/user-subscription/index.ts2
-rw-r--r--client/src/app/shared/user-subscription/subscribe-button.component.html15
-rw-r--r--client/src/app/shared/user-subscription/subscribe-button.component.scss37
-rw-r--r--client/src/app/shared/user-subscription/subscribe-button.component.ts74
-rw-r--r--client/src/app/shared/user-subscription/user-subscription.service.ts66
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 @@
1export * from './user-subscription.service'
2export * 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 @@
1import { Component, Input, OnInit } from '@angular/core'
2import { AuthService } from '@app/core'
3import { RestExtractor } from '@app/shared/rest'
4import { RedirectService } from '@app/core/routing/redirect.service'
5import { UserSubscriptionService } from '@app/shared/user-subscription/user-subscription.service'
6import { VideoChannel } from '@app/shared/video-channel/video-channel.model'
7import { NotificationsService } from 'angular2-notifications'
8import { 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})
15export 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 @@
1import { catchError, map } from 'rxjs/operators'
2import { HttpClient } from '@angular/common/http'
3import { Injectable } from '@angular/core'
4import { ResultList } from '../../../../../shared'
5import { environment } from '../../../environments/environment'
6import { RestExtractor } from '../rest'
7import { Observable, of } from 'rxjs'
8import { VideoChannel } from '@app/shared/video-channel/video-channel.model'
9import { VideoChannelService } from '@app/shared/video-channel/video-channel.service'
10import { VideoChannel as VideoChannelServer } from '../../../../../shared/models/videos'
11
12@Injectable()
13export 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}