1
0
Fork 0
peertube/client/src/app/shared/shared-user-subscription/subscribe-button.component.ts

196 lines
5.5 KiB
TypeScript
Raw Normal View History

2020-06-23 12:10:17 +00:00
import { concat, forkJoin, merge } from 'rxjs'
import { Component, Input, OnChanges, OnInit } from '@angular/core'
import { AuthService, Notifier, RedirectService } from '@app/core'
2020-06-23 12:10:17 +00:00
import { Account, VideoChannel, VideoService } from '@app/shared/shared-main'
import { FeedFormat } from '@shared/models'
import { UserSubscriptionService } from './user-subscription.service'
2018-08-21 14:18:59 +00:00
@Component({
selector: 'my-subscribe-button',
templateUrl: './subscribe-button.component.html',
styleUrls: [ './subscribe-button.component.scss' ]
})
export class SubscribeButtonComponent implements OnInit, OnChanges {
/**
* SubscribeButtonComponent can be used with a single VideoChannel passed as [VideoChannel],
* or with an account and a full list of that account's videoChannels. The latter is intended
* to allow mass un/subscription from an account's page, while keeping the channel-centric
* subscription model.
*/
@Input() account: Account
@Input() videoChannels: VideoChannel[]
2018-08-21 14:18:59 +00:00
@Input() displayFollowers = false
2018-08-23 15:58:39 +00:00
@Input() size: 'small' | 'normal' = 'normal'
2018-08-21 14:18:59 +00:00
subscribed = new Map<string, boolean>()
2018-08-21 14:18:59 +00:00
constructor (
private authService: AuthService,
private redirectService: RedirectService,
private notifier: Notifier,
2018-08-21 14:18:59 +00:00
private userSubscriptionService: UserSubscriptionService,
2018-09-26 07:39:41 +00:00
private videoService: VideoService
) { }
get handle () {
return this.account
? this.account.nameWithHost
2020-01-10 10:14:57 +00:00
: this.videoChannel.name + '@' + this.videoChannel.host
}
2018-08-21 14:18:59 +00:00
get channelHandle () {
2020-01-10 10:14:57 +00:00
return this.getChannelHandler(this.videoChannel)
2018-08-21 14:18:59 +00:00
}
get uri () {
return this.account
? this.account.url
: this.videoChannels[0].url
}
get rssUri () {
const rssFeed = this.account
? this.videoService
.getAccountFeedUrls(this.account.id)
.find(i => i.format === FeedFormat.RSS)
: this.videoService
.getVideoChannelFeedUrls(this.videoChannels[0].id)
.find(i => i.format === FeedFormat.RSS)
return rssFeed.url
}
2020-01-10 10:14:57 +00:00
get videoChannel () {
return this.videoChannels[0]
}
get isAllChannelsSubscribed () {
return this.subscribeStatus(true).length === this.videoChannels.length
}
get isAtLeastOneChannelSubscribed () {
return this.subscribeStatus(true).length > 0
}
get isBigButton () {
return this.isUserLoggedIn() && this.videoChannels.length > 1 && this.isAtLeastOneChannelSubscribed
}
2018-08-21 14:18:59 +00:00
ngOnInit () {
this.loadSubscribedStatus()
2018-08-21 14:18:59 +00:00
}
ngOnChanges () {
this.ngOnInit()
}
2018-08-21 14:18:59 +00:00
subscribe () {
if (this.isUserLoggedIn()) {
2018-12-11 14:27:46 +00:00
return this.localSubscribe()
}
2018-12-11 14:27:46 +00:00
return this.gotoLogin()
}
localSubscribe () {
2020-01-10 10:15:07 +00:00
const subscribedStatus = this.subscribeStatus(false)
const observableBatch = this.videoChannels
.map(videoChannel => this.getChannelHandler(videoChannel))
2020-01-10 10:15:07 +00:00
.filter(handle => subscribedStatus.includes(handle))
.map(handle => this.userSubscriptionService.addSubscription(handle))
2020-01-10 10:15:07 +00:00
forkJoin(observableBatch)
2021-08-17 09:27:47 +00:00
.subscribe({
next: () => {
this.notifier.success(
this.account
? $localize`Subscribed to all current channels of ${this.account.displayName}. You will be notified of all their new videos.`
: $localize`Subscribed to ${this.videoChannels[0].displayName}. You will be notified of all their new videos.`,
$localize`Subscribed`
2018-08-21 14:18:59 +00:00
)
},
2021-08-17 09:27:47 +00:00
error: err => this.notifier.error(err.message)
})
2018-08-21 14:18:59 +00:00
}
unsubscribe () {
if (this.isUserLoggedIn()) {
this.localUnsubscribe()
}
}
localUnsubscribe () {
2020-01-10 10:15:07 +00:00
const subscribeStatus = this.subscribeStatus(true)
const observableBatch = this.videoChannels
2020-01-10 10:15:07 +00:00
.map(videoChannel => this.getChannelHandler(videoChannel))
.filter(handle => subscribeStatus.includes(handle))
.map(handle => this.userSubscriptionService.deleteSubscription(handle))
2020-01-10 10:15:07 +00:00
concat(...observableBatch)
.subscribe({
complete: () => {
this.notifier.success(
this.account
? $localize`Unsubscribed from all channels of ${this.account.nameWithHost}`
2021-08-17 12:42:53 +00:00
: $localize`Unsubscribed from ${this.videoChannels[0].nameWithHost}`,
$localize`Unsubscribed`
2020-01-10 10:15:07 +00:00
)
},
2018-08-21 14:18:59 +00:00
2020-01-10 10:15:07 +00:00
error: err => this.notifier.error(err.message)
})
2018-08-21 14:18:59 +00:00
}
isUserLoggedIn () {
return this.authService.isLoggedIn()
}
gotoLogin () {
this.redirectService.redirectToLogin()
}
2020-01-10 10:14:57 +00:00
subscribeStatus (subscribed: boolean) {
const accumulator: string[] = []
2021-08-17 12:42:53 +00:00
for (const [ key, value ] of this.subscribed.entries()) {
if (value === subscribed) accumulator.push(key)
}
2020-01-10 10:14:57 +00:00
return accumulator
}
2021-07-12 08:03:46 +00:00
isSubscribedToAll () {
return Array.from(this.subscribed.values()).every(v => v === true)
}
2022-02-22 13:38:57 +00:00
isRemoteSubscribeAvailable () {
return !this.isUserLoggedIn()
}
2020-01-10 10:14:57 +00:00
private getChannelHandler (videoChannel: VideoChannel) {
return videoChannel.name + '@' + videoChannel.host
}
private loadSubscribedStatus () {
if (!this.isUserLoggedIn()) return
for (const videoChannel of this.videoChannels) {
const handle = this.getChannelHandler(videoChannel)
this.subscribed.set(handle, false)
2020-01-10 10:15:07 +00:00
merge(
this.userSubscriptionService.listenToSubscriptionCacheChange(handle),
this.userSubscriptionService.doesSubscriptionExist(handle)
2021-08-17 09:27:47 +00:00
).subscribe({
next: res => this.subscribed.set(handle, res),
2021-08-17 09:27:47 +00:00
error: err => this.notifier.error(err.message)
})
}
}
2018-08-21 14:18:59 +00:00
}