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