diff options
Diffstat (limited to 'client/src/app/shared/user-subscription/subscribe-button.component.ts')
-rw-r--r-- | client/src/app/shared/user-subscription/subscribe-button.component.ts | 86 |
1 files changed, 41 insertions, 45 deletions
diff --git a/client/src/app/shared/user-subscription/subscribe-button.component.ts b/client/src/app/shared/user-subscription/subscribe-button.component.ts index f0bee9d47..14a6bfe66 100644 --- a/client/src/app/shared/user-subscription/subscribe-button.component.ts +++ b/client/src/app/shared/user-subscription/subscribe-button.component.ts | |||
@@ -7,7 +7,7 @@ import { I18n } from '@ngx-translate/i18n-polyfill' | |||
7 | import { VideoService } from '@app/shared/video/video.service' | 7 | import { VideoService } from '@app/shared/video/video.service' |
8 | import { FeedFormat } from '../../../../../shared/models/feeds' | 8 | import { FeedFormat } from '../../../../../shared/models/feeds' |
9 | import { Account } from '@app/shared/account/account.model' | 9 | import { Account } from '@app/shared/account/account.model' |
10 | import { forkJoin } from 'rxjs' | 10 | import { forkJoin, merge } from 'rxjs' |
11 | 11 | ||
12 | @Component({ | 12 | @Component({ |
13 | selector: 'my-subscribe-button', | 13 | selector: 'my-subscribe-button', |
@@ -26,7 +26,7 @@ export class SubscribeButtonComponent implements OnInit { | |||
26 | @Input() displayFollowers = false | 26 | @Input() displayFollowers = false |
27 | @Input() size: 'small' | 'normal' = 'normal' | 27 | @Input() size: 'small' | 'normal' = 'normal' |
28 | 28 | ||
29 | subscribed: Map<string, boolean> | 29 | subscribed = new Map<string, boolean>() |
30 | 30 | ||
31 | constructor ( | 31 | constructor ( |
32 | private authService: AuthService, | 32 | private authService: AuthService, |
@@ -35,9 +35,7 @@ export class SubscribeButtonComponent implements OnInit { | |||
35 | private userSubscriptionService: UserSubscriptionService, | 35 | private userSubscriptionService: UserSubscriptionService, |
36 | private i18n: I18n, | 36 | private i18n: I18n, |
37 | private videoService: VideoService | 37 | private videoService: VideoService |
38 | ) { | 38 | ) { } |
39 | this.subscribed = new Map<string, boolean>() | ||
40 | } | ||
41 | 39 | ||
42 | get handle () { | 40 | get handle () { |
43 | return this.account | 41 | return this.account |
@@ -68,19 +66,7 @@ export class SubscribeButtonComponent implements OnInit { | |||
68 | } | 66 | } |
69 | 67 | ||
70 | ngOnInit () { | 68 | ngOnInit () { |
71 | if (this.isUserLoggedIn()) { | 69 | this.loadSubscribedStatus() |
72 | |||
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 | })) | ||
83 | } | ||
84 | } | 70 | } |
85 | 71 | ||
86 | subscribe () { | 72 | subscribe () { |
@@ -92,31 +78,22 @@ export class SubscribeButtonComponent implements OnInit { | |||
92 | } | 78 | } |
93 | 79 | ||
94 | localSubscribe () { | 80 | localSubscribe () { |
95 | const observableBatch: any = [] | 81 | const observableBatch = this.videoChannels |
96 | 82 | .map(videoChannel => this.getChannelHandler(videoChannel)) | |
97 | this.videoChannels | 83 | .filter(handle => this.subscribeStatus(false).includes(handle)) |
98 | .filter(videoChannel => this.subscribeStatus(false).includes(this.getChannelHandler(videoChannel))) | 84 | .map(handle => this.userSubscriptionService.addSubscription(handle)) |
99 | .forEach(videoChannel => observableBatch.push( | ||
100 | this.userSubscriptionService.addSubscription(this.getChannelHandler(videoChannel)) | ||
101 | )) | ||
102 | 85 | ||
103 | forkJoin(observableBatch) | 86 | forkJoin(observableBatch) |
104 | .subscribe( | 87 | .subscribe( |
105 | () => { | 88 | () => { |
106 | [...this.subscribed.keys()].forEach((key) => { | ||
107 | this.subscribed.set(key, true) | ||
108 | }) | ||
109 | |||
110 | this.notifier.success( | 89 | this.notifier.success( |
111 | this.account | 90 | this.account |
112 | ? this.i18n( | 91 | ? this.i18n( |
113 | 'Subscribed to all current channels of {{nameWithHost}}. ' + | 92 | 'Subscribed to all current channels of {{nameWithHost}}. You will be notified of all their new videos.', |
114 | 'You will be notified of all their new videos.', | ||
115 | { nameWithHost: this.account.displayName } | 93 | { nameWithHost: this.account.displayName } |
116 | ) | 94 | ) |
117 | : this.i18n( | 95 | : this.i18n( |
118 | 'Subscribed to {{nameWithHost}}. ' + | 96 | 'Subscribed to {{nameWithHost}}. You will be notified of all their new videos.', |
119 | 'You will be notified of all their new videos.', | ||
120 | { nameWithHost: this.videoChannels[0].displayName } | 97 | { nameWithHost: this.videoChannels[0].displayName } |
121 | ) | 98 | ) |
122 | , | 99 | , |
@@ -135,21 +112,14 @@ export class SubscribeButtonComponent implements OnInit { | |||
135 | } | 112 | } |
136 | 113 | ||
137 | localUnsubscribe () { | 114 | localUnsubscribe () { |
138 | const observableBatch: any = [] | 115 | const observableBatch = this.videoChannels |
139 | 116 | .map(videoChannel => this.getChannelHandler(videoChannel)) | |
140 | this.videoChannels | 117 | .filter(handle => this.subscribeStatus(true).includes(handle)) |
141 | .filter(videoChannel => this.subscribeStatus(true).includes(this.getChannelHandler(videoChannel))) | 118 | .map(handle => this.userSubscriptionService.deleteSubscription(handle)) |
142 | .forEach(videoChannel => observableBatch.push( | ||
143 | this.userSubscriptionService.deleteSubscription(this.getChannelHandler(videoChannel)) | ||
144 | )) | ||
145 | 119 | ||
146 | forkJoin(observableBatch) | 120 | forkJoin(observableBatch) |
147 | .subscribe( | 121 | .subscribe( |
148 | () => { | 122 | () => { |
149 | [...this.subscribed.keys()].forEach((key) => { | ||
150 | this.subscribed.set(key, false) | ||
151 | }) | ||
152 | |||
153 | this.notifier.success( | 123 | this.notifier.success( |
154 | this.account | 124 | this.account |
155 | ? this.i18n('Unsubscribed from all channels of {{nameWithHost}}', { nameWithHost: this.account.nameWithHost }) | 125 | ? this.i18n('Unsubscribed from all channels of {{nameWithHost}}', { nameWithHost: this.account.nameWithHost }) |
@@ -171,6 +141,14 @@ export class SubscribeButtonComponent implements OnInit { | |||
171 | return !Array.from(this.subscribed.values()).includes(false) | 141 | return !Array.from(this.subscribed.values()).includes(false) |
172 | } | 142 | } |
173 | 143 | ||
144 | isAtLeastOneChannelSubscribed () { | ||
145 | return this.subscribeStatus(true).length > 0 | ||
146 | } | ||
147 | |||
148 | isBigButton () { | ||
149 | return this.videoChannels.length > 1 && this.isAtLeastOneChannelSubscribed() | ||
150 | } | ||
151 | |||
174 | gotoLogin () { | 152 | gotoLogin () { |
175 | this.router.navigate([ '/login' ]) | 153 | this.router.navigate([ '/login' ]) |
176 | } | 154 | } |
@@ -180,10 +158,28 @@ export class SubscribeButtonComponent implements OnInit { | |||
180 | } | 158 | } |
181 | 159 | ||
182 | private subscribeStatus (subscribed: boolean) { | 160 | private subscribeStatus (subscribed: boolean) { |
183 | const accumulator = [] | 161 | const accumulator: string[] = [] |
184 | for (const [key, value] of this.subscribed.entries()) { | 162 | for (const [key, value] of this.subscribed.entries()) { |
185 | if (value === subscribed) accumulator.push(key) | 163 | if (value === subscribed) accumulator.push(key) |
186 | } | 164 | } |
187 | return accumulator | 165 | return accumulator |
188 | } | 166 | } |
167 | |||
168 | private loadSubscribedStatus () { | ||
169 | if (!this.isUserLoggedIn()) return | ||
170 | |||
171 | for (const videoChannel of this.videoChannels) { | ||
172 | const handle = this.getChannelHandler(videoChannel) | ||
173 | this.subscribed.set(handle, false) | ||
174 | merge( | ||
175 | this.userSubscriptionService.listenToSubscriptionCacheChange(handle), | ||
176 | this.userSubscriptionService.doesSubscriptionExist(handle) | ||
177 | ) | ||
178 | .subscribe( | ||
179 | res => this.subscribed.set(handle, res), | ||
180 | |||
181 | err => this.notifier.error(err.message) | ||
182 | ) | ||
183 | } | ||
184 | } | ||
189 | } | 185 | } |