1 import { concat, forkJoin, merge } from 'rxjs'
2 import { Component, Input, OnChanges, OnInit } from '@angular/core'
3 import { Router } from '@angular/router'
4 import { AuthService, Notifier } from '@app/core'
5 import { Account, VideoChannel, VideoService } from '@app/shared/shared-main'
6 import { FeedFormat } from '@shared/models'
7 import { UserSubscriptionService } from './user-subscription.service'
10 selector: 'my-subscribe-button',
11 templateUrl: './subscribe-button.component.html',
12 styleUrls: [ './subscribe-button.component.scss' ]
14 export class SubscribeButtonComponent implements OnInit, OnChanges {
16 * SubscribeButtonComponent can be used with a single VideoChannel passed as [VideoChannel],
17 * or with an account and a full list of that account's videoChannels. The latter is intended
18 * to allow mass un/subscription from an account's page, while keeping the channel-centric
21 @Input() account: Account
22 @Input() videoChannels: VideoChannel[]
23 @Input() displayFollowers = false
24 @Input() size: 'small' | 'normal' = 'normal'
26 subscribed = new Map<string, boolean>()
29 private authService: AuthService,
30 private router: Router,
31 private notifier: Notifier,
32 private userSubscriptionService: UserSubscriptionService,
33 private videoService: VideoService
38 ? this.account.nameWithHost
39 : this.videoChannel.name + '@' + this.videoChannel.host
42 get channelHandle () {
43 return this.getChannelHandler(this.videoChannel)
49 : this.videoChannels[0].url
53 const rssFeed = this.account
55 .getAccountFeedUrls(this.account.id)
56 .find(i => i.format === FeedFormat.RSS)
58 .getVideoChannelFeedUrls(this.videoChannels[0].id)
59 .find(i => i.format === FeedFormat.RSS)
65 return this.videoChannels[0]
68 get isAllChannelsSubscribed () {
69 return this.subscribeStatus(true).length === this.videoChannels.length
72 get isAtLeastOneChannelSubscribed () {
73 return this.subscribeStatus(true).length > 0
77 return this.isUserLoggedIn() && this.videoChannels.length > 1 && this.isAtLeastOneChannelSubscribed
81 this.loadSubscribedStatus()
89 if (this.isUserLoggedIn()) {
90 return this.localSubscribe()
93 return this.gotoLogin()
97 const subscribedStatus = this.subscribeStatus(false)
99 const observableBatch = this.videoChannels
100 .map(videoChannel => this.getChannelHandler(videoChannel))
101 .filter(handle => subscribedStatus.includes(handle))
102 .map(handle => this.userSubscriptionService.addSubscription(handle))
104 forkJoin(observableBatch)
107 this.notifier.success(
109 ? $localize`Subscribed to all current channels of ${this.account.displayName}. You will be notified of all their new videos.`
110 : $localize`Subscribed to ${this.videoChannels[0].displayName}. You will be notified of all their new videos.`,
112 $localize`Subscribed`
116 error: err => this.notifier.error(err.message)
121 if (this.isUserLoggedIn()) {
122 this.localUnsubscribe()
126 localUnsubscribe () {
127 const subscribeStatus = this.subscribeStatus(true)
129 const observableBatch = this.videoChannels
130 .map(videoChannel => this.getChannelHandler(videoChannel))
131 .filter(handle => subscribeStatus.includes(handle))
132 .map(handle => this.userSubscriptionService.deleteSubscription(handle))
134 concat(...observableBatch)
137 this.notifier.success(
139 ? $localize`Unsubscribed from all channels of ${this.account.nameWithHost}`
140 : $localize`Unsubscribed from ${this.videoChannels[0].nameWithHost}`,
142 $localize`Unsubscribed`
146 error: err => this.notifier.error(err.message)
151 return this.authService.isLoggedIn()
155 this.router.navigate([ '/login' ])
158 subscribeStatus (subscribed: boolean) {
159 const accumulator: string[] = []
160 for (const [ key, value ] of this.subscribed.entries()) {
161 if (value === subscribed) accumulator.push(key)
167 isSubscribedToAll () {
168 return Array.from(this.subscribed.values()).every(v => v === true)
171 isRemoteSubscribeAvailable () {
172 return !this.isUserLoggedIn()
175 private getChannelHandler (videoChannel: VideoChannel) {
176 return videoChannel.name + '@' + videoChannel.host
179 private loadSubscribedStatus () {
180 if (!this.isUserLoggedIn()) return
182 for (const videoChannel of this.videoChannels) {
183 const handle = this.getChannelHandler(videoChannel)
184 this.subscribed.set(handle, false)
187 this.userSubscriptionService.listenToSubscriptionCacheChange(handle),
188 this.userSubscriptionService.doesSubscriptionExist(handle)
190 next: res => this.subscribed.set(handle, res),
192 error: err => this.notifier.error(err.message)