]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/user-subscription/subscribe-button.component.ts
Cleanup DB indexes
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / user-subscription / subscribe-button.component.ts
CommitLineData
22a16e36 1import { Component, Input, OnInit } from '@angular/core'
660d11e9 2import { Router } from '@angular/router'
f8b2c1b4 3import { AuthService, Notifier } from '@app/core'
22a16e36
C
4import { UserSubscriptionService } from '@app/shared/user-subscription/user-subscription.service'
5import { VideoChannel } from '@app/shared/video-channel/video-channel.model'
22a16e36 6import { I18n } from '@ngx-translate/i18n-polyfill'
39ba2e8e
C
7import { VideoService } from '@app/shared/video/video.service'
8import { FeedFormat } from '../../../../../shared/models/feeds'
41eb700f 9import { Account } from '@app/shared/account/account.model'
9270ccf6 10import { forkJoin, merge } from 'rxjs'
22a16e36
C
11
12@Component({
13 selector: 'my-subscribe-button',
14 templateUrl: './subscribe-button.component.html',
15 styleUrls: [ './subscribe-button.component.scss' ]
16})
17export class SubscribeButtonComponent implements OnInit {
b061c8ed
RK
18 /**
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
22 * subscription model.
23 */
41eb700f
RK
24 @Input() account: Account
25 @Input() videoChannels: VideoChannel[]
22a16e36 26 @Input() displayFollowers = false
f37dc0dd 27 @Input() size: 'small' | 'normal' = 'normal'
22a16e36 28
9270ccf6 29 subscribed = new Map<string, boolean>()
22a16e36
C
30
31 constructor (
660d11e9
RK
32 private authService: AuthService,
33 private router: Router,
f8b2c1b4 34 private notifier: Notifier,
22a16e36 35 private userSubscriptionService: UserSubscriptionService,
39ba2e8e
C
36 private i18n: I18n,
37 private videoService: VideoService
9270ccf6 38 ) { }
41eb700f
RK
39
40 get handle () {
41 return this.account
42 ? this.account.nameWithHost
43 : this.videoChannels[0].name + '@' + this.videoChannels[0].host
44 }
22a16e36 45
3ddb1ec5 46 get channelHandle () {
41eb700f 47 return this.getChannelHandler(this.videoChannels[0])
22a16e36
C
48 }
49
41eb700f
RK
50 get uri () {
51 return this.account
52 ? this.account.url
53 : this.videoChannels[0].url
660d11e9
RK
54 }
55
405ec98b 56 get rssUri () {
41eb700f
RK
57 const rssFeed = this.account
58 ? this.videoService
59 .getAccountFeedUrls(this.account.id)
60 .find(i => i.format === FeedFormat.RSS)
61 : this.videoService
62 .getVideoChannelFeedUrls(this.videoChannels[0].id)
63 .find(i => i.format === FeedFormat.RSS)
405ec98b
FS
64
65 return rssFeed.url
66 }
67
22a16e36 68 ngOnInit () {
9270ccf6 69 this.loadSubscribedStatus()
22a16e36
C
70 }
71
72 subscribe () {
660d11e9 73 if (this.isUserLoggedIn()) {
dae5ca24 74 return this.localSubscribe()
660d11e9 75 }
dae5ca24
C
76
77 return this.gotoLogin()
660d11e9
RK
78 }
79
80 localSubscribe () {
9270ccf6
RK
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))
41eb700f
RK
85
86 forkJoin(observableBatch)
22a16e36
C
87 .subscribe(
88 () => {
f8b2c1b4 89 this.notifier.success(
41eb700f
RK
90 this.account
91 ? this.i18n(
9270ccf6 92 'Subscribed to all current channels of {{nameWithHost}}. You will be notified of all their new videos.',
41eb700f
RK
93 { nameWithHost: this.account.displayName }
94 )
95 : this.i18n(
9270ccf6 96 'Subscribed to {{nameWithHost}}. You will be notified of all their new videos.',
41eb700f
RK
97 { nameWithHost: this.videoChannels[0].displayName }
98 )
99 ,
f8b2c1b4 100 this.i18n('Subscribed')
22a16e36
C
101 )
102 },
103
f8b2c1b4 104 err => this.notifier.error(err.message)
22a16e36
C
105 )
106 }
107
108 unsubscribe () {
660d11e9
RK
109 if (this.isUserLoggedIn()) {
110 this.localUnsubscribe()
111 }
112 }
113
114 localUnsubscribe () {
9270ccf6
RK
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))
41eb700f
RK
119
120 forkJoin(observableBatch)
22a16e36
C
121 .subscribe(
122 () => {
f8b2c1b4 123 this.notifier.success(
41eb700f
RK
124 this.account
125 ? this.i18n('Unsubscribed from all channels of {{nameWithHost}}', { nameWithHost: this.account.nameWithHost })
126 : this.i18n('Unsubscribed from {{nameWithHost}}', { nameWithHost: this.videoChannels[0].nameWithHost })
127 ,
f8b2c1b4 128 this.i18n('Unsubscribed')
22a16e36
C
129 )
130 },
131
f8b2c1b4 132 err => this.notifier.error(err.message)
22a16e36
C
133 )
134 }
660d11e9
RK
135
136 isUserLoggedIn () {
137 return this.authService.isLoggedIn()
138 }
139
41eb700f
RK
140 isAllChannelsSubscribed () {
141 return !Array.from(this.subscribed.values()).includes(false)
142 }
143
9270ccf6
RK
144 isAtLeastOneChannelSubscribed () {
145 return this.subscribeStatus(true).length > 0
146 }
147
148 isBigButton () {
2d0d88a0 149 return this.isUserLoggedIn() && this.videoChannels.length > 1 && this.isAtLeastOneChannelSubscribed()
9270ccf6
RK
150 }
151
660d11e9
RK
152 gotoLogin () {
153 this.router.navigate([ '/login' ])
154 }
41eb700f
RK
155
156 private getChannelHandler (videoChannel: VideoChannel) {
157 return videoChannel.name + '@' + videoChannel.host
158 }
159
160 private subscribeStatus (subscribed: boolean) {
9270ccf6 161 const accumulator: string[] = []
41eb700f
RK
162 for (const [key, value] of this.subscribed.entries()) {
163 if (value === subscribed) accumulator.push(key)
164 }
165 return accumulator
166 }
9270ccf6
RK
167
168 private loadSubscribedStatus () {
169 if (!this.isUserLoggedIn()) return
170
171 for (const videoChannel of this.videoChannels) {
172 const handle = this.getChannelHandler(videoChannel)
173 this.subscribed.set(handle, false)
174 merge(
175 this.userSubscriptionService.listenToSubscriptionCacheChange(handle),
176 this.userSubscriptionService.doesSubscriptionExist(handle)
177 )
178 .subscribe(
179 res => this.subscribed.set(handle, res),
180
181 err => this.notifier.error(err.message)
182 )
183 }
184 }
22a16e36 185}