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 {
18 @Input() account: Account
19 @Input() videoChannels: VideoChannel[]
20 @Input() displayFollowers = false
21 @Input() size: 'small' | 'normal' = 'normal'
23 subscribed: Map<string, boolean>
26 private authService: AuthService,
27 private router: Router,
28 private notifier: Notifier,
29 private userSubscriptionService: UserSubscriptionService,
31 private videoService: VideoService
33 this.subscribed = new Map<string, boolean>()
38 ? this.account.nameWithHost
39 : this.videoChannels[0].name + '@' + this.videoChannels[0].host
42 get channelHandle () {
43 return this.getChannelHandler(this.videoChannels[0])
49 : this.videoChannels[0].url
53 const rssFeed = this.account
55 .getAccountFeedUrls(this.account.id)
56 .find(i => i.format === FeedFormat.RSS)
58 .getVideoChannelFeedUrls(this.videoChannels[0].id)
59 .find(i => i.format === FeedFormat.RSS)
65 if (this.isUserLoggedIn()) {
67 forkJoin(this.videoChannels.map(videoChannel => {
68 const handle = this.getChannelHandler(videoChannel)
69 this.subscribed.set(handle, false)
70 this.userSubscriptionService.doesSubscriptionExist(handle)
72 res => this.subscribed.set(handle, res[handle]),
74 err => this.notifier.error(err.message)
81 if (this.isUserLoggedIn()) {
82 return this.localSubscribe()
85 return this.gotoLogin()
89 const observableBatch: any = []
92 .filter(videoChannel => this.subscribeStatus(false).includes(this.getChannelHandler(videoChannel)))
93 .forEach(videoChannel => observableBatch.push(
94 this.userSubscriptionService.addSubscription(this.getChannelHandler(videoChannel))
97 forkJoin(observableBatch)
100 [...this.subscribed.keys()].forEach((key) => {
101 this.subscribed.set(key, true)
104 this.notifier.success(
107 'Subscribed to all current channels of {{nameWithHost}}. ' +
108 'You will be notified of all their new videos.',
109 { nameWithHost: this.account.displayName }
112 'Subscribed to {{nameWithHost}}. ' +
113 'You will be notified of all their new videos.',
114 { nameWithHost: this.videoChannels[0].displayName }
117 this.i18n('Subscribed')
121 err => this.notifier.error(err.message)
126 if (this.isUserLoggedIn()) {
127 this.localUnsubscribe()
131 localUnsubscribe () {
132 const observableBatch: any = []
135 .filter(videoChannel => this.subscribeStatus(true).includes(this.getChannelHandler(videoChannel)))
136 .forEach(videoChannel => observableBatch.push(
137 this.userSubscriptionService.deleteSubscription(this.getChannelHandler(videoChannel))
140 forkJoin(observableBatch)
143 [...this.subscribed.keys()].forEach((key) => {
144 this.subscribed.set(key, false)
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 err => this.notifier.error(err.message)
161 return this.authService.isLoggedIn()
164 isAllChannelsSubscribed () {
165 return !Array.from(this.subscribed.values()).includes(false)
169 this.router.navigate([ '/login' ])
172 private getChannelHandler (videoChannel: VideoChannel) {
173 return videoChannel.name + '@' + videoChannel.host
176 private subscribeStatus (subscribed: boolean) {
177 const accumulator = []
178 for (const [key, value] of this.subscribed.entries()) {
179 if (value === subscribed) accumulator.push(key)