1 import { concat, forkJoin, merge } from 'rxjs'
2 import { Component, Input, OnChanges, OnInit } from '@angular/core'
3 import { AuthService, Notifier, RedirectService } from '@app/core'
4 import { Account, VideoChannel, VideoService } from '@app/shared/shared-main'
5 import { FeedFormat } from '@shared/models'
6 import { UserSubscriptionService } from './user-subscription.service'
9 selector: 'my-subscribe-button',
10 templateUrl: './subscribe-button.component.html',
11 styleUrls: [ './subscribe-button.component.scss' ]
13 export class SubscribeButtonComponent implements OnInit, OnChanges {
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
20 @Input() account: Account
21 @Input() videoChannels: VideoChannel[]
22 @Input() displayFollowers = false
23 @Input() size: 'small' | 'normal' = 'normal'
25 subscribed = new Map<string, boolean>()
28 private authService: AuthService,
29 private redirectService: RedirectService,
30 private notifier: Notifier,
31 private userSubscriptionService: UserSubscriptionService,
32 private videoService: VideoService
37 ? this.account.nameWithHost
38 : this.videoChannel.name + '@' + this.videoChannel.host
41 get channelHandle () {
42 return this.getChannelHandler(this.videoChannel)
48 : this.videoChannels[0].url
52 const rssFeed = this.account
54 .getAccountFeedUrls(this.account.id)
55 .find(i => i.format === FeedFormat.RSS)
57 .getVideoChannelFeedUrls(this.videoChannels[0].id)
58 .find(i => i.format === FeedFormat.RSS)
64 return this.videoChannels[0]
67 get isAllChannelsSubscribed () {
68 return this.subscribeStatus(true).length === this.videoChannels.length
71 get isAtLeastOneChannelSubscribed () {
72 return this.subscribeStatus(true).length > 0
76 return this.isUserLoggedIn() && this.videoChannels.length > 1 && this.isAtLeastOneChannelSubscribed
80 this.loadSubscribedStatus()
88 if (this.isUserLoggedIn()) {
89 return this.localSubscribe()
92 return this.gotoLogin()
96 const subscribedStatus = this.subscribeStatus(false)
98 const observableBatch = this.videoChannels
99 .map(videoChannel => this.getChannelHandler(videoChannel))
100 .filter(handle => subscribedStatus.includes(handle))
101 .map(handle => this.userSubscriptionService.addSubscription(handle))
103 forkJoin(observableBatch)
106 this.notifier.success(
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.`,
111 $localize`Subscribed`
115 error: err => this.notifier.error(err.message)
120 if (this.isUserLoggedIn()) {
121 this.localUnsubscribe()
125 localUnsubscribe () {
126 const subscribeStatus = this.subscribeStatus(true)
128 const observableBatch = this.videoChannels
129 .map(videoChannel => this.getChannelHandler(videoChannel))
130 .filter(handle => subscribeStatus.includes(handle))
131 .map(handle => this.userSubscriptionService.deleteSubscription(handle))
133 concat(...observableBatch)
136 this.notifier.success(
138 ? $localize`Unsubscribed from all channels of ${this.account.nameWithHost}`
139 : $localize`Unsubscribed from ${this.videoChannels[0].nameWithHost}`,
141 $localize`Unsubscribed`
145 error: err => this.notifier.error(err.message)
150 return this.authService.isLoggedIn()
154 this.redirectService.redirectToLogin()
157 subscribeStatus (subscribed: boolean) {
158 const accumulator: string[] = []
159 for (const [ key, value ] of this.subscribed.entries()) {
160 if (value === subscribed) accumulator.push(key)
166 isSubscribedToAll () {
167 return Array.from(this.subscribed.values()).every(v => v === true)
170 isRemoteSubscribeAvailable () {
171 return !this.isUserLoggedIn()
174 private getChannelHandler (videoChannel: VideoChannel) {
175 return videoChannel.name + '@' + videoChannel.host
178 private loadSubscribedStatus () {
179 if (!this.isUserLoggedIn()) return
181 for (const videoChannel of this.videoChannels) {
182 const handle = this.getChannelHandler(videoChannel)
183 this.subscribed.set(handle, false)
186 this.userSubscriptionService.listenToSubscriptionCacheChange(handle),
187 this.userSubscriptionService.doesSubscriptionExist(handle)
189 next: res => this.subscribed.set(handle, res),
191 error: err => this.notifier.error(err.message)