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

114 lines
3.0 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'
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 {
@Input() videoChannel: VideoChannel
@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: boolean
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
2018-08-21 14:18:59 +00:00
) { }
get channelHandle () {
2018-08-21 14:18:59 +00:00
return this.videoChannel.name + '@' + this.videoChannel.host
}
get channelUri () {
return this.videoChannel.url
}
get rssUri () {
const rssFeed = this.videoService
.getVideoChannelFeedUrls(this.videoChannel.id)
.find(i => i.format === FeedFormat.RSS)
return rssFeed.url
}
2018-08-21 14:18:59 +00:00
ngOnInit () {
if (this.isUserLoggedIn()) {
this.userSubscriptionService.doesSubscriptionExist(this.channelHandle)
.subscribe(
res => this.subscribed = res[this.channelHandle],
2018-08-21 14:18:59 +00:00
err => this.notifier.error(err.message)
)
}
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 () {
this.userSubscriptionService.addSubscription(this.channelHandle)
2018-08-21 14:18:59 +00:00
.subscribe(
() => {
this.subscribed = true
this.notifier.success(
2020-01-08 19:00:52 +00:00
this.i18n('Subscribed to {{nameWithHost}}. You will be notified of all their new videos.',
{ nameWithHost: this.videoChannel.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 () {
this.userSubscriptionService.deleteSubscription(this.channelHandle)
2018-08-21 14:18:59 +00:00
.subscribe(
() => {
this.subscribed = false
this.notifier.success(
this.i18n('Unsubscribed from {{nameWithHost}}', { nameWithHost: this.videoChannel.displayName }),
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()
}
gotoLogin () {
this.router.navigate([ '/login' ])
}
2018-08-21 14:18:59 +00:00
}