1 import { Component, Input, OnInit, OnChanges } from '@angular/core'
2 import { Router } from '@angular/router'
3 import { AuthService, Notifier } from '@app/core'
4 import { UserSubscriptionService } from '@app/shared/user-subscription/user-subscription.service'
5 import { VideoChannel } from '@app/shared/video-channel/video-channel.model'
6 import { I18n } from '@ngx-translate/i18n-polyfill'
7 import { VideoService } from '@app/shared/video/video.service'
8 import { FeedFormat } from '../../../../../shared/models/feeds'
9 import { Account } from '@app/shared/account/account.model'
10 import { concat, forkJoin, merge } from 'rxjs'
13 selector: 'my-subscribe-button',
14 templateUrl: './subscribe-button.component.html',
15 styleUrls: [ './subscribe-button.component.scss' ]
17 export class SubscribeButtonComponent implements OnInit, OnChanges {
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
24 @Input() account: Account
25 @Input() videoChannels: VideoChannel[]
26 @Input() displayFollowers = false
27 @Input() size: 'small' | 'normal' = 'normal'
29 subscribed = new Map<string, boolean>()
32 private authService: AuthService,
33 private router: Router,
34 private notifier: Notifier,
35 private userSubscriptionService: UserSubscriptionService,
37 private videoService: VideoService
42 ? this.account.nameWithHost
43 : this.videoChannel.name + '@' + this.videoChannel.host
46 get channelHandle () {
47 return this.getChannelHandler(this.videoChannel)
53 : this.videoChannels[0].url
57 const rssFeed = this.account
59 .getAccountFeedUrls(this.account.id)
60 .find(i => i.format === FeedFormat.RSS)
62 .getVideoChannelFeedUrls(this.videoChannels[0].id)
63 .find(i => i.format === FeedFormat.RSS)
69 return this.videoChannels[0]
72 get isAllChannelsSubscribed () {
73 return this.subscribeStatus(true).length === this.videoChannels.length
76 get isAtLeastOneChannelSubscribed () {
77 return this.subscribeStatus(true).length > 0
81 return this.isUserLoggedIn() && this.videoChannels.length > 1 && this.isAtLeastOneChannelSubscribed
85 this.loadSubscribedStatus()
93 if (this.isUserLoggedIn()) {
94 return this.localSubscribe()
97 return this.gotoLogin()
101 const subscribedStatus = this.subscribeStatus(false)
103 const observableBatch = this.videoChannels
104 .map(videoChannel => this.getChannelHandler(videoChannel))
105 .filter(handle => subscribedStatus.includes(handle))
106 .map(handle => this.userSubscriptionService.addSubscription(handle))
108 forkJoin(observableBatch)
111 this.notifier.success(
114 'Subscribed to all current channels of {{nameWithHost}}. You will be notified of all their new videos.',
115 { nameWithHost: this.account.displayName }
118 'Subscribed to {{nameWithHost}}. You will be notified of all their new videos.',
119 { nameWithHost: this.videoChannels[0].displayName }
122 this.i18n('Subscribed')
126 err => this.notifier.error(err.message)
131 if (this.isUserLoggedIn()) {
132 this.localUnsubscribe()
136 localUnsubscribe () {
137 const subscribeStatus = this.subscribeStatus(true)
139 const observableBatch = this.videoChannels
140 .map(videoChannel => this.getChannelHandler(videoChannel))
141 .filter(handle => subscribeStatus.includes(handle))
142 .map(handle => this.userSubscriptionService.deleteSubscription(handle))
144 concat(...observableBatch)
147 this.notifier.success(
149 ? this.i18n('Unsubscribed from all channels of {{nameWithHost}}', { nameWithHost: this.account.nameWithHost })
150 : this.i18n('Unsubscribed from {{nameWithHost}}', { nameWithHost: this.videoChannels[ 0 ].nameWithHost })
152 this.i18n('Unsubscribed')
156 error: err => this.notifier.error(err.message)
161 return this.authService.isLoggedIn()
165 this.router.navigate([ '/login' ])
168 subscribeStatus (subscribed: boolean) {
169 const accumulator: string[] = []
170 for (const [key, value] of this.subscribed.entries()) {
171 if (value === subscribed) accumulator.push(key)
177 private getChannelHandler (videoChannel: VideoChannel) {
178 return videoChannel.name + '@' + videoChannel.host
181 private loadSubscribedStatus () {
182 if (!this.isUserLoggedIn()) return
184 for (const videoChannel of this.videoChannels) {
185 const handle = this.getChannelHandler(videoChannel)
186 this.subscribed.set(handle, false)
189 this.userSubscriptionService.listenToSubscriptionCacheChange(handle),
190 this.userSubscriptionService.doesSubscriptionExist(handle)
192 res => this.subscribed.set(handle, res),
194 err => this.notifier.error(err.message)