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 { 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 {
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.videoChannels[0].name + '@' + this.videoChannels[0].host
46 get channelHandle () {
47 return this.getChannelHandler(this.videoChannels[0])
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 this.loadSubscribedStatus()
73 if (this.isUserLoggedIn()) {
74 return this.localSubscribe()
77 return this.gotoLogin()
81 const observableBatch = this.videoChannels
82 .map(videoChannel => this.getChannelHandler(videoChannel))
83 .filter(handle => this.subscribeStatus(false).includes(handle))
84 .map(handle => this.userSubscriptionService.addSubscription(handle))
86 forkJoin(observableBatch)
89 this.notifier.success(
92 'Subscribed to all current channels of {{nameWithHost}}. You will be notified of all their new videos.',
93 { nameWithHost: this.account.displayName }
96 'Subscribed to {{nameWithHost}}. You will be notified of all their new videos.',
97 { nameWithHost: this.videoChannels[0].displayName }
100 this.i18n('Subscribed')
104 err => this.notifier.error(err.message)
109 if (this.isUserLoggedIn()) {
110 this.localUnsubscribe()
114 localUnsubscribe () {
115 const observableBatch = this.videoChannels
116 .map(videoChannel => this.getChannelHandler(videoChannel))
117 .filter(handle => this.subscribeStatus(true).includes(handle))
118 .map(handle => this.userSubscriptionService.deleteSubscription(handle))
120 forkJoin(observableBatch)
123 this.notifier.success(
125 ? this.i18n('Unsubscribed from all channels of {{nameWithHost}}', { nameWithHost: this.account.nameWithHost })
126 : this.i18n('Unsubscribed from {{nameWithHost}}', { nameWithHost: this.videoChannels[0].nameWithHost })
128 this.i18n('Unsubscribed')
132 err => this.notifier.error(err.message)
137 return this.authService.isLoggedIn()
140 isAllChannelsSubscribed () {
141 return !Array.from(this.subscribed.values()).includes(false)
144 isAtLeastOneChannelSubscribed () {
145 return this.subscribeStatus(true).length > 0
149 return this.isUserLoggedIn() && this.videoChannels.length > 1 && this.isAtLeastOneChannelSubscribed()
153 this.router.navigate([ '/login' ])
156 private getChannelHandler (videoChannel: VideoChannel) {
157 return videoChannel.name + '@' + videoChannel.host
160 private subscribeStatus (subscribed: boolean) {
161 const accumulator: string[] = []
162 for (const [key, value] of this.subscribed.entries()) {
163 if (value === subscribed) accumulator.push(key)
168 private loadSubscribedStatus () {
169 if (!this.isUserLoggedIn()) return
171 for (const videoChannel of this.videoChannels) {
172 const handle = this.getChannelHandler(videoChannel)
173 this.subscribed.set(handle, false)
175 this.userSubscriptionService.listenToSubscriptionCacheChange(handle),
176 this.userSubscriptionService.doesSubscriptionExist(handle)
179 res => this.subscribed.set(handle, res),
181 err => this.notifier.error(err.message)