]>
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 RK |
9 | import { Account } from '@app/shared/account/account.model' |
10 | import { 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 | }) | |
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 | |
41eb700f | 29 | subscribed: 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 | |
41eb700f RK |
38 | ) { |
39 | this.subscribed = new Map<string, boolean>() | |
40 | } | |
41 | ||
42 | get handle () { | |
43 | return this.account | |
44 | ? this.account.nameWithHost | |
45 | : this.videoChannels[0].name + '@' + this.videoChannels[0].host | |
46 | } | |
22a16e36 | 47 | |
3ddb1ec5 | 48 | get channelHandle () { |
41eb700f | 49 | return this.getChannelHandler(this.videoChannels[0]) |
22a16e36 C |
50 | } |
51 | ||
41eb700f RK |
52 | get uri () { |
53 | return this.account | |
54 | ? this.account.url | |
55 | : this.videoChannels[0].url | |
660d11e9 RK |
56 | } |
57 | ||
405ec98b | 58 | get rssUri () { |
41eb700f RK |
59 | const rssFeed = this.account |
60 | ? this.videoService | |
61 | .getAccountFeedUrls(this.account.id) | |
62 | .find(i => i.format === FeedFormat.RSS) | |
63 | : this.videoService | |
64 | .getVideoChannelFeedUrls(this.videoChannels[0].id) | |
65 | .find(i => i.format === FeedFormat.RSS) | |
405ec98b FS |
66 | |
67 | return rssFeed.url | |
68 | } | |
69 | ||
22a16e36 | 70 | ngOnInit () { |
660d11e9 | 71 | if (this.isUserLoggedIn()) { |
22a16e36 | 72 | |
41eb700f RK |
73 | forkJoin(this.videoChannels.map(videoChannel => { |
74 | const handle = this.getChannelHandler(videoChannel) | |
75 | this.subscribed.set(handle, false) | |
76 | this.userSubscriptionService.doesSubscriptionExist(handle) | |
77 | .subscribe( | |
78 | res => this.subscribed.set(handle, res[handle]), | |
79 | ||
80 | err => this.notifier.error(err.message) | |
81 | ) | |
82 | })) | |
660d11e9 | 83 | } |
22a16e36 C |
84 | } |
85 | ||
86 | subscribe () { | |
660d11e9 | 87 | if (this.isUserLoggedIn()) { |
dae5ca24 | 88 | return this.localSubscribe() |
660d11e9 | 89 | } |
dae5ca24 C |
90 | |
91 | return this.gotoLogin() | |
660d11e9 RK |
92 | } |
93 | ||
94 | localSubscribe () { | |
41eb700f RK |
95 | const observableBatch: any = [] |
96 | ||
97 | this.videoChannels | |
98 | .filter(videoChannel => this.subscribeStatus(false).includes(this.getChannelHandler(videoChannel))) | |
99 | .forEach(videoChannel => observableBatch.push( | |
100 | this.userSubscriptionService.addSubscription(this.getChannelHandler(videoChannel)) | |
101 | )) | |
102 | ||
103 | forkJoin(observableBatch) | |
22a16e36 C |
104 | .subscribe( |
105 | () => { | |
41eb700f RK |
106 | [...this.subscribed.keys()].forEach((key) => { |
107 | this.subscribed.set(key, true) | |
108 | }) | |
22a16e36 | 109 | |
f8b2c1b4 | 110 | this.notifier.success( |
41eb700f RK |
111 | this.account |
112 | ? this.i18n( | |
113 | 'Subscribed to all current channels of {{nameWithHost}}. ' + | |
114 | 'You will be notified of all their new videos.', | |
115 | { nameWithHost: this.account.displayName } | |
116 | ) | |
117 | : this.i18n( | |
118 | 'Subscribed to {{nameWithHost}}. ' + | |
119 | 'You will be notified of all their new videos.', | |
120 | { nameWithHost: this.videoChannels[0].displayName } | |
121 | ) | |
122 | , | |
f8b2c1b4 | 123 | this.i18n('Subscribed') |
22a16e36 C |
124 | ) |
125 | }, | |
126 | ||
f8b2c1b4 | 127 | err => this.notifier.error(err.message) |
22a16e36 C |
128 | ) |
129 | } | |
130 | ||
131 | unsubscribe () { | |
660d11e9 RK |
132 | if (this.isUserLoggedIn()) { |
133 | this.localUnsubscribe() | |
134 | } | |
135 | } | |
136 | ||
137 | localUnsubscribe () { | |
41eb700f RK |
138 | const observableBatch: any = [] |
139 | ||
140 | this.videoChannels | |
141 | .filter(videoChannel => this.subscribeStatus(true).includes(this.getChannelHandler(videoChannel))) | |
142 | .forEach(videoChannel => observableBatch.push( | |
143 | this.userSubscriptionService.deleteSubscription(this.getChannelHandler(videoChannel)) | |
144 | )) | |
145 | ||
146 | forkJoin(observableBatch) | |
22a16e36 C |
147 | .subscribe( |
148 | () => { | |
41eb700f RK |
149 | [...this.subscribed.keys()].forEach((key) => { |
150 | this.subscribed.set(key, false) | |
151 | }) | |
22a16e36 | 152 | |
f8b2c1b4 | 153 | this.notifier.success( |
41eb700f RK |
154 | this.account |
155 | ? this.i18n('Unsubscribed from all channels of {{nameWithHost}}', { nameWithHost: this.account.nameWithHost }) | |
156 | : this.i18n('Unsubscribed from {{nameWithHost}}', { nameWithHost: this.videoChannels[0].nameWithHost }) | |
157 | , | |
f8b2c1b4 | 158 | this.i18n('Unsubscribed') |
22a16e36 C |
159 | ) |
160 | }, | |
161 | ||
f8b2c1b4 | 162 | err => this.notifier.error(err.message) |
22a16e36 C |
163 | ) |
164 | } | |
660d11e9 RK |
165 | |
166 | isUserLoggedIn () { | |
167 | return this.authService.isLoggedIn() | |
168 | } | |
169 | ||
41eb700f RK |
170 | isAllChannelsSubscribed () { |
171 | return !Array.from(this.subscribed.values()).includes(false) | |
172 | } | |
173 | ||
660d11e9 RK |
174 | gotoLogin () { |
175 | this.router.navigate([ '/login' ]) | |
176 | } | |
41eb700f RK |
177 | |
178 | private getChannelHandler (videoChannel: VideoChannel) { | |
179 | return videoChannel.name + '@' + videoChannel.host | |
180 | } | |
181 | ||
182 | private subscribeStatus (subscribed: boolean) { | |
183 | const accumulator = [] | |
184 | for (const [key, value] of this.subscribed.entries()) { | |
185 | if (value === subscribed) accumulator.push(key) | |
186 | } | |
187 | return accumulator | |
188 | } | |
22a16e36 | 189 | } |