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 } 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: Map<string, boolean>
32 private authService: AuthService,
33 private router: Router,
34 private notifier: Notifier,
35 private userSubscriptionService: UserSubscriptionService,
37 private videoService: VideoService
39 this.subscribed = new Map<string, boolean>()
44 ? this.account.nameWithHost
45 : this.videoChannels[0].name + '@' + this.videoChannels[0].host
48 get channelHandle () {
49 return this.getChannelHandler(this.videoChannels[0])
55 : this.videoChannels[0].url
59 const rssFeed = this.account
61 .getAccountFeedUrls(this.account.id)
62 .find(i => i.format === FeedFormat.RSS)
64 .getVideoChannelFeedUrls(this.videoChannels[0].id)
65 .find(i => i.format === FeedFormat.RSS)
71 if (this.isUserLoggedIn()) {
73 forkJoin(this.videoChannels.map(videoChannel => {
74 const handle = this.getChannelHandler(videoChannel)
75 this.subscribed.set(handle, false)
76 this.userSubscriptionService.doesSubscriptionExist(handle)
78 res => this.subscribed.set(handle, res[handle]),
80 err => this.notifier.error(err.message)
87 if (this.isUserLoggedIn()) {
88 return this.localSubscribe()
91 return this.gotoLogin()
95 const observableBatch: any = []
98 .filter(videoChannel => this.subscribeStatus(false).includes(this.getChannelHandler(videoChannel)))
99 .forEach(videoChannel => observableBatch.push(
100 this.userSubscriptionService.addSubscription(this.getChannelHandler(videoChannel))
103 forkJoin(observableBatch)
106 [...this.subscribed.keys()].forEach((key) => {
107 this.subscribed.set(key, true)
110 this.notifier.success(
113 'Subscribed to all current channels of {{nameWithHost}}. ' +
114 'You will be notified of all their new videos.',
115 { nameWithHost: this.account.displayName }
118 'Subscribed to {{nameWithHost}}. ' +
119 'You will be notified of all their new videos.',
120 { nameWithHost: this.videoChannels[0].displayName }
123 this.i18n('Subscribed')
127 err => this.notifier.error(err.message)
132 if (this.isUserLoggedIn()) {
133 this.localUnsubscribe()
137 localUnsubscribe () {
138 const observableBatch: any = []
141 .filter(videoChannel => this.subscribeStatus(true).includes(this.getChannelHandler(videoChannel)))
142 .forEach(videoChannel => observableBatch.push(
143 this.userSubscriptionService.deleteSubscription(this.getChannelHandler(videoChannel))
146 forkJoin(observableBatch)
149 [...this.subscribed.keys()].forEach((key) => {
150 this.subscribed.set(key, false)
153 this.notifier.success(
155 ? this.i18n('Unsubscribed from all channels of {{nameWithHost}}', { nameWithHost: this.account.nameWithHost })
156 : this.i18n('Unsubscribed from {{nameWithHost}}', { nameWithHost: this.videoChannels[0].nameWithHost })
158 this.i18n('Unsubscribed')
162 err => this.notifier.error(err.message)
167 return this.authService.isLoggedIn()
170 isAllChannelsSubscribed () {
171 return !Array.from(this.subscribed.values()).includes(false)
175 this.router.navigate([ '/login' ])
178 private getChannelHandler (videoChannel: VideoChannel) {
179 return videoChannel.name + '@' + videoChannel.host
182 private subscribeStatus (subscribed: boolean) {
183 const accumulator = []
184 for (const [key, value] of this.subscribed.entries()) {
185 if (value === subscribed) accumulator.push(key)