]>
Commit | Line | Data |
---|---|---|
67ed6552 C |
1 | import { concat, forkJoin, merge } from 'rxjs' |
2 | import { Component, Input, OnChanges, OnInit } from '@angular/core' | |
98bd5e22 | 3 | import { AuthService, Notifier, RedirectService } from '@app/core' |
67ed6552 | 4 | import { Account, VideoChannel, VideoService } from '@app/shared/shared-main' |
67ed6552 C |
5 | import { FeedFormat } from '@shared/models' |
6 | import { UserSubscriptionService } from './user-subscription.service' | |
22a16e36 C |
7 | |
8 | @Component({ | |
9 | selector: 'my-subscribe-button', | |
10 | templateUrl: './subscribe-button.component.html', | |
11 | styleUrls: [ './subscribe-button.component.scss' ] | |
12 | }) | |
f8b65c22 | 13 | export class SubscribeButtonComponent implements OnInit, OnChanges { |
b061c8ed RK |
14 | /** |
15 | * SubscribeButtonComponent can be used with a single VideoChannel passed as [VideoChannel], | |
16 | * or with an account and a full list of that account's videoChannels. The latter is intended | |
17 | * to allow mass un/subscription from an account's page, while keeping the channel-centric | |
18 | * subscription model. | |
19 | */ | |
41eb700f RK |
20 | @Input() account: Account |
21 | @Input() videoChannels: VideoChannel[] | |
22a16e36 | 22 | @Input() displayFollowers = false |
f37dc0dd | 23 | @Input() size: 'small' | 'normal' = 'normal' |
22a16e36 | 24 | |
9270ccf6 | 25 | subscribed = new Map<string, boolean>() |
22a16e36 C |
26 | |
27 | constructor ( | |
660d11e9 | 28 | private authService: AuthService, |
98bd5e22 | 29 | private redirectService: RedirectService, |
f8b2c1b4 | 30 | private notifier: Notifier, |
22a16e36 | 31 | private userSubscriptionService: UserSubscriptionService, |
39ba2e8e | 32 | private videoService: VideoService |
9270ccf6 | 33 | ) { } |
41eb700f RK |
34 | |
35 | get handle () { | |
36 | return this.account | |
37 | ? this.account.nameWithHost | |
db84cf89 | 38 | : this.videoChannel.name + '@' + this.videoChannel.host |
41eb700f | 39 | } |
22a16e36 | 40 | |
3ddb1ec5 | 41 | get channelHandle () { |
db84cf89 | 42 | return this.getChannelHandler(this.videoChannel) |
22a16e36 C |
43 | } |
44 | ||
41eb700f RK |
45 | get uri () { |
46 | return this.account | |
47 | ? this.account.url | |
48 | : this.videoChannels[0].url | |
660d11e9 RK |
49 | } |
50 | ||
405ec98b | 51 | get rssUri () { |
41eb700f RK |
52 | const rssFeed = this.account |
53 | ? this.videoService | |
54 | .getAccountFeedUrls(this.account.id) | |
55 | .find(i => i.format === FeedFormat.RSS) | |
56 | : this.videoService | |
57 | .getVideoChannelFeedUrls(this.videoChannels[0].id) | |
58 | .find(i => i.format === FeedFormat.RSS) | |
405ec98b FS |
59 | |
60 | return rssFeed.url | |
61 | } | |
62 | ||
db84cf89 C |
63 | get videoChannel () { |
64 | return this.videoChannels[0] | |
65 | } | |
66 | ||
f8b65c22 RK |
67 | get isAllChannelsSubscribed () { |
68 | return this.subscribeStatus(true).length === this.videoChannels.length | |
69 | } | |
70 | ||
71 | get isAtLeastOneChannelSubscribed () { | |
72 | return this.subscribeStatus(true).length > 0 | |
73 | } | |
74 | ||
75 | get isBigButton () { | |
76 | return this.isUserLoggedIn() && this.videoChannels.length > 1 && this.isAtLeastOneChannelSubscribed | |
77 | } | |
78 | ||
22a16e36 | 79 | ngOnInit () { |
9270ccf6 | 80 | this.loadSubscribedStatus() |
22a16e36 C |
81 | } |
82 | ||
f8b65c22 RK |
83 | ngOnChanges () { |
84 | this.ngOnInit() | |
85 | } | |
86 | ||
22a16e36 | 87 | subscribe () { |
660d11e9 | 88 | if (this.isUserLoggedIn()) { |
dae5ca24 | 89 | return this.localSubscribe() |
660d11e9 | 90 | } |
dae5ca24 C |
91 | |
92 | return this.gotoLogin() | |
660d11e9 RK |
93 | } |
94 | ||
95 | localSubscribe () { | |
ab4d4db4 C |
96 | const subscribedStatus = this.subscribeStatus(false) |
97 | ||
9270ccf6 RK |
98 | const observableBatch = this.videoChannels |
99 | .map(videoChannel => this.getChannelHandler(videoChannel)) | |
ab4d4db4 | 100 | .filter(handle => subscribedStatus.includes(handle)) |
9270ccf6 | 101 | .map(handle => this.userSubscriptionService.addSubscription(handle)) |
41eb700f | 102 | |
ab4d4db4 | 103 | forkJoin(observableBatch) |
1378c0d3 C |
104 | .subscribe({ |
105 | next: () => { | |
f8b2c1b4 | 106 | this.notifier.success( |
41eb700f | 107 | this.account |
66357162 C |
108 | ? $localize`Subscribed to all current channels of ${this.account.displayName}. You will be notified of all their new videos.` |
109 | : $localize`Subscribed to ${this.videoChannels[0].displayName}. You will be notified of all their new videos.`, | |
110 | ||
111 | $localize`Subscribed` | |
22a16e36 C |
112 | ) |
113 | }, | |
114 | ||
1378c0d3 C |
115 | error: err => this.notifier.error(err.message) |
116 | }) | |
22a16e36 C |
117 | } |
118 | ||
119 | unsubscribe () { | |
660d11e9 RK |
120 | if (this.isUserLoggedIn()) { |
121 | this.localUnsubscribe() | |
122 | } | |
123 | } | |
124 | ||
125 | localUnsubscribe () { | |
ab4d4db4 C |
126 | const subscribeStatus = this.subscribeStatus(true) |
127 | ||
9270ccf6 | 128 | const observableBatch = this.videoChannels |
ab4d4db4 C |
129 | .map(videoChannel => this.getChannelHandler(videoChannel)) |
130 | .filter(handle => subscribeStatus.includes(handle)) | |
131 | .map(handle => this.userSubscriptionService.deleteSubscription(handle)) | |
41eb700f | 132 | |
ab4d4db4 C |
133 | concat(...observableBatch) |
134 | .subscribe({ | |
135 | complete: () => { | |
136 | this.notifier.success( | |
137 | this.account | |
66357162 | 138 | ? $localize`Unsubscribed from all channels of ${this.account.nameWithHost}` |
9df52d66 | 139 | : $localize`Unsubscribed from ${this.videoChannels[0].nameWithHost}`, |
66357162 C |
140 | |
141 | $localize`Unsubscribed` | |
ab4d4db4 C |
142 | ) |
143 | }, | |
22a16e36 | 144 | |
ab4d4db4 C |
145 | error: err => this.notifier.error(err.message) |
146 | }) | |
22a16e36 | 147 | } |
660d11e9 RK |
148 | |
149 | isUserLoggedIn () { | |
150 | return this.authService.isLoggedIn() | |
151 | } | |
152 | ||
153 | gotoLogin () { | |
98bd5e22 | 154 | this.redirectService.redirectToLogin() |
660d11e9 | 155 | } |
41eb700f | 156 | |
db84cf89 | 157 | subscribeStatus (subscribed: boolean) { |
9270ccf6 | 158 | const accumulator: string[] = [] |
9df52d66 | 159 | for (const [ key, value ] of this.subscribed.entries()) { |
41eb700f RK |
160 | if (value === subscribed) accumulator.push(key) |
161 | } | |
db84cf89 | 162 | |
41eb700f RK |
163 | return accumulator |
164 | } | |
9270ccf6 | 165 | |
77d873c5 C |
166 | isSubscribedToAll () { |
167 | return Array.from(this.subscribed.values()).every(v => v === true) | |
168 | } | |
169 | ||
1d54b205 C |
170 | isRemoteSubscribeAvailable () { |
171 | return !this.isUserLoggedIn() | |
172 | } | |
173 | ||
db84cf89 C |
174 | private getChannelHandler (videoChannel: VideoChannel) { |
175 | return videoChannel.name + '@' + videoChannel.host | |
176 | } | |
177 | ||
9270ccf6 RK |
178 | private loadSubscribedStatus () { |
179 | if (!this.isUserLoggedIn()) return | |
180 | ||
181 | for (const videoChannel of this.videoChannels) { | |
182 | const handle = this.getChannelHandler(videoChannel) | |
183 | this.subscribed.set(handle, false) | |
ab4d4db4 | 184 | |
9270ccf6 RK |
185 | merge( |
186 | this.userSubscriptionService.listenToSubscriptionCacheChange(handle), | |
187 | this.userSubscriptionService.doesSubscriptionExist(handle) | |
1378c0d3 C |
188 | ).subscribe({ |
189 | next: res => this.subscribed.set(handle, res), | |
9270ccf6 | 190 | |
1378c0d3 C |
191 | error: err => this.notifier.error(err.message) |
192 | }) | |
9270ccf6 RK |
193 | } |
194 | } | |
22a16e36 | 195 | } |