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

191 lines
5.6 KiB
TypeScript
Raw Normal View History

2018-08-21 14:18:59 +00:00
import { Component, Input, OnInit } from '@angular/core'
import { Router } from '@angular/router'
import { AuthService, Notifier } from '@app/core'
2018-08-21 14:18:59 +00:00
import { UserSubscriptionService } from '@app/shared/user-subscription/user-subscription.service'
import { VideoChannel } from '@app/shared/video-channel/video-channel.model'
import { I18n } from '@ngx-translate/i18n-polyfill'
2018-09-26 07:39:41 +00:00
import { VideoService } from '@app/shared/video/video.service'
import { FeedFormat } from '../../../../../shared/models/feeds'
import { Account } from '@app/shared/account/account.model'
import { forkJoin, merge } from 'rxjs'
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 {
/**
* 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 router: Router,
private notifier: Notifier,
2018-08-21 14:18:59 +00:00
private userSubscriptionService: UserSubscriptionService,
2018-09-26 07:39:41 +00:00
private i18n: I18n,
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]
}
2018-08-21 14:18:59 +00:00
ngOnInit () {
this.loadSubscribedStatus()
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 () {
const observableBatch = this.videoChannels
.map(videoChannel => this.getChannelHandler(videoChannel))
.filter(handle => this.subscribeStatus(false).includes(handle))
.map(handle => this.userSubscriptionService.addSubscription(handle))
2020-01-10 10:14:57 +00:00
merge(observableBatch, 2)
2018-08-21 14:18:59 +00:00
.subscribe(
() => {
this.notifier.success(
this.account
? this.i18n(
'Subscribed to all current channels of {{nameWithHost}}. You will be notified of all their new videos.',
{ nameWithHost: this.account.displayName }
)
: this.i18n(
'Subscribed to {{nameWithHost}}. You will be notified of all their new videos.',
{ nameWithHost: this.videoChannels[0].displayName }
)
,
this.i18n('Subscribed')
2018-08-21 14:18:59 +00:00
)
},
err => this.notifier.error(err.message)
2018-08-21 14:18:59 +00:00
)
}
unsubscribe () {
if (this.isUserLoggedIn()) {
this.localUnsubscribe()
}
}
localUnsubscribe () {
const observableBatch = this.videoChannels
.map(videoChannel => this.getChannelHandler(videoChannel))
.filter(handle => this.subscribeStatus(true).includes(handle))
.map(handle => this.userSubscriptionService.deleteSubscription(handle))
forkJoin(observableBatch)
2018-08-21 14:18:59 +00:00
.subscribe(
() => {
this.notifier.success(
this.account
? this.i18n('Unsubscribed from all channels of {{nameWithHost}}', { nameWithHost: this.account.nameWithHost })
: this.i18n('Unsubscribed from {{nameWithHost}}', { nameWithHost: this.videoChannels[0].nameWithHost })
,
this.i18n('Unsubscribed')
2018-08-21 14:18:59 +00:00
)
},
err => this.notifier.error(err.message)
2018-08-21 14:18:59 +00:00
)
}
isUserLoggedIn () {
return this.authService.isLoggedIn()
}
isAllChannelsSubscribed () {
return !Array.from(this.subscribed.values()).includes(false)
}
isAtLeastOneChannelSubscribed () {
return this.subscribeStatus(true).length > 0
}
isBigButton () {
return this.isUserLoggedIn() && this.videoChannels.length > 1 && this.isAtLeastOneChannelSubscribed()
}
gotoLogin () {
this.router.navigate([ '/login' ])
}
2020-01-10 10:14:57 +00:00
subscribeStatus (subscribed: boolean) {
const accumulator: string[] = []
for (const [key, value] of this.subscribed.entries()) {
if (value === subscribed) accumulator.push(key)
}
2020-01-10 10:14:57 +00:00
return accumulator
}
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)
merge(
this.userSubscriptionService.listenToSubscriptionCacheChange(handle),
this.userSubscriptionService.doesSubscriptionExist(handle)
)
.subscribe(
res => this.subscribed.set(handle, res),
err => this.notifier.error(err.message)
)
}
}
2018-08-21 14:18:59 +00:00
}