]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/user-subscription/subscribe-button.component.ts
Refractor notification service
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / user-subscription / subscribe-button.component.ts
CommitLineData
22a16e36 1import { Component, Input, OnInit } from '@angular/core'
660d11e9 2import { Router } from '@angular/router'
f8b2c1b4 3import { AuthService, Notifier } from '@app/core'
22a16e36
C
4import { UserSubscriptionService } from '@app/shared/user-subscription/user-subscription.service'
5import { VideoChannel } from '@app/shared/video-channel/video-channel.model'
22a16e36 6import { I18n } from '@ngx-translate/i18n-polyfill'
39ba2e8e
C
7import { VideoService } from '@app/shared/video/video.service'
8import { FeedFormat } from '../../../../../shared/models/feeds'
22a16e36
C
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
f37dc0dd 18 @Input() size: 'small' | 'normal' = 'normal'
22a16e36
C
19
20 subscribed: boolean
21
22 constructor (
660d11e9
RK
23 private authService: AuthService,
24 private router: Router,
f8b2c1b4 25 private notifier: Notifier,
22a16e36 26 private userSubscriptionService: UserSubscriptionService,
39ba2e8e
C
27 private i18n: I18n,
28 private videoService: VideoService
22a16e36
C
29 ) { }
30
31 get uri () {
32 return this.videoChannel.name + '@' + this.videoChannel.host
33 }
34
660d11e9
RK
35 get uriAccount () {
36 return this.videoChannel.ownerAccount.name + '@' + this.videoChannel.host
37 }
38
22a16e36 39 ngOnInit () {
660d11e9
RK
40 if (this.isUserLoggedIn()) {
41 this.userSubscriptionService.isSubscriptionExists(this.uri)
42 .subscribe(
43 res => this.subscribed = res[this.uri],
22a16e36 44
f8b2c1b4 45 err => this.notifier.error(err.message)
660d11e9
RK
46 )
47 }
22a16e36
C
48 }
49
50 subscribe () {
660d11e9 51 if (this.isUserLoggedIn()) {
dae5ca24 52 return this.localSubscribe()
660d11e9 53 }
dae5ca24
C
54
55 return this.gotoLogin()
660d11e9
RK
56 }
57
58 localSubscribe () {
22a16e36
C
59 this.userSubscriptionService.addSubscription(this.uri)
60 .subscribe(
61 () => {
62 this.subscribed = true
63
f8b2c1b4
C
64 this.notifier.success(
65 this.i18n('Subscribed to {{nameWithHost}}', { nameWithHost: this.videoChannel.displayName }),
66 this.i18n('Subscribed')
22a16e36
C
67 )
68 },
69
f8b2c1b4 70 err => this.notifier.error(err.message)
22a16e36
C
71 )
72 }
73
74 unsubscribe () {
660d11e9
RK
75 if (this.isUserLoggedIn()) {
76 this.localUnsubscribe()
77 }
78 }
79
80 localUnsubscribe () {
22a16e36
C
81 this.userSubscriptionService.deleteSubscription(this.uri)
82 .subscribe(
83 () => {
84 this.subscribed = false
85
f8b2c1b4
C
86 this.notifier.success(
87 this.i18n('Unsubscribed from {{nameWithHost}}', { nameWithHost: this.videoChannel.displayName }),
88 this.i18n('Unsubscribed')
22a16e36
C
89 )
90 },
91
f8b2c1b4 92 err => this.notifier.error(err.message)
22a16e36
C
93 )
94 }
660d11e9
RK
95
96 isUserLoggedIn () {
97 return this.authService.isLoggedIn()
98 }
99
100 gotoLogin () {
101 this.router.navigate([ '/login' ])
102 }
103
104 rssOpen () {
39ba2e8e
C
105 const rssFeed = this.videoService
106 .getVideoChannelFeedUrls(this.videoChannel.id)
107 .find(i => i.format === FeedFormat.RSS)
108
109 window.open(rssFeed.url)
660d11e9 110 }
22a16e36 111}