]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - 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
1 import { Component, Input, OnInit } from '@angular/core'
2 import { Router } from '@angular/router'
3 import { AuthService, Notifier } from '@app/core'
4 import { UserSubscriptionService } from '@app/shared/user-subscription/user-subscription.service'
5 import { VideoChannel } from '@app/shared/video-channel/video-channel.model'
6 import { I18n } from '@ngx-translate/i18n-polyfill'
7 import { VideoService } from '@app/shared/video/video.service'
8 import { FeedFormat } from '../../../../../shared/models/feeds'
9 import { Account } from '@app/shared/account/account.model'
10 import { forkJoin } from 'rxjs'
11
12 @Component({
13 selector: 'my-subscribe-button',
14 templateUrl: './subscribe-button.component.html',
15 styleUrls: [ './subscribe-button.component.scss' ]
16 })
17 export class SubscribeButtonComponent implements OnInit {
18 @Input() account: Account
19 @Input() videoChannels: VideoChannel[]
20 @Input() displayFollowers = false
21 @Input() size: 'small' | 'normal' = 'normal'
22
23 subscribed: Map<string, boolean>
24
25 constructor (
26 private authService: AuthService,
27 private router: Router,
28 private notifier: Notifier,
29 private userSubscriptionService: UserSubscriptionService,
30 private i18n: I18n,
31 private videoService: VideoService
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 }
41
42 get channelHandle () {
43 return this.getChannelHandler(this.videoChannels[0])
44 }
45
46 get uri () {
47 return this.account
48 ? this.account.url
49 : this.videoChannels[0].url
50 }
51
52 get rssUri () {
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)
60
61 return rssFeed.url
62 }
63
64 ngOnInit () {
65 if (this.isUserLoggedIn()) {
66
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 }))
77 }
78 }
79
80 subscribe () {
81 if (this.isUserLoggedIn()) {
82 return this.localSubscribe()
83 }
84
85 return this.gotoLogin()
86 }
87
88 localSubscribe () {
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)
98 .subscribe(
99 () => {
100 [...this.subscribed.keys()].forEach((key) => {
101 this.subscribed.set(key, true)
102 })
103
104 this.notifier.success(
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 ,
117 this.i18n('Subscribed')
118 )
119 },
120
121 err => this.notifier.error(err.message)
122 )
123 }
124
125 unsubscribe () {
126 if (this.isUserLoggedIn()) {
127 this.localUnsubscribe()
128 }
129 }
130
131 localUnsubscribe () {
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)
141 .subscribe(
142 () => {
143 [...this.subscribed.keys()].forEach((key) => {
144 this.subscribed.set(key, false)
145 })
146
147 this.notifier.success(
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 ,
152 this.i18n('Unsubscribed')
153 )
154 },
155
156 err => this.notifier.error(err.message)
157 )
158 }
159
160 isUserLoggedIn () {
161 return this.authService.isLoggedIn()
162 }
163
164 isAllChannelsSubscribed () {
165 return !Array.from(this.subscribed.values()).includes(false)
166 }
167
168 gotoLogin () {
169 this.router.navigate([ '/login' ])
170 }
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 }
183 }