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 { I18n } from '@ngx-translate/i18n-polyfill'
7 import { FeedFormat } from '@shared/models'
8 import { UserSubscriptionService } from './user-subscription.service'
11 selector: 'my-subscribe-button',
12 templateUrl: './subscribe-button.component.html',
13 styleUrls: [ './subscribe-button.component.scss' ]
15 export class SubscribeButtonComponent implements OnInit, OnChanges {
17 * SubscribeButtonComponent can be used with a single VideoChannel passed as [VideoChannel],
18 * or with an account and a full list of that account's videoChannels. The latter is intended
19 * to allow mass un/subscription from an account's page, while keeping the channel-centric
22 @Input() account: Account
23 @Input() videoChannels: VideoChannel[]
24 @Input() displayFollowers = false
25 @Input() size: 'small' | 'normal' = 'normal'
27 subscribed = new Map<string, boolean>()
30 private authService: AuthService,
31 private router: Router,
32 private notifier: Notifier,
33 private userSubscriptionService: UserSubscriptionService,
35 private videoService: VideoService
40 ? this.account.nameWithHost
41 : this.videoChannel.name + '@' + this.videoChannel.host
44 get channelHandle () {
45 return this.getChannelHandler(this.videoChannel)
51 : this.videoChannels[0].url
55 const rssFeed = this.account
57 .getAccountFeedUrls(this.account.id)
58 .find(i => i.format === FeedFormat.RSS)
60 .getVideoChannelFeedUrls(this.videoChannels[0].id)
61 .find(i => i.format === FeedFormat.RSS)
67 return this.videoChannels[0]
70 get isAllChannelsSubscribed () {
71 return this.subscribeStatus(true).length === this.videoChannels.length
74 get isAtLeastOneChannelSubscribed () {
75 return this.subscribeStatus(true).length > 0
79 return this.isUserLoggedIn() && this.videoChannels.length > 1 && this.isAtLeastOneChannelSubscribed
83 this.loadSubscribedStatus()
91 if (this.isUserLoggedIn()) {
92 return this.localSubscribe()
95 return this.gotoLogin()
99 const subscribedStatus = this.subscribeStatus(false)
101 const observableBatch = this.videoChannels
102 .map(videoChannel => this.getChannelHandler(videoChannel))
103 .filter(handle => subscribedStatus.includes(handle))
104 .map(handle => this.userSubscriptionService.addSubscription(handle))
106 forkJoin(observableBatch)
109 this.notifier.success(
112 'Subscribed to all current channels of {{nameWithHost}}. You will be notified of all their new videos.',
113 { nameWithHost: this.account.displayName }
116 'Subscribed to {{nameWithHost}}. You will be notified of all their new videos.',
117 { nameWithHost: this.videoChannels[0].displayName }
120 this.i18n('Subscribed')
124 err => this.notifier.error(err.message)
129 if (this.isUserLoggedIn()) {
130 this.localUnsubscribe()
134 localUnsubscribe () {
135 const subscribeStatus = this.subscribeStatus(true)
137 const observableBatch = this.videoChannels
138 .map(videoChannel => this.getChannelHandler(videoChannel))
139 .filter(handle => subscribeStatus.includes(handle))
140 .map(handle => this.userSubscriptionService.deleteSubscription(handle))
142 concat(...observableBatch)
145 this.notifier.success(
147 ? this.i18n('Unsubscribed from all channels of {{nameWithHost}}', { nameWithHost: this.account.nameWithHost })
148 : this.i18n('Unsubscribed from {{nameWithHost}}', { nameWithHost: this.videoChannels[ 0 ].nameWithHost })
150 this.i18n('Unsubscribed')
154 error: err => this.notifier.error(err.message)
159 return this.authService.isLoggedIn()
163 this.router.navigate([ '/login' ])
166 subscribeStatus (subscribed: boolean) {
167 const accumulator: string[] = []
168 for (const [key, value] of this.subscribed.entries()) {
169 if (value === subscribed) accumulator.push(key)
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 res => this.subscribed.set(handle, res),
192 err => this.notifier.error(err.message)