]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/user-subscription/subscribe-button.component.ts
Skip videos count on client if we don't use it
[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
3ddb1ec5 31 get channelHandle () {
22a16e36
C
32 return this.videoChannel.name + '@' + this.videoChannel.host
33 }
34
3ddb1ec5
C
35 get channelUri () {
36 return this.videoChannel.url
660d11e9
RK
37 }
38
405ec98b
FS
39 get rssUri () {
40 const rssFeed = this.videoService
41 .getVideoChannelFeedUrls(this.videoChannel.id)
42 .find(i => i.format === FeedFormat.RSS)
43
44 return rssFeed.url
45 }
46
22a16e36 47 ngOnInit () {
660d11e9 48 if (this.isUserLoggedIn()) {
3ddb1ec5 49 this.userSubscriptionService.doesSubscriptionExist(this.channelHandle)
660d11e9 50 .subscribe(
3ddb1ec5 51 res => this.subscribed = res[this.channelHandle],
22a16e36 52
f8b2c1b4 53 err => this.notifier.error(err.message)
660d11e9
RK
54 )
55 }
22a16e36
C
56 }
57
58 subscribe () {
660d11e9 59 if (this.isUserLoggedIn()) {
dae5ca24 60 return this.localSubscribe()
660d11e9 61 }
dae5ca24
C
62
63 return this.gotoLogin()
660d11e9
RK
64 }
65
66 localSubscribe () {
3ddb1ec5 67 this.userSubscriptionService.addSubscription(this.channelHandle)
22a16e36
C
68 .subscribe(
69 () => {
70 this.subscribed = true
71
f8b2c1b4 72 this.notifier.success(
5def76eb
RK
73 this.i18n(
74 'Subscribed to {{nameWithHost}}. ' +
75 'You will be notified of all their new videos.',
76 { nameWithHost: this.videoChannel.displayName }
77 ),
f8b2c1b4 78 this.i18n('Subscribed')
22a16e36
C
79 )
80 },
81
f8b2c1b4 82 err => this.notifier.error(err.message)
22a16e36
C
83 )
84 }
85
86 unsubscribe () {
660d11e9
RK
87 if (this.isUserLoggedIn()) {
88 this.localUnsubscribe()
89 }
90 }
91
92 localUnsubscribe () {
3ddb1ec5 93 this.userSubscriptionService.deleteSubscription(this.channelHandle)
22a16e36
C
94 .subscribe(
95 () => {
96 this.subscribed = false
97
f8b2c1b4
C
98 this.notifier.success(
99 this.i18n('Unsubscribed from {{nameWithHost}}', { nameWithHost: this.videoChannel.displayName }),
100 this.i18n('Unsubscribed')
22a16e36
C
101 )
102 },
103
f8b2c1b4 104 err => this.notifier.error(err.message)
22a16e36
C
105 )
106 }
660d11e9
RK
107
108 isUserLoggedIn () {
109 return this.authService.isLoggedIn()
110 }
111
112 gotoLogin () {
113 this.router.navigate([ '/login' ])
114 }
22a16e36 115}