]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/user-subscription/subscribe-button.component.ts
Refactor my-subscribe-button to support full account subscription
[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'
41eb700f
RK
9import { Account } from '@app/shared/account/account.model'
10import { forkJoin } from 'rxjs'
22a16e36
C
11
12@Component({
13 selector: 'my-subscribe-button',
14 templateUrl: './subscribe-button.component.html',
15 styleUrls: [ './subscribe-button.component.scss' ]
16})
17export class SubscribeButtonComponent implements OnInit {
41eb700f
RK
18 @Input() account: Account
19 @Input() videoChannels: VideoChannel[]
22a16e36 20 @Input() displayFollowers = false
f37dc0dd 21 @Input() size: 'small' | 'normal' = 'normal'
22a16e36 22
41eb700f 23 subscribed: Map<string, boolean>
22a16e36
C
24
25 constructor (
660d11e9
RK
26 private authService: AuthService,
27 private router: Router,
f8b2c1b4 28 private notifier: Notifier,
22a16e36 29 private userSubscriptionService: UserSubscriptionService,
39ba2e8e
C
30 private i18n: I18n,
31 private videoService: VideoService
41eb700f
RK
32 ) {
33 this.subscribed = new Map<string, boolean>()
34 }
35
36 get handle () {
37 return this.account
38 ? this.account.nameWithHost
39 : this.videoChannels[0].name + '@' + this.videoChannels[0].host
40 }
22a16e36 41
3ddb1ec5 42 get channelHandle () {
41eb700f 43 return this.getChannelHandler(this.videoChannels[0])
22a16e36
C
44 }
45
41eb700f
RK
46 get uri () {
47 return this.account
48 ? this.account.url
49 : this.videoChannels[0].url
660d11e9
RK
50 }
51
405ec98b 52 get rssUri () {
41eb700f
RK
53 const rssFeed = this.account
54 ? this.videoService
55 .getAccountFeedUrls(this.account.id)
56 .find(i => i.format === FeedFormat.RSS)
57 : this.videoService
58 .getVideoChannelFeedUrls(this.videoChannels[0].id)
59 .find(i => i.format === FeedFormat.RSS)
405ec98b
FS
60
61 return rssFeed.url
62 }
63
22a16e36 64 ngOnInit () {
660d11e9 65 if (this.isUserLoggedIn()) {
22a16e36 66
41eb700f
RK
67 forkJoin(this.videoChannels.map(videoChannel => {
68 const handle = this.getChannelHandler(videoChannel)
69 this.subscribed.set(handle, false)
70 this.userSubscriptionService.doesSubscriptionExist(handle)
71 .subscribe(
72 res => this.subscribed.set(handle, res[handle]),
73
74 err => this.notifier.error(err.message)
75 )
76 }))
660d11e9 77 }
22a16e36
C
78 }
79
80 subscribe () {
660d11e9 81 if (this.isUserLoggedIn()) {
dae5ca24 82 return this.localSubscribe()
660d11e9 83 }
dae5ca24
C
84
85 return this.gotoLogin()
660d11e9
RK
86 }
87
88 localSubscribe () {
41eb700f
RK
89 const observableBatch: any = []
90
91 this.videoChannels
92 .filter(videoChannel => this.subscribeStatus(false).includes(this.getChannelHandler(videoChannel)))
93 .forEach(videoChannel => observableBatch.push(
94 this.userSubscriptionService.addSubscription(this.getChannelHandler(videoChannel))
95 ))
96
97 forkJoin(observableBatch)
22a16e36
C
98 .subscribe(
99 () => {
41eb700f
RK
100 [...this.subscribed.keys()].forEach((key) => {
101 this.subscribed.set(key, true)
102 })
22a16e36 103
f8b2c1b4 104 this.notifier.success(
41eb700f
RK
105 this.account
106 ? this.i18n(
107 'Subscribed to all current channels of {{nameWithHost}}. ' +
108 'You will be notified of all their new videos.',
109 { nameWithHost: this.account.displayName }
110 )
111 : this.i18n(
112 'Subscribed to {{nameWithHost}}. ' +
113 'You will be notified of all their new videos.',
114 { nameWithHost: this.videoChannels[0].displayName }
115 )
116 ,
f8b2c1b4 117 this.i18n('Subscribed')
22a16e36
C
118 )
119 },
120
f8b2c1b4 121 err => this.notifier.error(err.message)
22a16e36
C
122 )
123 }
124
125 unsubscribe () {
660d11e9
RK
126 if (this.isUserLoggedIn()) {
127 this.localUnsubscribe()
128 }
129 }
130
131 localUnsubscribe () {
41eb700f
RK
132 const observableBatch: any = []
133
134 this.videoChannels
135 .filter(videoChannel => this.subscribeStatus(true).includes(this.getChannelHandler(videoChannel)))
136 .forEach(videoChannel => observableBatch.push(
137 this.userSubscriptionService.deleteSubscription(this.getChannelHandler(videoChannel))
138 ))
139
140 forkJoin(observableBatch)
22a16e36
C
141 .subscribe(
142 () => {
41eb700f
RK
143 [...this.subscribed.keys()].forEach((key) => {
144 this.subscribed.set(key, false)
145 })
22a16e36 146
f8b2c1b4 147 this.notifier.success(
41eb700f
RK
148 this.account
149 ? this.i18n('Unsubscribed from all channels of {{nameWithHost}}', { nameWithHost: this.account.nameWithHost })
150 : this.i18n('Unsubscribed from {{nameWithHost}}', { nameWithHost: this.videoChannels[0].nameWithHost })
151 ,
f8b2c1b4 152 this.i18n('Unsubscribed')
22a16e36
C
153 )
154 },
155
f8b2c1b4 156 err => this.notifier.error(err.message)
22a16e36
C
157 )
158 }
660d11e9
RK
159
160 isUserLoggedIn () {
161 return this.authService.isLoggedIn()
162 }
163
41eb700f
RK
164 isAllChannelsSubscribed () {
165 return !Array.from(this.subscribed.values()).includes(false)
166 }
167
660d11e9
RK
168 gotoLogin () {
169 this.router.navigate([ '/login' ])
170 }
41eb700f
RK
171
172 private getChannelHandler (videoChannel: VideoChannel) {
173 return videoChannel.name + '@' + videoChannel.host
174 }
175
176 private subscribeStatus (subscribed: boolean) {
177 const accumulator = []
178 for (const [key, value] of this.subscribed.entries()) {
179 if (value === subscribed) accumulator.push(key)
180 }
181 return accumulator
182 }
22a16e36 183}