2018-08-21 14:18:59 +00:00
|
|
|
import { Component, Input, OnInit } from '@angular/core'
|
2018-09-25 13:42:58 +00:00
|
|
|
import { Router } from '@angular/router'
|
2018-12-19 15:04:34 +00:00
|
|
|
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 (
|
2018-09-25 13:42:58 +00:00
|
|
|
private authService: AuthService,
|
|
|
|
private router: Router,
|
2018-12-19 15:04:34 +00:00
|
|
|
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
|
|
|
) { }
|
|
|
|
|
2019-06-07 08:56:59 +00:00
|
|
|
get channelHandle () {
|
2018-08-21 14:18:59 +00:00
|
|
|
return this.videoChannel.name + '@' + this.videoChannel.host
|
|
|
|
}
|
|
|
|
|
2019-06-07 08:56:59 +00:00
|
|
|
get channelUri () {
|
|
|
|
return this.videoChannel.url
|
2018-09-25 13:42:58 +00:00
|
|
|
}
|
|
|
|
|
2018-08-21 14:18:59 +00:00
|
|
|
ngOnInit () {
|
2018-09-25 13:42:58 +00:00
|
|
|
if (this.isUserLoggedIn()) {
|
2019-06-07 08:56:59 +00:00
|
|
|
this.userSubscriptionService.doesSubscriptionExist(this.channelHandle)
|
2018-09-25 13:42:58 +00:00
|
|
|
.subscribe(
|
2019-06-07 08:56:59 +00:00
|
|
|
res => this.subscribed = res[this.channelHandle],
|
2018-08-21 14:18:59 +00:00
|
|
|
|
2018-12-19 15:04:34 +00:00
|
|
|
err => this.notifier.error(err.message)
|
2018-09-25 13:42:58 +00:00
|
|
|
)
|
|
|
|
}
|
2018-08-21 14:18:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
subscribe () {
|
2018-09-25 13:42:58 +00:00
|
|
|
if (this.isUserLoggedIn()) {
|
2018-12-11 14:27:46 +00:00
|
|
|
return this.localSubscribe()
|
2018-09-25 13:42:58 +00:00
|
|
|
}
|
2018-12-11 14:27:46 +00:00
|
|
|
|
|
|
|
return this.gotoLogin()
|
2018-09-25 13:42:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
localSubscribe () {
|
2019-06-07 08:56:59 +00:00
|
|
|
this.userSubscriptionService.addSubscription(this.channelHandle)
|
2018-08-21 14:18:59 +00:00
|
|
|
.subscribe(
|
|
|
|
() => {
|
|
|
|
this.subscribed = true
|
|
|
|
|
2018-12-19 15:04:34 +00:00
|
|
|
this.notifier.success(
|
|
|
|
this.i18n('Subscribed to {{nameWithHost}}', { nameWithHost: this.videoChannel.displayName }),
|
|
|
|
this.i18n('Subscribed')
|
2018-08-21 14:18:59 +00:00
|
|
|
)
|
|
|
|
},
|
|
|
|
|
2018-12-19 15:04:34 +00:00
|
|
|
err => this.notifier.error(err.message)
|
2018-08-21 14:18:59 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
unsubscribe () {
|
2018-09-25 13:42:58 +00:00
|
|
|
if (this.isUserLoggedIn()) {
|
|
|
|
this.localUnsubscribe()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
localUnsubscribe () {
|
2019-06-07 08:56:59 +00:00
|
|
|
this.userSubscriptionService.deleteSubscription(this.channelHandle)
|
2018-08-21 14:18:59 +00:00
|
|
|
.subscribe(
|
|
|
|
() => {
|
|
|
|
this.subscribed = false
|
|
|
|
|
2018-12-19 15:04:34 +00:00
|
|
|
this.notifier.success(
|
|
|
|
this.i18n('Unsubscribed from {{nameWithHost}}', { nameWithHost: this.videoChannel.displayName }),
|
|
|
|
this.i18n('Unsubscribed')
|
2018-08-21 14:18:59 +00:00
|
|
|
)
|
|
|
|
},
|
|
|
|
|
2018-12-19 15:04:34 +00:00
|
|
|
err => this.notifier.error(err.message)
|
2018-08-21 14:18:59 +00:00
|
|
|
)
|
|
|
|
}
|
2018-09-25 13:42:58 +00:00
|
|
|
|
|
|
|
isUserLoggedIn () {
|
|
|
|
return this.authService.isLoggedIn()
|
|
|
|
}
|
|
|
|
|
|
|
|
gotoLogin () {
|
|
|
|
this.router.navigate([ '/login' ])
|
|
|
|
}
|
|
|
|
|
|
|
|
rssOpen () {
|
2018-09-26 07:39:41 +00:00
|
|
|
const rssFeed = this.videoService
|
|
|
|
.getVideoChannelFeedUrls(this.videoChannel.id)
|
|
|
|
.find(i => i.format === FeedFormat.RSS)
|
|
|
|
|
|
|
|
window.open(rssFeed.url)
|
2018-09-25 13:42:58 +00:00
|
|
|
}
|
2018-08-21 14:18:59 +00:00
|
|
|
}
|