1 import { Component, Input, OnInit } 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'
11 import { toArray } from 'rxjs/operators'
14 selector: 'my-subscribe-button',
15 templateUrl: './subscribe-button.component.html',
16 styleUrls: [ './subscribe-button.component.scss' ]
18 export class SubscribeButtonComponent implements OnInit {
20 * SubscribeButtonComponent can be used with a single VideoChannel passed as [VideoChannel],
21 * or with an account and a full list of that account's videoChannels. The latter is intended
22 * to allow mass un/subscription from an account's page, while keeping the channel-centric
25 @Input() account: Account
26 @Input() videoChannels: VideoChannel[]
27 @Input() displayFollowers = false
28 @Input() size: 'small' | 'normal' = 'normal'
30 subscribed = new Map<string, boolean>()
33 private authService: AuthService,
34 private router: Router,
35 private notifier: Notifier,
36 private userSubscriptionService: UserSubscriptionService,
38 private videoService: VideoService
43 ? this.account.nameWithHost
44 : this.videoChannel.name + '@' + this.videoChannel.host
47 get channelHandle () {
48 return this.getChannelHandler(this.videoChannel)
54 : this.videoChannels[0].url
58 const rssFeed = this.account
60 .getAccountFeedUrls(this.account.id)
61 .find(i => i.format === FeedFormat.RSS)
63 .getVideoChannelFeedUrls(this.videoChannels[0].id)
64 .find(i => i.format === FeedFormat.RSS)
70 return this.videoChannels[0]
74 this.loadSubscribedStatus()
78 if (this.isUserLoggedIn()) {
79 return this.localSubscribe()
82 return this.gotoLogin()
86 const subscribedStatus = this.subscribeStatus(false)
88 const observableBatch = this.videoChannels
89 .map(videoChannel => this.getChannelHandler(videoChannel))
90 .filter(handle => subscribedStatus.includes(handle))
91 .map(handle => this.userSubscriptionService.addSubscription(handle))
93 forkJoin(observableBatch)
96 this.notifier.success(
99 'Subscribed to all current channels of {{nameWithHost}}. You will be notified of all their new videos.',
100 { nameWithHost: this.account.displayName }
103 'Subscribed to {{nameWithHost}}. You will be notified of all their new videos.',
104 { nameWithHost: this.videoChannels[0].displayName }
107 this.i18n('Subscribed')
111 err => this.notifier.error(err.message)
116 if (this.isUserLoggedIn()) {
117 this.localUnsubscribe()
121 localUnsubscribe () {
122 const subscribeStatus = this.subscribeStatus(true)
124 const observableBatch = this.videoChannels
125 .map(videoChannel => this.getChannelHandler(videoChannel))
126 .filter(handle => subscribeStatus.includes(handle))
127 .map(handle => this.userSubscriptionService.deleteSubscription(handle))
129 concat(...observableBatch)
132 this.notifier.success(
134 ? this.i18n('Unsubscribed from all channels of {{nameWithHost}}', { nameWithHost: this.account.nameWithHost })
135 : this.i18n('Unsubscribed from {{nameWithHost}}', { nameWithHost: this.videoChannels[ 0 ].nameWithHost })
137 this.i18n('Unsubscribed')
141 error: err => this.notifier.error(err.message)
146 return this.authService.isLoggedIn()
149 isAllChannelsSubscribed () {
150 return !Array.from(this.subscribed.values()).includes(false)
153 isAtLeastOneChannelSubscribed () {
154 return this.subscribeStatus(true).length > 0
158 return this.isUserLoggedIn() && this.videoChannels.length > 1 && this.isAtLeastOneChannelSubscribed()
162 this.router.navigate([ '/login' ])
165 subscribeStatus (subscribed: boolean) {
166 const accumulator: string[] = []
167 for (const [key, value] of this.subscribed.entries()) {
168 if (value === subscribed) accumulator.push(key)
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 res => this.subscribed.set(handle, res),
191 err => this.notifier.error(err.message)