Add local user subscriptions
[oweals/peertube.git] / client / src / app / shared / user-subscription / subscribe-button.component.ts
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 }