1 import { Component, Input, OnInit } from '@angular/core'
2 import { Router } from '@angular/router'
3 import { AuthService } from '@app/core'
4 import { UserSubscriptionService } from '@app/shared/user-subscription/user-subscription.service'
5 import { VideoChannel } from '@app/shared/video-channel/video-channel.model'
6 import { NotificationsService } from 'angular2-notifications'
7 import { I18n } from '@ngx-translate/i18n-polyfill'
8 import { VideoService } from '@app/shared/video/video.service'
9 import { FeedFormat } from '../../../../../shared/models/feeds'
12 selector: 'my-subscribe-button',
13 templateUrl: './subscribe-button.component.html',
14 styleUrls: [ './subscribe-button.component.scss' ]
16 export class SubscribeButtonComponent implements OnInit {
17 @Input() videoChannel: VideoChannel
18 @Input() displayFollowers = false
19 @Input() size: 'small' | 'normal' = 'normal'
24 private authService: AuthService,
25 private router: Router,
26 private notificationsService: NotificationsService,
27 private userSubscriptionService: UserSubscriptionService,
29 private videoService: VideoService
33 return this.videoChannel.name + '@' + this.videoChannel.host
37 return this.videoChannel.ownerAccount.name + '@' + this.videoChannel.host
41 if (this.isUserLoggedIn()) {
42 this.userSubscriptionService.isSubscriptionExists(this.uri)
44 res => this.subscribed = res[this.uri],
46 err => this.notificationsService.error(this.i18n('Error'), err.message)
52 if (this.isUserLoggedIn()) {
55 this.authService.redirectUrl = this.router.url
61 this.userSubscriptionService.addSubscription(this.uri)
64 this.subscribed = true
66 this.notificationsService.success(
67 this.i18n('Subscribed'),
68 this.i18n('Subscribed to {{nameWithHost}}', { nameWithHost: this.videoChannel.displayName })
72 err => this.notificationsService.error(this.i18n('Error'), err.message)
77 if (this.isUserLoggedIn()) {
78 this.localUnsubscribe()
83 this.userSubscriptionService.deleteSubscription(this.uri)
86 this.subscribed = false
88 this.notificationsService.success(
89 this.i18n('Unsubscribed'),
90 this.i18n('Unsubscribed from {{nameWithHost}}', { nameWithHost: this.videoChannel.displayName })
94 err => this.notificationsService.error(this.i18n('Error'), err.message)
99 return this.authService.isLoggedIn()
103 this.router.navigate([ '/login' ])
107 const rssFeed = this.videoService
108 .getVideoChannelFeedUrls(this.videoChannel.id)
109 .find(i => i.format === FeedFormat.RSS)
111 window.open(rssFeed.url)