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